C++互联网络连接丢失

C++互联网络连接丢失,c++,timeout,C++,Timeout,我使用CInternetSession获取和发布请求。但是当连接超时时,我失去了连接,并且总是出现无效的服务器请求错误,我不明白为什么。此外,还存在内存泄漏 #include "stdafx.h" #include "httpConnexion.h" #include "TSException.h" ChttpConnexion::ChttpConnexion() : CInternetSession(AfxGetAppName(), INTERNET_OPEN_TYPE_DIRECT

我使用CInternetSession获取和发布请求。但是当连接超时时,我失去了连接,并且总是出现无效的服务器请求错误,我不明白为什么。此外,还存在内存泄漏

#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();
}
谢谢你的帮助

构造对象并调用CInternetSession构造函数时,为什么第三个参数为NULL?它应该类似于预配置互联网访问。如果您在代理后面,则可能需要使用代理设置。 检查strServerName的内容,它可能无效,GetHttpConnection因此失败。 在您的尝试中,您获取HttpConnection和OpenRequest,但不检查结果是否为NULL。如果它们不为NULL,那么您只会在晚些时候进行检查以删除它们。在使用它们之前,应该在GetHttpConnection和OpenRequest之后检查它们。但我猜异常发生在GetHttpConnection期间 如果超时发生在10秒之后,可能是因为构造函数中的超时太低。 有一些旧报告称https可能存在问题。但是你没有提到你的要求和你的要求。 在您的请求头中,您正在请求不同的文件格式xml、html、png等,但您正在使用GetString并将其附加到字符串中。如果您得到的是二进制数据,它可能无法工作。您应该使用char[]缓冲区。 使用对象ChttpConnexion后,是否调用。是否关闭?
最后,我建议附加一个调试器并在catch中放置一个断点,以查看错误发生的确切位置,或者添加一些调试日志。这可能会有所帮助。

正如它试图告诉您的那样,这里有很多代码,请将其缩减到错误发生的位置。我不知道错误发生的位置。。。我得到一个请求超时,它跳转到catch语句中。在那之后,不要再等了properly@Nagasaki我看不出有什么问题,但看起来您是在要求我们为您调试程序;您作为一名成员已经有足够长的时间了,知道如果您这样做而不显示您以前的努力,社区将如何反应我的问题是,为什么当连接超时发生时,当我发送新请求时,我会收到服务器响应错误。我不能告诉更多,如果我知道答案,我不会要求它。。。我不需要调试,我的代码在超时之前运行良好。谢谢你的回答。我将以这种方式修改我的代码。我已经将超时修改为60秒。我请求的url是https协议。我只要求html文件。在使用对象ChttpConnexion之后,我只是删除了它。我没有获得300奖金,你知道为什么吗?