I use CInternetSession
to get and post request. But when a connexion
time out occurs, I lost the connexion
and I always get invalid server request error, I don't understand why. Moreover, there is also a memory leak.
#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(); }
Thanks for helping!
1 Answers
Answers 1
- when you construct your object and you call
CInternetSession
constructor, why the 3rd param is NULL? It should be something likePRE_CONFIG_INTERNET_ACCESS
. You may need to use the proxy settings if you are behind a proxy. - check the content of
strServerName
, it may be invalid andGetHttpConnection
fails because of that. - in your
try
, youGetHttpConnection
andOpenRequest
, but you don't check if the results aren't NULL. You only check later (too late) if they aren't NULL to delete them. You should check them afterGetHttpConnection
andOpenRequest
, before using them. (But I guess the exception occurs duringGetHttpConnection
) - if the timeout occurs after 10sec, maybe it's because your timeout is too low (in your constructor).
- there were some old reports that https may be a problem. But you didn't mentioned what was your request and what you were requesting.
- in your request header, you are requesting different file formats (xml, html, png, etc...), but you are using a GetString and appending it to a string. If you are getting a binary data, it may not work. You should use a char[] buffer. Check this example.
- after using your object
ChttpConnexion
, did you call.Close()
on it?
In the end, I would recommend attaching a debugger and put a breakpoint in the catch to see where exactly the error occurs, or add some debug logs. This may help.
0 comments:
Post a Comment