C Cinternetsession连接丢失
C++ CInternetSession connexion lost
本文关键字:连接 Cinternetsession 更新时间:2023-10-16
我使用 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()
?
最后,我建议连接一个调试器,并在捕获量中放置一个断点,以查看错误的确切位置,或添加一些调试日志。这可能会有所帮助。
相关文章:
- 当套接字连接断开时检测C/C++Unix
- 无法在windows上使用mingw将sqlite3与c连接
- 到连接组件算法的问题(递归)
- QTcpSocket在不阻塞GUI的情况下重新连接到服务器
- 无法在C++中建立与MySQL数据库的连接
- PC中的程序和PHONE中的本机描述应用程序之间的数据连接
- 在Qt Creator中,如何在连接到正在运行的进程后查看控制台输出
- 连接 dockerized 模型和 dockerized 数据库时出现"无法 SQLConnect"错误
- 使用 bfs 解决连接组件问题时得到错误的答案
- 在C++的两个字符串中连接以逗号分隔的唯一值
- 如何修复valgrind启动时的致命错误(与libc6-dbg和libc6-dbg:i386连接)
- C++应用程序 MySQL odbc 数据库连接错误:在引发"otl_tmpl_exception<>"实例后终止调用
- OSX蓝牙打开RFCOMMChannelAsync声称已连接,但未建立连接,并且从未调用过委托
- 在 libcurl 连接池中预创建连接
- 套接字连接"Operation not permitted"错误,甚至使用升压/平发器根.cpp
- libcurl 和 DNS ttl 中的内部连接管理
- 如何应用 libcurl 的持久连接选项
- 提升 ASIO - io_service 不要等待连接到线程
- WinSock2:connect() 提供"连接被拒绝"
- 升压信号2将插槽传递到成员功能以断开连接