天天看点

web服务器读写权限可能会引起HttpSendRequest返回错误码12152

<code>strcpy</code><code>(szURL, </code><code>"http://www.xxx.com"</code><code>) // xxx 仅仅为了说明问题</code>

<code>if</code> <code>(!::InternetCheckConnection(szURL, FLAG_ICC_FORCE_CONNECTION, 0))</code>

<code>    </code><code>return</code> <code>FALSE;</code>

<code>TCHAR</code> <code>szModuleFile[MAX_PATH] = {0};</code>

<code>::GetModuleFileName(NULL, szModuleFile, MAX_PATH);</code>

<code>LPCTSTR</code> <code>lpPath = ::PathFindFileName(szModuleFile);</code>

<code>HINTERNET hOpen = ::InternetOpen(lpPath, INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY, NULL, NULL, 0);</code>

<code>if</code> <code>(NULL == hOpen)</code>

<code>HINTERNET hConnect = ::InternetConnect(hOpen, lpDomain, dwPort, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);</code>

<code>if</code> <code>(NULL == hConnect)</code>

<code>    </code><code>goto</code> <code>FUN_END2;</code>

<code>LPCTSTR</code> <code>szAccept[] = {_T(</code><code>"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"</code><code>), NULL};</code>

<code>DWORD</code> <code>dwFlag = INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_RELOAD | INTERNET_FLAG_KEEP_CONNECTION;</code>

<code>HINTERNET hOpenRequest = ::HttpOpenRequest(hConnect, _T(</code><code>"POST"</code><code>), _T(</code><code>"my_app"</code><code>),</code>

<code>    </code><code>_T(</code><code>"HTTP/1.1"</code><code>), szURL, szAccept, dwFlag, 0); </code><code>// my_app是apache扩展C++模块.</code>

<code>if</code> <code>(NULL == hOpenRequest)</code>

<code>    </code><code>goto</code> <code>FUN_END1;</code>

<code>BOOL</code> <code>bRet = FALSE;</code>

<code>TCHAR</code> <code>headerContentLength[64];</code>

<code>_stprintf(headerContentLength, _T(</code><code>"Content-Length: %d\r\n\r\n"</code><code>), nLen);</code>

<code>// 这个语句向http://www.xxx.com/my_app发送数据.</code>

<code>bRet = ::HttpSendRequest(hOpenRequest, headerContentLength, _tcslen(headerContentLength), utf8PostData, nLen);</code>

<code>DWORD</code> <code>dwErr = ::GetLastError();</code>

<code>if</code> <code>(!bRet)  </code><code>goto</code> <code>FUN_END1;</code>

<code>TCHAR</code> <code>szBuff[BUF_LEN_1024] = {0};</code>

<code>DWORD</code> <code>dwBuffSize = BUF_LEN_1024*</code><code>sizeof</code><code>(szBuff)-2;</code>

<code>bRet = ::HttpQueryInfo(hOpenRequest, HTTP_QUERY_STATUS_CODE, (</code><code>LPVOID</code><code>)szBuff, &amp;dwBuffSize, NULL);</code>

<code>//Reference to http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html</code>

<code>int</code> <code>nStatusCode = _tstoi(szBuff);</code>

<code>//if (nStatusCode&lt;200 || 206&lt;nStatusCode)</code>

<code>if</code> <code>(200 != nStatusCode)</code>

<code>    </code><code>bRet = FALSE;</code>

<code>......</code>

在调试过程中发现,HttpSendRequest总是返回FALSE,错误码为12152(MSDN定义,ERROR_HTTP_INVALID_SERVER_RESPONSE,The server response could not be parsed).经过抓包可知,网络正常,3次握手,4次分手都正常,说明apache确实收到了这份数据,通过浏览器访问http://www.xxx.com/my_app也正常.  

 有不少网友说该错误可能发生在客户端或者系统,例如杀毒软件,网络异常等等,现这里不是这种问题,经过调查发现,错误竟然出在服务端读写权限上.扩展模块my_app会将收到的数据写到目录log,但log的权限开始是"drwxr-xr-x.",后来修改了权限(chmod ugo+w log)为"drwxrwxrwx."竟然可以了.

 从这个错误可以看出,当apache不能处理某个http请求时,不会将相应的数据传递给C++扩展模块,而是返回错误(12152)给发送程序.

本文转自jetyi51CTO博客,原文链接:http://blog.51cto.com/jetyi/1254778 ,如需转载请自行联系原作者