<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, &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<200 || 206<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 ,如需轉載請自行聯系原作者