- //向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);