您的位置:首页 > 理论基础 > 计算机网络

如何使用VC进行HTTP连接

2008-12-12 20:23 603 查看
CString httpRequest(char* lpHostName, short sPort, char* lpUrl, char* lpMethod, char* lpPostDara, int nPostDataLen)
{
HINTERNET hInternet, hConnect, hRequest;
BOOL bRet;
CString strResponse;

hInternet = InternetOpen(TEXT("User-Agent:"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (hInternet)
{
hConnect = InternetConnect(hInternet, lpHostName, sPort, NULL, TEXT("HTTP/1.1"), INTERNET_SERVICE_HTTP, 0, 0);
if (hConnect)
{
hRequest = HttpOpenRequest(hConnect, lpMethod, lpUrl, TEXT("HTTP/1.1"), NULL, NULL, INTERNET_FLAG_RELOAD, 0);
if (hRequest)
{
bRet = HttpSendRequest(hRequest, NULL, 0, lpPostDara, nPostDataLen);

while (TRUE)
{
char cReadBuf[4096];
unsigned long lNumOfByteRead;

bRet = InternetReadFile(hRequest, cReadBuf, sizeof(cReadBuf)-1, &lNumOfByteRead);
if(!bRet || !lNumOfByteRead)
{break;}
cReadBuf[lNumOfByteRead]=0;
strResponse = strResponse + cReadBuf;
}
}
InternetCloseHandle(hRequest);
}
InternetCloseHandle(hConnect);
}
InternetCloseHandle(hInternet);

return strResponse;
}

测试的时候,使用如下代码调用:
CString strResponse = httpRequest("www.hao123.com",80,NULL,"POST",NULL,0);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: