Home > Software engineering >  Using MFC to wininet closed CHttpConnection and related classes, HTTP access, in a visit to fail on
Using MFC to wininet closed CHttpConnection and related classes, HTTP access, in a visit to fail on

Time:09-25

The code below
BOOL CHttpTransfer: : Post (LPCTSTR szRemoteURI, const char * sendBuf, int sendLen, ReceiveData& RevData, const CString&=ContentType_TEXT_PLAN strContentType/* */)
{
Assert (NULL!=szRemoteURI);
BOOL bResult=FALSE;
DWORD dwType=0;
Cstrings sServer=_T (" ");
Cstrings sObject=_T (" ");
INTERNET_PORT wPort=0;
DWORD dwPostSize=0;
CHttpConnection * pHttpConn=NULL;
CHttpFile * pHttpFile=NULL;
CInternetSession cis (_T (" "));

BResult=FALSE;
Try
{
BResult=AfxParseURL (szRemoteURI dwType, sServer, sObject, wPort);
if(! BResult)
return FALSE;

Cis. SetOption (INTERNET_OPTION_CONNECT_TIMEOUT, 5000);
Cis. SetOption (INTERNET_OPTION_CONNECT_RETRIES, 1);
PHttpConn=cis. GetHttpConnection (sServer wPort, NULL, NULL);
Assert (NULL!=pHttpConn);
PHttpFile=pHttpConn - & gt; OpenRequest (CHttpConnection: : HTTP_VERB_POST, sObject);
Assert (NULL!=pHttpFile);

//to HTTP headers
Cstrings strTemp;
Cstrings strHeaders=_T (" content-type: ") + strContentType;//application/x - WWW - form - urlencoded//multipart/form - data text/plan
StrHeaders +=_T (" \ r \ nAccept - Language: useful - cn ");
StrHeaders +=_T (" \ r \ nAccept - Charset: utf-8 ");
//strHeaders +=_T (" \ r \ nReferer: ");
//strHeaders +=szRemoteURI;
Set related parameters request *//*
//pHttpFile - & gt; AddRequestHeaders (L "Accept: */*, application/json");//the accept request header fields, represent the client accept what types of information
//pHttpFile - & gt; AddRequestHeaders (L "Accept - Charset: UTF8");
//pHttpFile - & gt; AddRequestHeaders (L "Accept - Language: useful - cn; Q=0.8, en. Q=0.6, ja. Q=0.4 ");
//pHttpFile - & gt; AddRequestHeaders (L "content-type:") + strContentType;//application/x - WWW - form - urlencoded//multipart/form - data text/plan
StrHeaders +=_T (" \ r \ nAccept - Language: useful - cn ");
StrHeaders +=_T (" \ r \ nAccept - Charset: utf-8 ");
BResult=FALSE;
If (sendLen==0)
BResult=pHttpFile - & gt; The SendRequest (strHeaders, _T (" 1 "), 1);//in order to guarantee the data returned is correct,
The else
BResult=pHttpFile - & gt; The SendRequest (strHeaders, (LPVOID) sendBuf, sendLen);
If (bResult)
BResult=HttpReceive (pHttpFile revData);
}
The catch (CFileException * e)
{
E - & gt; The Delete ();
//REPORT_CACHED_EXCEPTION (_T (" CFileException "));
}
The catch (CInternetException * e)
{
Cstrings sError;
SError. The Format (_T (" Inernet connection error: % d "), e - & gt; M_dwError);

//used to return to the network abnormal information
TCHAR szError [64]={0};
E - & gt; GetErrorMessage (szError, 64);
RevData. ErrInfo=szError;
RevData. NErrorCode=e - & gt; M_dwError;

E - & gt; The Delete ();
}
If (pHttpFile)
{
PHttpFile - & gt; Close ();
The delete pHttpFile;
}
If (pHttpConn)
{
PHttpConn - & gt; Close ();
The delete pHttpConn;
}
Cis. The Close ();

Return bResult;
}

CodePudding user response:

Whether have to use the proxy server, etc.,

CodePudding user response:

To judge the return value of every function call?
Caught?
Close the UAC, firewall, anti-virus software,... ?

CodePudding user response:

Every and each

CodePudding user response:

. HTTP problem is much more, all kinds of factors,
Such as whether the ie agent
The crawler and the corresponding web page filtering machine, according to the HTTP headers, even cookies, all want to check,

CodePudding user response:

Try to reset the Winsocket
The command line to run the netsh winsock reset

CodePudding user response:

Solve the building Lord, share the experience
  • Related