HttpSendRequest sends data to the server and constructs the http header of the request.

Source: Internet
Author: User

For more information, see this article: Use the WinInte API to send HTTP requests, but it was found to be incorrect in my tests.

 
 
  1. // Send data to http: // 192.168.8.72: 8080/oss/client/analysis. g.
  2. LPCTSTR lpURL = _ T ("http: // 192.168.8.72: 8080 ");
  3. If (! : InternetCheckConnection (lpURL, FLAG_ICC_FORCE_CONNECTION, 0 ))
  4. Return;
  5. HINTERNET hOpen =: InternetOpen (_ T ("client.exe"), INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY, NULL, NULL, 0 );
  6. If (NULL = hOpen)
  7. Return;
  8. LPCTSTR lpDomainName = _ T ("192.168.8.72"); // do not include http ://
  9. // The 3rd parameters of the function cannot be 80, but 8080
  10. HINTERNET hConnect =: InternetConnect (hOpen, lpDomainName, 8080, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0 );
  11. If (NULL = hConnect)
  12. Goto FUN_END2;
  13. // 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
  14. LPCTSTR szAccept [] = {_ T ("*/*"), NULL };
  15. DWORD dwFlag = INTERNET_FLAG_NO_CACHE_WRITE;
  16. HINTERNET hOpenRequest =: HttpOpenRequest (hConnect, _ T ("POST"), _ T ("/oss/client/analysis. g "),
  17. _ T ("HTTP/1.1"), lpURL, szAccept, dwFlag, 0 );
  18. If (NULL = hOpenRequest)
  19. Goto FUN_END1;
  20. BOOL bRet;
  21. TCHAR szPostData [] = _ T ("<Root> this is test data from client </Root> ");
  22. Char utf8PostData [BUF_LEN] = {0 };
  23. Strcpy (utf8PostData, dataConvert. TCharToUTF8 (szPostData ));
  24. // Outbound header.
  25. // Note that the header here is prone to errors.
  26. // 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 ".
  27. // 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,
  28. // The status code returned by HttpQueryInfo is always 400, which indicates "Incorrect request-the request has a syntax problem or cannot meet the request ".
  29. // 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.
  30. TCHAR headerLanguage [] = _ T ("Accept-Language: zh-CN, zh; q = 0.8 ");
  31. TCHAR headerEncoding [] = _ T ("Accept-Encoding: gzip, deflate, sdch ";);
  32. TCHAR headerCharset [] = _ T ("Accept-Charset: GBK, UTF-8; q = 0.7, *; q = 0.3 ");
  33. TCHAR headerContentType [] = _ T ("Content-Type: text/xml ";);
  34. TCHAR headerHost [] = _ T ("Host: 192.168.8.72: 8080 ";);
  35. TCHAR headerOrigin [] = _ T ("Origin: http: // 192.168.8.72: 8080 ";);
  36. TCHAR headerReferer [] = _ T ("Referer: http: // 192.168.8.72: 8080/oss/client/create. g ");
  37. TCHAR headerContentLength [64];
  38. _ Stprintf (headerContentLength, _ T ("Content-Length: % d \ r \ n"), strlen (utf8PostData )); // note that there are two \ r \ n at the end.
  39. // Add header information
  40. BRet = HttpAddRequestHeaders (hOpenRequest, headerLanguage,-1, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE );
  41. BRet = HttpAddRequestHeaders (hOpenRequest, headerEncoding,-1, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE );
  42. BRet = HttpAddRequestHeaders (hOpenRequest, headerCharset,-1, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE );
  43. BRet = HttpAddRequestHeaders (hOpenRequest, headerContentType,-1, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE );
  44. BRet = HttpAddRequestHeaders (hOpenRequest, headerHost,-1, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE );
  45. BRet = HttpAddRequestHeaders (hOpenRequest, headerOrigin,-1, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE );
  46. BRet = HttpAddRequestHeaders (hOpenRequest, headerReferer,-1, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE );
  47. BRet = HttpAddRequestHeaders (hOpenRequest, headerContentLength,-1, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE );
  48. BRet =: HttpSendRequest (hOpenRequest, NULL, 0, utf8PostData, strlen (utf8PostData ));
  49. DWORD dwErr =: GetLastError ();
  50. If (! BRet) goto FUN_END1;
  51. TCHAR szBuff [BUF_LEN] = {0 };
  52. DWORD dwBuffSize = BUF_LEN * sizeof (szBuff );
  53. BRet =: HttpQueryInfo (hOpenRequest, HTTP_QUERY_STATUS_CODE, (LPVOID) szBuff, & dwBuffSize, NULL );
  54. // Reference to http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
  55. Int nStatusCode = _ tstoi (szBuff );
  56. If (nStatusCode <200 | 206 <nStatusCode)
  57. BRet = FALSE;
  58. If (bRet)
  59. {
  60. TCHAR szBuff [BUF_LEN_1024 + 1] = {0 };
  61. DWORD dwBuffSize = BUF_LEN_1024 * sizeof (szBuff );
  62. BRet =: HttpQueryInfo (hOpenRequest, HTTP_QUERY_CONTENT_LENGTH, (LPVOID) szBuff, & dwBuffSize, NULL );
  63. Memset (szBuff, 0, sizeof (szBuff ));
  64. DwBuffSize = BUF_LEN_1024 * sizeof (szBuff );
  65. BRet =: HttpQueryInfo (hOpenRequest, HTTP_QUERY_DATE, (LPVOID) szBuff, & dwBuffSize, NULL );
  66. Memset (szBuff, 0, sizeof (szBuff ));
  67. DwBuffSize = BUF_LEN_1024 * sizeof (szBuff );
  68. BRet =: HttpQueryInfo (hOpenRequest, HTTP_QUERY_SERVER, (LPVOID) szBuff, & dwBuffSize, NULL );
  69. Memset (szBuff, 0, sizeof (szBuff ));
  70. DwBuffSize = BUF_LEN_1024 * sizeof (szBuff );
  71. BRet =: InternetReadFile (hOpenRequest, szBuff, dwBuffSize, & dwBuffSize );
  72. If (bRet)
  73. {
  74. //...
  75. }
  76. }
  77. InternetCloseHandle (hOpenRequest );
  78.  
  79. FUN_END1:
  80. : InternetCloseHandle (hConnect );
  81. FUN_END2:
  82. : 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.