Home > Software engineering >  MFC how to implement the HTTP request send documents in question
MFC how to implement the HTTP request send documents in question

Time:10-09

Recently, using MFC to achieve a wifi upgrade vendor provide upgrades is web page, I want to integrate into the MFC Cheng Xun, in imitation of the HTTP request, meet send update file, found that is not the same as before the get and post (pictured) , how to implement the HTTP request? Prequest payload information exactly should be added? Because I don't understand HTTP and the underlying communication, so please feel free to comment... Grateful...

CodePudding user response:

Can see the curl library

CodePudding user response:

http://blog.csdn.net/a956602523/article/details/50956530 under reference

CodePudding user response:

The file into the content of the post

CodePudding user response:

Upload files the content-type using multipart/form - the data format, usually post generally don't use this,
You can see the HTTP protocol,

http://blog.csdn.net/xiaojianpitt/article/details/6856536

But if there are special provisions on both sides, can not use this format, before I write my own httpserver is useless in this format,

CodePudding user response:

The prequest content value location in the registry is HKEY_CURRENT_USER \ Software \ Microsoft \ Windows \ CurrentVersion Internet Settings \ \ User Agent,
 
HRESULT PostRequestDataToAppServer (LPWSTR lpwServerUrl,
Char * pszRequestData,
BYTE * * ppbyResponseData,
DWORD & amp; DwResponseDataSize,
DWORD dwTimeOut)
{
TRACE_FN_SCOPE;

HRESULT HRESULT=E_FAIL;
BOOL bRetCode=FALSE;
LPWSTR lpwUserAgent=NULL;
STD: : wstring wstrHostName=L "";
STD: : wstring wstrObjectName=L "";
DWORD dwServerPort=80;
BOOL bSSLConnect=FALSE;
HINTERNET HINTERNET=NULL;
HINTERNET hConnect=NULL;
HINTERNET hRequest=NULL;

DWORD dwSecureFlags=INTERNET_FLAG_RELOAD;
DWORD dwQueryFlags=0;
DWORD dwFlagsSize=sizeof (dwQueryFlags);
WCHAR wszStatusCode [16]={0};
DWORD dwStatusCodeSize=_countof (wszStatusCode);
LPBYTE lpReturnBuffer=NULL;
LPBYTE lpReturnBufferNewStart=NULL;
DWORD dwNumberOfBytesAvailable=0;
DWORD dwBytesOfRead=0;

WCHAR * wszAccept []={L "application/json," L "text/javascript," L "*/*", NULL};
Struct
{
Would be * ContentType;
} Headers={L "content-type: application/x - WWW - form - urlencoded"/* L "content-type: application/json; Charset=utf-8 "*/};

ERROR_ESCAPE (lpwServerUrl==NULL | | pszRequestData=0, https://bbs.csdn.net/topics/=ERROR_INVALID_PARAMETER, L "Check the Parameter");//write a macro, which means that the first parameter is TRUE, then jump to goto, FALSE, then continue to go down

HResult=ReadRegistryData (HKEY_CURRENT_USER,
REG_USERAGENT_SUBKEY,
REG_USERAGENT_VALUENAME,
(LPVOID *) & amp; LpwUserAgent,
NULL);//read the registry key value, the function code not list one by one, I mean understand become
ERROR_ESCAPE (hResult!=S_OK hResult, L "ReadRegistryData");

HResult=ParseNetworkURL (lpwServerUrl,
WstrHostName,
WstrObjectName,
DwServerPort,
BSSLConnect);//URL apart, the Internet at the beginning of the functions are ready, I'm still sealed a
ERROR_ESCAPE (hResult!=S_OK hResult, L "ParseNetworkURL");

If (bSSLConnect==TRUE)
{
DwSecureFlags=INTERNET_FLAG_SECURE |
INTERNET_FLAG_RELOAD |
SECURITY_INTERNET_MASK |
SECURITY_IGNORE_ERROR_MASK;
}
The else
{
DwSecureFlags=INTERNET_FLAG_RELOAD;
}

HInternet=: : InternetOpenW (lpwUserAgent,
INTERNET_OPEN_TYPE_DIRECT,
NULL,
NULL,
0);
ERROR_ESCAPE (hInternet==NULL, 0, L "InternetOpenW");

HConnect=: : InternetConnectW (hInternet,
WstrHostName. C_str (),
(INTERNET_PORT) dwServerPort,
NULL,
NULL,
INTERNET_SERVICE_HTTP,
0,
0);
ERROR_ESCAPE (hConnect==NULL, 0, L "InternetConnect");

If (dwTimeOut!=INFINITE)
{
BRetCode=: : InternetSetOptionW (hConnect,
INTERNET_OPTION_CONNECT_TIMEOUT,
(LPVOID) & amp; DwTimeOut,
Sizeof (dwTimeOut));
TRACE (INFO, L "InternetSetOptionW result: % d", bRetCode);
}

HRequest=: : HttpOpenRequestW (hConnect,
L "POST",
WstrObjectName. C_str (),
NULL,
NULL,
(LPCWSTR *) wszAccept,
DwSecureFlags,
0);
ERROR_ESCAPE (hRequest==NULL, 0, L "HttpOpenRequest");

If (bSSLConnect==TRUE)
{
BRetCode=: : InternetQueryOptionW (hRequest,
INTERNET_OPTION_SECURITY_FLAGS,
(LPVOID) & amp; DwQueryFlags,
& DwFlagsSize);
TRACE (INFO, L "InternetQueryOptionW result: % d", bRetCode);

DwQueryFlags |=SECURITY_FLAG_IGNORE_REVOCATION;
BRetCode=: : InternetSetOptionW (hRequest,
INTERNET_OPTION_SECURITY_FLAGS,
(LPVOID) & amp; DwQueryFlags,
Sizeof (dwQueryFlags));
TRACE (INFO, L "InternetSetOptionW result: % d", bRetCode);
}

BRetCode=: : HttpAddRequestHeadersW (hRequest,
Headers. ContentType,
Wcslen (Headers. ContentType),
HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE);
ERROR_ESCAPE (bRetCode==FALSE, 0, L "HttpAddRequestHeaders");

BRetCode=: : HttpSendRequestW (hRequest,
NULL,
1,
LPVOID pszRequestData,
Strlen (pszRequestData));
ERROR_ESCAPE (bRetCode==FALSE, 0, L "HttpSendRequest");

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related