Home > Software engineering >  MFC through CHttpClient how to Post more parameters
MFC through CHttpClient how to Post more parameters

Time:09-18

Had been through CHttpClient ExecuteRequest function of a class to invoke the webapi related interface, to the Post way and also is a basic parameter is given priority to, recently due to the need of project Post two parameters (one is a string type, the other is a json type) to the server, helpless, request a great god giving directions, thanked first! CHttpClient class is as follows:

//the header file
The class CHttpClient
{
Public:
CHttpClient (LPCTSTR strAgent=IE_AGENT);
Virtual ~ CHttpClient (void);

Int HttpGet (LPCTSTR strUrl, LPCTSTR strPostData, string & amp; StrResponse);
Int HttpPost (LPCTSTR strUrl, LPCTSTR strPostData, string & amp; StrResponse);

Private:
Int ExecuteRequest (LPCTSTR strMethod, LPCTSTR strUrl, LPCTSTR strPostData, string & amp; StrResponse);
Void the Clear ();

Private:
CInternetSession * m_pSession;
CHttpConnection * m_pConnection;
CHttpFile * m_pFile;
};

//the source file
Int CHttpClient: : ExecuteRequest (LPCTSTR strMethod, LPCTSTR strUrl, LPCTSTR strPostData, string & amp; StrResponse)
{
Cstrings strServer.
Cstrings strObject;
DWORD dwServiceType.
INTERNET_PORT nPort.
StrResponse="";
Char * psText;

AfxParseURL (strUrl, dwServiceType strServer strObject, nPort);

If (AFX_INET_SERVICE_HTTP!=dwServiceType & amp; & AFX_INET_SERVICE_HTTPS!=dwServiceType)
{
Return FAILURE;
}

Try
{
M_pConnection=m_pSession - & gt; GetHttpConnection (strServer,
DwServiceType==AFX_INET_SERVICE_HTTP? NORMAL_CONNECT: SECURE_CONNECT
, nPort, _T (" "), _T (" "));
M_pFile=m_pConnection - & gt; OpenRequest (strMethod strObject, NULL, 1, NULL, NULL,
(dwServiceType==AFX_INET_SERVICE_HTTP? NORMAL_REQUEST: SECURE_REQUEST));

DWORD dwFlags;
M_pFile - & gt; QueryOption (INTERNET_OPTION_SECURITY_FLAGS dwFlags);
DwFlags |=SECURITY_FLAG_IGNORE_UNKNOWN_CA;
//set the web server option
M_pFile - & gt; SetOption (INTERNET_OPTION_SECURITY_FLAGS dwFlags);
M_pFile - & gt; SetOption (INTERNET_OPTION_SEND_TIMEOUT PDLG - & gt; M_stAPP. NHttpSendTimeOut);
M_pFile - & gt; SetOption (INTERNET_OPTION_DATA_SEND_TIMEOUT PDLG - & gt; M_stAPP. NHttpSendTimeOut);
M_pFile - & gt; SetOption (INTERNET_OPTION_RECEIVE_TIMEOUT PDLG - & gt; M_stAPP. NHttpSendTimeOut);
M_pFile - & gt; SetOption (INTERNET_OPTION_DATA_RECEIVE_TIMEOUT PDLG - & gt; M_stAPP. NHttpSendTimeOut);

M_pFile - & gt; AddRequestHeaders (_T (" Accept: * and */* "));
M_pFile - & gt; AddRequestHeaders (_T (" Accept - Language: useful - cn "));
M_pFile - & gt; AddRequestHeaders (_T (" content-type: application/json "));
M_pFile - & gt; AddRequestHeaders (_T (" Accept - Encoding: gzip, deflate "));

DWORD dwNum=WideCharToMultiByte (strPostData CP_UTF8, NULL, 1, NULL, 0, NULL, FALSE);
PsText=new char [dwNum];
if(! PsText)
{
The delete [] psText;
Return FAILURE;
}
WideCharToMultiByte (strPostData CP_UTF8, NULL, 1, psText, dwNum, NULL, FALSE);
//m_pFile - & gt; The SendRequest (NULL, 0, (LPVOID) (LPCTSTR) strPostData, strPostData NULL=https://bbs.csdn.net/topics/=? 0: _tcslen (strPostData));
M_pFile - & gt; The SendRequest (NULL, 0, (LPVOID) psText, strPostData NULL=https://bbs.csdn.net/topics/=? 0: dwNum);
The delete [] psText;

Char szChars [BUFFER_SIZE + 1)={0};
Char buf [BUFFER_SIZE];
Memset (buf, 0, BUFFER_SIZE);
UINT nLen=0;
String strRawResponse="";
UINT nReaded=0;
While ((nReaded=m_pFile - & gt; Read ((void *) szChars, BUFFER_SIZE)) & gt; 0)
{
SzChars [nReaded]='\ 0';
StrRawResponse +=szChars;
Memcpy (& amp; Buf [nLen], szChars nReaded);
NLen +=nReaded;
Memset (szChars, 0, BUFFER_SIZE + 1);
}

Int unicodeLen=MultiByteToWideChar (CP_UTF8, 0, strRawResponse c_str (), 1, NULL, 0).
WCHAR * pUnicode=new WCHAR [unicodeLen + 1];
(unicodeLen memset (pUnicode, 0, + 1) * sizeof (would));

MultiByteToWideChar (CP_UTF8, 0, strRawResponse c_str (), 1, pUnicode, unicodeLen);
Cstrings cs (pUnicode);
The delete [] pUnicode;
PUnicode=NULL;

//strResponse=cs;
USES_CONVERSION;
String STR (W2A (cs));
StrResponse=STR;

The Clear ();
}
return SUCCESS;
}

CodePudding user response:

Top for yourself!

CodePudding user response:

Post regardless of several parameters are not through & amp; The combination of?

CodePudding user response:


Take a look at the status of the browser's address bar

CodePudding user response:

https://blog.csdn.net/edwin_/article/details/76738193


CodePudding user response:

Ding!!!!!
  • Related