可以參考這篇文章: 使用 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);
本文出自 “零一小築” 博客,請務必保留此出處http://jetyi.blog.51cto.com/1460128/1019642