关于网友提出的“ http get请求 SendRequest抛出异常”问题疑问,本网通过在网上对“ http get请求 SendRequest抛出异常”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:
问题: http get请求 SendRequest抛出异常描述:
通过以下代码实现发送http的get请求时,在win7下总是成功,在winxp sp3下,基数次发送成功,偶数次发送失败,http_file->SendRequest(0,0,(LPVOID)(szBodyTemp.c_str()),szBodyTemp.size())抛出异常。异常为:“与服务器的连接被重置”或“服务器返回的信息无效或不可识别”。
请教各位大侠,xp sp3下为啥会偶数次抛出异常?什么原因?解决方案是什么?
try
{
CString str_server_name;
CString str_object;
INTERNET_PORT port;
DWORD service_type;
if (!AfxParseURL(url.c_str(), service_type, str_server_name, str_object, port)
|| service_type != INTERNET_SERVICE_HTTP)
{
TRACE(_T("Error: can only use URLs beginning with http://\n"));
ThrowException(-1);
}
iTimes ++;
VERIFY(session.EnableStatusCallback(TRUE));
session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT,1000* timeout_in_second);
http_server = session.GetHttpConnection(str_server_name, port, _T(""), _T(""));
if (http_server == NULL)
{
ThrowException(-1);
}
http_file = http_server->OpenRequest(verb, str_object, NULL, 1, NULL, NULL, http_request_flags);
http_file->AddRequestHeaders(strHeaders.c_str());
if(CHttpConnection::HTTP_VERB_POST == verb)
{
CString type;
type.AppendFormat(_T("Content-Type: application/x-www-form-urlencoded\r\nContent-Length: %d\r\n"),body.size());
http_file->AddRequestHeaders(type);
}
string szBodyTemp = WideToMultibyte(body);
bool bFlag = http_file->SendRequest(0,0,(LPVOID)(szBodyTemp.c_str()),szBodyTemp.size());
DWORD dwRet;
http_file->QueryInfoStatusCode(dwRet);
StatusCode = dwRet ;
if(StatusCode == 302 || StatusCode == 301)
{
CString csRetHeaders ;
http_file->QueryInfo(HTTP_QUERY_RAW_HEADERS_CRLF,csRetHeaders);
MovedLocation = GetLocation(csRetHeaders) ;
TRACE(_T("Moved Permanately:%s\n") ,MovedLocation.c_str()) ;
}
LPSTR szResponse;
CString strResponse;
DWORD dwResponseLength = http_file->GetLength();
while (0 != dwResponseLength)
{
szResponse = (LPSTR)malloc(dwResponseLength + 1);
szResponse[dwResponseLength] = '\0';
http_file->Read(szResponse, dwResponseLength);
strResponse += szResponse;
free(szResponse);
dwResponseLength = http_file->GetLength();
}
ResponseData = strResponse;
http_file->Close();
http_server->Close();
}
catch (CInternetException* pEx)
{
TCHAR szErr[1024];
pEx->GetErrorMessage(szErr, 1024);
TRACE(szErr);
return_code = -2;
pEx->Delete();
}
catch (CWISPrException* pEx)
{
return_code = pEx->m_nErrorCode;
TRACE1("Error: Exiting with CWISPrException(%d)\n", return_code);
pEx->Delete();
}
解决方案1:
那客户端启动后第一次请求能否成功?你试试重启客户端再发送 解决方案2:
奇数次发送成功,偶数次发送失败
就是说首次发送成功,再次发送失败是吧,那你说的“发送成功”是不是得到了正确结果?再次发送的时候是怎么操作的?重启应用程序?
试试发送到其它服务器 解决方案4:
服务器是你的吗?
抓包检查请求数据