- 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);