For more information, see this article: Use the WinInte API to send HTTP requests, but it was found to be incorrect in my tests.
- // Send data to http: // 192.168.8.72: 8080/oss/client/analysis. g.
- LPCTSTR lpURL = _ T ("http: // 192.168.8.72: 8080 ");
- If (! : InternetCheckConnection (lpURL, FLAG_ICC_FORCE_CONNECTION, 0 ))
- Return;
- HINTERNET hOpen =: InternetOpen (_ T ("client.exe"), INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY, NULL, NULL, 0 );
- If (NULL = hOpen)
- Return;
- LPCTSTR lpDomainName = _ T ("192.168.8.72"); // do not include http ://
- // The 3rd parameters of the function cannot be 80, but 8080
- HINTERNET hConnect =: InternetConnect (hOpen, lpDomainName, 8080, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0 );
- If (NULL = hConnect)
- Goto FUN_END2;
- // Pay attention to the number of 3rd parameters, which must be preceded by "/". It is spliced with lpURL into http: // 192.168.8.72: 8080/oss/client/analysis. g
- LPCTSTR szAccept [] = {_ T ("*/*"), NULL };
- DWORD dwFlag = INTERNET_FLAG_NO_CACHE_WRITE;
- HINTERNET hOpenRequest =: HttpOpenRequest (hConnect, _ T ("POST"), _ T ("/oss/client/analysis. g "),
- _ T ("HTTP/1.1"), lpURL, szAccept, dwFlag, 0 );
- If (NULL = hOpenRequest)
- Goto FUN_END1;
- BOOL bRet;
- TCHAR szPostData [] = _ T ("<Root> this is test data from client </Root> ");
- Char utf8PostData [BUF_LEN] = {0 };
- Strcpy (utf8PostData, dataConvert. TCharToUTF8 (szPostData ));
- // Outbound header.
- // Note that the header here is prone to errors.
- // Each string cannot end with an end such as "/r/n" or "\ r \ n". The last string must contain two "\ r \ n ", that is, "\ r \ n ".
- // In some examples written by some netizens, each string is followed by an end such as "/r/n" or "\ r \ n, however, I found it wrong in my tests,
- // The status code returned by HttpQueryInfo is always 400, which indicates "Incorrect request-the request has a syntax problem or cannot meet the request ".
- // Specific web Services. however, there is a simple way to solve this problem. You can use the chrome browser to access a url (http: // 192.168.8.72: 8080/oss/client // analysis. g), and then use the packet capture tool Wireshark to capture the http packet and analyze the request and response headers. then, use your program to request your url, capture packets, modify your code, and try again, until successful.
- TCHAR headerLanguage [] = _ T ("Accept-Language: zh-CN, zh; q = 0.8 ");
- TCHAR headerEncoding [] = _ T ("Accept-Encoding: gzip, deflate, sdch ";);
- TCHAR headerCharset [] = _ T ("Accept-Charset: GBK, UTF-8; q = 0.7, *; q = 0.3 ");
- TCHAR headerContentType [] = _ T ("Content-Type: text/xml ";);
- TCHAR headerHost [] = _ T ("Host: 192.168.8.72: 8080 ";);
- TCHAR headerOrigin [] = _ T ("Origin: http: // 192.168.8.72: 8080 ";);
- TCHAR headerReferer [] = _ T ("Referer: http: // 192.168.8.72: 8080/oss/client/create. g ");
- TCHAR headerContentLength [64];
- _ Stprintf (headerContentLength, _ T ("Content-Length: % d \ r \ n"), strlen (utf8PostData )); // note that there are two \ r \ n at the end.
- // Add header information
- BRet = HttpAddRequestHeaders (hOpenRequest, headerLanguage,-1, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE );
- BRet = HttpAddRequestHeaders (hOpenRequest, headerEncoding,-1, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE );
- BRet = HttpAddRequestHeaders (hOpenRequest, headerCharset,-1, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE );
- BRet = HttpAddRequestHeaders (hOpenRequest, headerContentType,-1, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE );
- BRet = HttpAddRequestHeaders (hOpenRequest, headerHost,-1, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE );
- BRet = HttpAddRequestHeaders (hOpenRequest, headerOrigin,-1, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE );
- BRet = HttpAddRequestHeaders (hOpenRequest, headerReferer,-1, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE );
- BRet = HttpAddRequestHeaders (hOpenRequest, headerContentLength,-1, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE );
- BRet =: HttpSendRequest (hOpenRequest, NULL, 0, utf8PostData, strlen (utf8PostData ));
- DWORD dwErr =: GetLastError ();
- If (! BRet) goto FUN_END1;
- TCHAR szBuff [BUF_LEN] = {0 };
- DWORD dwBuffSize = BUF_LEN * sizeof (szBuff );
- BRet =: HttpQueryInfo (hOpenRequest, HTTP_QUERY_STATUS_CODE, (LPVOID) szBuff, & dwBuffSize, NULL );
- // Reference to http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
- Int nStatusCode = _ tstoi (szBuff );
- If (nStatusCode <200 | 206 <nStatusCode)
- BRet = FALSE;
- If (bRet)
- {
- TCHAR szBuff [BUF_LEN_1024 + 1] = {0 };
- DWORD dwBuffSize = BUF_LEN_1024 * sizeof (szBuff );
- BRet =: HttpQueryInfo (hOpenRequest, HTTP_QUERY_CONTENT_LENGTH, (LPVOID) szBuff, & dwBuffSize, NULL );
- Memset (szBuff, 0, sizeof (szBuff ));
- DwBuffSize = BUF_LEN_1024 * sizeof (szBuff );
- BRet =: HttpQueryInfo (hOpenRequest, HTTP_QUERY_DATE, (LPVOID) szBuff, & dwBuffSize, NULL );
- Memset (szBuff, 0, sizeof (szBuff ));
- DwBuffSize = BUF_LEN_1024 * sizeof (szBuff );
- BRet =: HttpQueryInfo (hOpenRequest, HTTP_QUERY_SERVER, (LPVOID) szBuff, & dwBuffSize, NULL );
- Memset (szBuff, 0, sizeof (szBuff ));
- DwBuffSize = BUF_LEN_1024 * sizeof (szBuff );
- BRet =: InternetReadFile (hOpenRequest, szBuff, dwBuffSize, & dwBuffSize );
- If (bRet)
- {
- //...
- }
- }
- InternetCloseHandle (hOpenRequest );
-
- FUN_END1:
- : InternetCloseHandle (hConnect );
- FUN_END2:
- : InternetCloseHandle (hOpen );
This article from the "zero one small building" blog, please be sure to keep this source http://jetyi.blog.51cto.com/1460128/1019642