我使用 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.36rnAccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8rnAccept-Language: fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7rnConnection: keep-alivernContent-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.36rnAccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8rnAccept-Language: fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7rnConnection: keep-alivernContent-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();
}
感谢您的帮助!
- 当您构造对象并调用
CInternetSession
构造函数时,为什么第三个参数为null?它应该像PRE_CONFIG_INTERNET_ACCESS
一样。如果您在代理后面,则可能需要使用代理设置。 - 检查
strServerName
的内容,它可能无效,因此GetHttpConnection
失败了。 - 在您的
try
中,您的GetHttpConnection
和OpenRequest
,但您不检查结果是否未null。您只会在以后检查(为时已晚(,如果它们没有删除它们以删除它们。您应该在使用GetHttpConnection
和OpenRequest
之后检查它们,然后再使用它们。(但是我想例外发生在GetHttpConnection
期间( - 如果超时发生在10秒之后,则可能是因为您的超时太低(在构造函数中(。
- 有一些旧的报道说HTTP可能是一个问题。但是您没有提及您的要求和您的要求。
- 在您的请求标题中,您请求不同的文件格式(XML,HTML,PNG等...(,但是您使用的是getString并将其附加到字符串上。如果您获得了二进制数据,则可能行不通。您应该使用char []缓冲区。检查此示例。
- 使用您的对象
ChttpConnexion
后,您是否在其上调用.Close()
?
最后,我建议连接一个调试器,并在捕获量中放置一个断点,以查看错误的确切位置,或添加一些调试日志。这可能会有所帮助。