转自:http://jetyi.blog.51cto.com/1460128/628676
Windows中有一组WinINet函数(http://msdn.microsoft.com/en-us/library/aa385473(v=VS.85).aspx),其中关于向Internet发送/接受请求的函数比较奇怪,尤其是HttpSendRequest函数问题更是诡异,下面是代码示例(该代码只是分析出问题,但没找到原因)
这组测试代码假设网络都是正常的.
- BOOL SendHttpHeaderTest()
- {
- BOOL bRet = FALSE;
- if (ERROR_SUCCESS != ::InternetAttemptConnect(0))
- return FALSE;
-
- if (!::InternetCheckConnection(_T("http://www.baidu.com"), FLAG_ICC_FORCE_CONNECTION, 0))
- return FALSE;
-
- TCHAR szModuleFile[MAX_PATH] = {0};
- ::GetModuleFileName(::GetInstance(), szModuleFile, MAX_PATH);
- LPCTSTR lpPath = ::PathFindFileName(szModuleFile);
- HINTERNET hOpen = ::InternetOpen(lpPath, INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY, NULL, NULL, 0);
- DWORD dwErr = ::GetLastError();
-
- HINTERNET hConnect = ::InternetConnect(hOpen, _T("www.baidu.com"), INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
- dwErr = ::GetLastError();
-
- #if 1
- DWORD dwFlag = INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_NO_AUTO_REDIRECT | INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_NO_COOKIES;
- HINTERNET hOpenRequest = ::HttpOpenRequest(hConnect, _T("GET"), _T("img/baidu_sylogo1.gif"), _T("HTTP/1.1"), _T("http://www.baidu.com/"), NULL, dwFlag, 0);
- dwErr = ::GetLastError();
-
-
- bRet = ::HttpSendRequest(hOpenRequest, NULL, 0, NULL, 0);
- dwErr = ::GetLastError();
- #endif
-
- TCHAR szBuff[BUFF_LEN_1024] = {0};
- DWORD dwBuffSize = BUFF_LEN_1024;
- bRet = ::HttpQueryInfo(hOpenRequest, HTTP_QUERY_STATUS_CODE, (LPVOID)szBuff, &dwBuffSize, NULL);
- dwErr = ::GetLastError();
-
-
- int nStatusCode = _tstoi(szBuff);
- if (nStatusCode<200 || 206<nStatusCode)
- bRet = FALSE;
-
- bRet = TRUE;
- ::InternetCloseHandle(hConnect);
-
- ::InternetCloseHandle(hOpen);
-
- return bRet;
- }
#if 1 ... #endif HttpOpenRequest说明要向www.baidu.com请求baidu_sylogo1.gif,但这个函数是不会向www.baidu.com发送任何数据的,只有调用HttpSendRequest时才会发送这个请求.
HttpOpenRequest函数的返回值很有意思,可以看到它的返回值是有效的,证明对该函数的调用是成功的,但问题dwErr返回值是122,它的含义是 ERROR_INSUFFICIENT_BUFFER: The data area passed to a system call is too small,但不知道什么意思,也不知到如何才能使dwErr成为0.
根据msdn中的描述,这段代码可以用下面一段代码替换:
- #if 2
- HINTERNET hOpenRequest = ::HttpOpenRequest(hConnect, NULL, NULL, NULL, NULL, NULL, dwFlag, 0);
- dwErr = ::GetLastError();
-
- LPCTSTR lpHeader =
- _T("GET /img/baidu_sylogo1.gif /HTTP/1.1")
- _T("Referer: http://www.baidu.com/\r\n") //B line
- _T("Host: www.baidu.com")
- _T("\r\n\r\n");
-
- DWORD dwHeaderLen = _tcslen(lpHeader);
- bRet = ::HttpAddRequestHeaders(hOpenRequest, lpHeader, dwHeaderLen, HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE);
-
- bRet = ::HttpSendRequest(hOpenRequest, NULL, 0, NULL, 0);
- dwErr = ::GetLastError();
- #endif
-
-
- Test1:
- LPCTSTR lpHeader = _T("GET /img/baidu_sylogo1.gif /HTTP/1.1\r\n")_T("Referer: http://www.baidu.com/")_T("Host: www.baidu.com")_T("\r\n\r\n");
-
- Test2:
- LPCTSTR lpHeader = _T("GET /img/baidu_sylogo1.gif /HTTP/1.1")_T("Referer: http://www.baidu.com/")_T("Host: www.baidu.com")_T("\r\n\r\n");
-
- Test3:
- LPCTSTR lpHeader = _T("GET /img/baidu_sylogo1.gif /HTTP/1.1")_T("Referer: http://www.baidu.com/")_T("Host: www.baidu.com")_T("\r\n");
-
- Test4:
- LPCTSTR lpHeader = _T("GET /img/baidu_sylogo1.gif /HTTP/1.1")_T("Referer: http://www.baidu.com/")_T("Host: www.baidu.com");
-
从以上4个测试可以看出,HttpSendRequest的第二个参数header是一个字符串,A行必须不能有\r\n,其它行可以有,也可以没有.
在测试中还发现,仅仅只有A行会导致HttpSendRequest调用失败,必须得有B行.
其实lpHeader的值A行刚好对应HttpOpenRequest的参数lpszVerb,lpszObjectName和lpszVersion;B行对应参数lpszReferer.所以,#if 2...endif 还可以用下面的代码代替:
- #if 3
- hOpenRequestHandle = ::HttpOpenRequest(hConnectHandle, NULL, NULL, NULL, NULL, NULL, dwFlag, dwContext);
- LPCTSTR lpHeader = _T("GET /img/baidu_sylogo1.gif /HTTP/1.1")_T("Referer: http://www.baidu.com/");
- DWORD dwLen = _tcslen(lpHeader);
- bRet = ::HttpAddRequestHeaders(hOpenRequestHandle, lpHeader, nLen, HTTP_ADDREQ_FLAG_REPLACE|HTTP_ADDREQ_FLAG_ADD);
- bRet = ::HttpSendRequest(hOpenRequestHandle, NULL, 0, NULL, 0);
- #endif
以上这些测试都是在VS2008系统中测试出来的,如果有谁发现HttpSendRequest发送的HTTP头结构文档及其它特点,还请告诉我,不胜感激.