首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

http get请求 SendRequest抛出错误

2012-05-09 
http get请求 SendRequest抛出异常通过以下代码实现发送http的get请求时,在win7下总是成功,在winxp sp3下,

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

[解决办法]
服务器是你的吗?

抓包检查请求数据
[解决办法]

探讨

通过抓到的包,请求好像没有发出去

[解决办法]
奇数次发送成功,偶数次发送失败

就是说首次发送成功,再次发送失败是吧,那你说的“发送成功”是不是得到了正确结果?再次发送的时候是怎么操作的?重启应用程序?
[解决办法]
探讨

get请求失败后重新发一次get请求,能成功。下线,再次重新通过客户端连接指定网络,首次发get请求后总是失败

热点排行