HttpSendRequest向服務端傳送資料,構造請求http頭
可以參考這篇文章: 使用 WinInte API 傳送 HTTP 請求,不過在我的測試中發現是錯誤的.
- //向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"); // 注意不能帶 http://
- // 該函式第3個引數不能是80,而應該是8080
- HINTERNET hConnect = ::InternetConnect(hOpen, lpDomainName, 8080, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
- if (NULL == hConnect)
- goto FUN_END2;
- // 注意第3個引數的個數,最前面要有"/",它同lpURL拼接成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));
- // 外發的header.
- // 注意,這裡的頭部很容易錯.
- // 每個字串結尾不能帶諸如 "/r/n","\r\n"這樣的結尾,最後一個字串必須要帶兩個"\r\n",也就是"\r\n\r\n".
- // 有些網友寫的示例中,每個字串後都帶有諸如 "/r/n"或"\r\n"這樣的結尾,但在我的測試中發現是錯誤的,
- // HttpQueryInfo返回的狀態碼總是400,意思是"錯誤請求 — 請求中有語法問題,或不能滿足請求",估計這跟
- // 具體的web服務有關.但有個簡單的方式處理該問題,可使用chrome瀏覽器訪問某個url(這裡是http://192.168.8.72:8080/oss/client/// analysis.g),然後使用抓包工具Wireshark抓取http包,分析request和response頭.然後再使用你的程式請求你的url,一樣抓包,然後對// 比這兩次抓取的資料包中http頭有什麼區別,修改你的程式碼,不斷嘗試,直到成功.
- 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\r\n"), strlen(utf8PostData)); //注意結尾有兩個\r\n
- // 新增header 資訊
- 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);
本文轉自jetyi51CTO部落格,原文連結:http://blog.51cto.com/jetyi/1019642 ,如需轉載請自行聯絡原作者
相關文章
- HttpSendRequest向服務端傳送資料,構造請求http頭薦HTTP服務端
- 前端傳送的請求,是如何請求到後端服務的?前端後端
- SSRF 服務端請求偽造服務端
- perl傳送http請求HTTP
- java傳送http請求JavaHTTP
- 使用httpclient傳送http請求HTTPclient
- 使用Feign傳送HTTP請求HTTP
- python傳送HTTP POST請求PythonHTTP
- .net 後臺 傳送http請求HTTP
- 使用requests庫來傳送HTTP請求HTTP
- nodejs使用request傳送http請求NodeJSHTTP
- java傳送http的get、post請求JavaHTTP
- 【轉】怎麼用PHP傳送HTTP請求(POST請求、GET請求)?PHPHTTP
- java傳送post請求 ,請求資料放到body裡Java
- http請求頭HTTP
- Go使用net/http庫傳送GET請求GoHTTP
- Go HTTP GET 請求可以傳送 body 嗎GoHTTP
- PHP 傳送 HTTP 請求的幾種方式PHPHTTP
- Windows Phone 8.1 傳送http 網路請求。WindowsHTTP
- php curl_multi批量傳送http請求PHPHTTP
- 從客戶端向服務端發起請求(3種)客戶端服務端
- jmeter 傳送 UDP 請求,怎麼接收伺服器返回的 TCP 服務資料JMeterUDP伺服器TCP
- react-fetch資料傳送請求React
- 理解ASP.NET Core - 傳送Http請求(HttpClient)ASP.NETHTTPclient
- wireshark抓包curl傳送http2請求HTTP
- 使用 request 和 cheerio 庫來傳送 HTTP 請求HTTP
- oracle使用utl_http包傳送post請求OracleHTTP
- 使用Python獲取HTTP請求頭資料PythonHTTP
- 二、傳送請求,獲取響應資料
- 在Java中,使用HttpUtils實現傳送HTTP請求JavaHTTP
- Jmeter —— jmeter利用取樣器中http傳送請求JMeterHTTP
- HTTP請求頭與響應頭HTTP
- jmeter之傳送json資料的post請求JMeterJSON
- Java用HttpClient3傳送http/https協議get/post請求,傳送map,json,xml,txt資料JavaHTTPclient協議JSONXML
- Http請求資料格式HTTP
- Postman傳送Post請求Postman
- Java傳送Post請求Java
- 傳送GET請求 示例