我使用CInternetSession
来获取和发布请求。但是当connexion
超时发生时,我丢失了connexion
并且我总是得到无效的服务器请求错误,我不明白为什么。此外,还有内存泄漏。
#include "stdafx.h"
#include "httpConnexion.h"
#include "TSException.h"
ChttpConnexion::ChttpConnexion()
: CInternetSession(AfxGetAppName(), INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, NULL, INTERNET_FLAG_DONT_CACHE)
, m_lastRequest()
{
SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, 10000);
SetOption(INTERNET_OPTION_RECEIVE_TIMEOUT, 10000);
m_bAttente = true;
}
ChttpConnexion::~ChttpConnexion()
{
}
std::string ChttpConnexion::sendRequest(const std::string& strUrl)
{
DWORD dwServiceType;
CString strServerName;
CString strObject;
INTERNET_PORT nPort;
AfxParseURL(strUrl.c_str(), dwServiceType, strServerName, strObject, nPort);
CString strHeaders = _T("User-Agent: User-Agent=Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8\r\nAccept-Language: fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7\r\nConnection: keep-alive\r\nContent-Type: application/x-www-form-urlencoded");
CString strTmp = "", strResult = "";
CHttpConnection* pHttpConnexion = NULL;
CHttpFile* pHttpFile = NULL;
try
{
//Creation de la connexion Http
pHttpConnexion = GetHttpConnection(strServerName, INTERNET_FLAG_RELOAD | INTERNET_FLAG_DONT_CACHE, nPort, NULL, NULL);
//Creation de la requete GET
pHttpFile = pHttpConnexion->OpenRequest(CHttpConnection::HTTP_VERB_GET, strObject, NULL, 1, NULL, NULL, INTERNET_FLAG_RELOAD | INTERNET_FLAG_DONT_CACHE);
//Envoi de la requéte
BOOL bRequestSend = pHttpFile->SendRequest(strHeaders);
CString headers;headers.Empty();
DWORD dwRet;
pHttpFile->QueryInfo(HTTP_QUERY_RAW_HEADERS_CRLF,headers);
pHttpFile->QueryInfoStatusCode(dwRet);
//Lecture du résultat
while ( pHttpFile->ReadString(strTmp))
{
strResult += strTmp;
}
//Fermeture de la requéte
pHttpFile->Close();
//Fermeture de la connexion
pHttpConnexion->Close();
//Suppression des objets
if (pHttpFile != NULL)
delete pHttpFile;
if (pHttpConnexion != NULL)
delete pHttpConnexion;
}
catch(CInternetException* exp)
{
exp->Delete();
//Fermeture de la requéte
if (pHttpFile != NULL)
{
pHttpFile->Close();
delete pHttpFile;
}
//Fermeture de la connexion
if (pHttpConnexion != NULL)
{
pHttpConnexion->Close();
delete pHttpConnexion;
}
throw CTSException("sendRequest");
}
return strResult.GetString();
}
std::string ChttpConnexion::postRequest(const std::string& strUrl, const std::string& postData)
{
DWORD dwServiceType;
CString strServerName;
CString strObject;
INTERNET_PORT nPort;
AfxParseURL(strUrl.c_str(), dwServiceType, strServerName, strObject, nPort);
CString strHeaders = _T("User-Agent: User-Agent=Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8\r\nAccept-Language: fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7\r\nConnection: keep-alive\r\nContent-Type: application/x-www-form-urlencoded");
CString strTmp = "", strResult = "";
CHttpConnection* pHttpConnexion = NULL;
CHttpFile* pHttpFile = NULL;
try
{
//Creation de la connexion Http
pHttpConnexion = GetHttpConnection(strServerName, INTERNET_FLAG_RELOAD | INTERNET_FLAG_DONT_CACHE, nPort, NULL, NULL);
//Creation de la requete GET
pHttpFile = pHttpConnexion->OpenRequest(CHttpConnection::HTTP_VERB_POST, strObject, NULL, 1, NULL, NULL, INTERNET_FLAG_RELOAD | INTERNET_FLAG_DONT_CACHE);
//Envoi de la requéte
BOOL bRequestSend = pHttpFile->SendRequest(strHeaders, (LPVOID) (LPCTSTR) postData.c_str(), postData.length());
CString headers;headers.Empty();
DWORD dwRet;
pHttpFile->QueryInfo(HTTP_QUERY_RAW_HEADERS_CRLF,headers);
pHttpFile->QueryInfoStatusCode(dwRet);
CString data;
GetCookie(strServerName, "sess_id", data);
//Lecture du résultat
while ( pHttpFile->ReadString(strTmp))
{
strResult += strTmp;
}
//Fermeture de la requéte
pHttpFile->Close();
//Fermeture de la connexion
pHttpConnexion->Close();
//Suppression des objets
if (pHttpFile != NULL)
delete pHttpFile;
if (pHttpConnexion != NULL)
delete pHttpConnexion;
}
catch(CInternetException* exp)
{
exp->Delete();
//Fermeture de la requéte
if (pHttpFile != NULL)
{
pHttpFile->Close();
delete pHttpFile;
}
//Fermeture de la connexion
if (pHttpConnexion != NULL)
{
pHttpConnexion->Close();
delete pHttpConnexion;
}
throw CTSException("postRequest");
}
return strResult.GetString();
}
感谢您的帮助!
答案 0 :(得分:0)
CInternetSession
构造函数时,为什么第3个参数为NULL?它应该是PRE_CONFIG_INTERNET_ACCESS
之类的东西。如果您在代理服务器后面,则可能需要使用代理设置。strServerName
的内容,可能无效,GetHttpConnection
因此失败。try
,GetHttpConnection
和OpenRequest
,但您不会检查结果是否为空。如果它们不是NULL以便删除它们,您只能稍后检查(太晚)。在使用GetHttpConnection
和OpenRequest
之前,您应该先检查它们。 (但我猜这个例外发生在GetHttpConnection
)ChttpConnexion
后,您是否在其上调用了.Close()
?最后,我建议附加一个调试器并在catch中放置一个断点,以查看错误发生的确切位置,或者添加一些调试日志。这可能会有所帮助。