Cstrings aa=_T (" http://39.98.237.78/sycompany/machineLog/add? ChannelId=5454 ");//this can send
INT CFileMoniterDlg: : DownloadFile (const cstrings strUrl)
{
//check incoming two parameters
If (strUrl. IsEmpty ())
Return to 5;
Unsigned short nPort.//used to hold the target HTTP service port
Cstrings strServer strObject;//strServer used to save the server address, strObject used to save the file object name
DWORD dwServiceType dwRet;//dwServiceType used to hold the service type, dwRet submitted to hold the state number of the GET request return
//parsing urls, access to information
if (! AfxParseURL (strUrl, dwServiceType strServer strObject, nPort)) {
//resolution fail, the Url is not correct
return -1;
}
//create a network connection object, HTTP connection object pointer and used for the connection of HttpFile file object pointer, pay attention to delete
CInternetSession intsess;
CHttpFile * pHtFile=NULL;
CHttpConnection * pHtCon=NULL;
Intsess. SetOption (INTERNET_OPTION_CONNECT_TIMEOUT, 1000 * 20);//connection timeout
Intsess. SetOption (INTERNET_OPTION_CONNECT_BACKOFF, 1000);//the time interval between the two retries
Intsess. SetOption (INTERNET_OPTION_CONNECT_RETRIES, 1);//2 retries
Intsess. SetOption (INTERNET_OPTION_SEND_TIMEOUT, 6000);//send the request timeout
Intsess. SetOption (INTERNET_OPTION_RECEIVE_TIMEOUT, 6000);//to accept data timeout
Try {
//to establish network connection
PHtCon=intsess. GetHttpConnection (strServer, nPort);
If (pHtCon==NULL) {
//to establish network connection failure
Intsess. Close ();
The return - 2;
}
//a GET request
PHtFile=pHtCon - & gt; OpenRequest (CHttpConnection: : HTTP_VERB_POST, strObject);
If (pHtFile==NULL)
{
//a GET request failed
Intsess. Close ();
The delete pHtCon;
PHtCon=NULL;
The return - 3;
}
//submit request
PHtFile - & gt; The SendRequest ();
//get the state number of the server returns
PHtFile - & gt; QueryInfoStatusCode (dwRet);
If (dwRet!={HTTP_STATUS_OK)
//the server will not accept the request
Intsess. Close ();
The delete pHtCon; PHtCon=NULL;
The delete pHtFile; PHtFile=NULL;
The return - 4;
}
}
The catch (CInternetException * e) {
E - & gt; The Delete ();
Intsess. Close ();
The delete pHtCon; PHtCon=NULL;
The delete pHtFile; PHtFile=NULL;
The return - 2;
}
////get file size
//UINT nFileLen=(UINT) pHtFile - & gt; GetLength ();
//DWORD dwRead=1;//used to identify read how much for 1 to enter circulation
////to create buffer
//CHAR * szBuffer=new CHAR [nFileLen + 1];
//TRY {
////creates the file
//CFile PicFile (strSavePath CFile: : modeCreate | CFile: : modeWrite | CFile: : shareExclusive);
//while (dwRead> 0)
//{
////empty buffer
//memset (szBuffer, 0, (size_t) (nFileLen + 1));
////read into the buffer
//dwRead=pHtFile - & gt; Read (szBuffer nFileLen);
////written to the file
//PicFile. Write (szBuffer dwRead);
//}
////close the file
//PicFile. Close ();
//release the memory
//delete [] szBuffer;
The delete pHtFile;
The delete pHtCon;
//off the network connection
Intsess. Close ();
//CATCH (CFileException, e)
//{
////free memory
//delete [] szBuffer;
//delete pHtFile;
//delete pHtCon;
////off the network connection
//intsess. Close ();
//return - 7;//read/write file anomalies
//}
//END_CATCH
return 0;
}
Pray god to help the younger brother to take a look at how be to return a responsibility
CodePudding user response:
Http requests