I am a rookie, please give detailed solving steps, thank you!
Server interface method of exposure, as shown in the following
@ POST
@ Produces (" application/json; Charset=utf-8 ")
@ Path ("/heartbeat. Do ")
HandsetHeartbeatRes heartbeat (HandsetHeartbeatReq the req);
CodePudding user response:
header file (HttpFileClient. H)
# pragma once
# define __BUFFER_SIZE 1024
The class CHttpFile;
The class CHttpFileClient
{
Public:
CHttpFileClient (void);
~ CHttpFileClient (void);
Public:
BOOL UploadFile (LPCTSTR szRemoteURI, LPCTSTR szLocalPath);
BOOL DownLoadFile (LPCTSTR szRemoteURI, LPCTSTR szLocalPath);
BOOL DeleteFile (LPCTSTR szRemoteURI);
BOOL CanWebsiteVisit (cstrings thought);
Private:
BOOL UseHttpSendReqEx (CHttpFile * httpFile, LPCTSTR szLocalFile);
};
The source file (HttpFileClient. CPP)
# include "StdAfx. H"
# include "HttpFileClient. H"
#include
CHttpFileClient: : CHttpFileClient (void)
{
}
CHttpFileClient: : ~ CHttpFileClient (void)
{
}
BOOL CHttpFileClient: : CanWebsiteVisit (cstrings thought)
{
CHttpConnection * pHttpConn=NULL;
CHttpFile * pHttpFile=NULL;
CInternetSession cis;
BOOL bResult=FALSE;
BOOL bRetVal=FALSE;
DWORD dwType=0;
DWORD dwStateCode=0;
INTERNET_PORT wPort=0;
Cstrings sServer=_T (" ");
Cstrings sObject=_T (" ");
Const int nTimeOut=5000;
Try
{
BResult=AfxParseURL (thought, dwType sServer, sObject, wPort);
if (! BResult)
Return FALSE;
Cis. SetOption (INTERNET_OPTION_CONNECT_TIMEOUT nTimeOut);
Cis. SetOption (INTERNET_OPTION_CONNECT_RETRIES, 1);
PHttpConn=cis. GetHttpConnection (sServer wPort);
If (pHttpConn)
{
PHttpFile=pHttpConn - & gt; OpenRequest (CHttpConnection: : HTTP_VERB_GET, sObject);
If (pHttpFile - & gt; The SendRequest ())
{
PHttpFile - & gt; QueryInfoStatusCode (dwStateCode);
If (dwStateCode==HTTP_STATUS_CREATED | | dwStateCode==HTTP_STATUS_OK)
BRetVal=TRUE;
}
}
}
The catch (CInternetException * e)
{
E - & gt; The Delete ();
}
The catch (... )
{
}
If (pHttpFile)
{
PHttpFile - & gt; Close ();
The delete pHttpFile;
}
If (pHttpConn)
{
PHttpConn - & gt; Close ();
The delete pHttpConn;
}
Cis. The Close ();
Return bRetVal;
}
BOOL CHttpFileClient: : UploadFile (LPCTSTR szRemoteURI, LPCTSTR szLocalPath)
{
ENSURE_ARG_EX (NULL!=szRemoteURI & amp; & NULL!=szLocalPath);
BOOL bResult=FALSE;
DWORD dwType=0;
Cstrings sServer=_T (" ");
Cstrings sObject=_T (" ");
INTERNET_PORT wPort=0;
DWORD dwPostSize=0;
Const int nTimeOut=5000;
CHttpConnection * pHttpConn=NULL;
CHttpFile * pHttpFile=NULL;
CInternetSession cis;
BResult=AfxParseURL (szRemoteURI dwType, sServer, sObject, wPort);
if(! BResult)
Return FALSE;
BResult=FALSE;
Try
{
Cis. SetOption (INTERNET_OPTION_CONNECT_TIMEOUT nTimeOut);
Cis. SetOption (INTERNET_OPTION_CONNECT_RETRIES, 1);
PHttpConn=cis. GetHttpConnection (sServer wPort, NULL, NULL);
VERIFY_EX (NULL!=pHttpConn);
PHttpFile=pHttpConn - & gt; OpenRequest (CHttpConnection: : HTTP_VERB_PUT, sObject);
VERIFY_EX (NULL!=pHttpFile);
If (UseHttpSendReqEx (pHttpFile szLocalPath))
{
DWORD dwStateCode=0;
PHttpFile - & gt; QueryInfoStatusCode (dwStateCode);
If (dwStateCode==HTTP_STATUS_CREATED | | dwStateCode==HTTP_STATUS_OK)
BResult=TRUE;
}
}
The catch (CFileException * e)
{
E - & gt; The Delete ();
CFileException REPORT_CACHED_EXCEPTION (_T (" "));
}
The catch (CInternetException * e)
{
E - & gt; The Delete ();
Cstrings sError;
SError. The Format (_T (" Inernet connection error: % d "), e - & gt; M_dwError);
REPORT_CACHED_EXCEPTION (sError);
}
If (pHttpFile)
{
PHttpFile - & gt; Close ();
The delete pHttpFile;
}
If (pHttpConn)
{
PHttpConn - & gt; Close ();
The delete pHttpConn;
}
Cis. The Close ();
Return bResult;
}
BOOL CHttpFileClient: : DownLoadFile (LPCTSTR szRemoteURI, LPCTSTR szLocalPath)
{
ENSURE_ARG_EX (NULL!=szRemoteURI & amp; & NULL!=szLocalPath);
CInternetSession session;
CHttpConnection * pHttpConnection=NULL;
CHttpFile * pHttpFile=NULL;
Cstrings strServer=_T (" "), strObject=_T (" ");
INTERNET_PORT wPort=0;
DWORD dwType=0;
HANDLE hFile=NULL;
TCHAR pszBuffer [__BUFFER_SIZE];
DWORD dwFileSize=0;
Const int nTimeOut=2000;
The session. SetOption (INTERNET_OPTION_CONNECT_TIMEOUT nTimeOut);
The session. SetOption (INTERNET_OPTION_CONNECT_RETRIES, 1);
BOOL bResult=FALSE;
Try
{
AfxParseURL (szRemoteURI dwType, strServer strObject, wPort);
PHttpConnection=session. GetHttpConnection (strServer wPort, NULL, NULL);
VERIFY_EX (NULL! nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull