使用 WinInte API 发送 HTTP 请求
-
-
- 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");
-
- HINTERNET hConnect = ::InternetConnect(hOpen, lpDomainName, 8080, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
- if (NULL == hConnect)
- goto FUN_END2;
-
-
- 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));
-
-
-
-
-
-
-
-
- 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));
-
-
- 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);
-
-
- 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);