HttpSendRequest's demo

来源:互联网 发布:大学生防网络诈骗教育 编辑:程序博客网 时间:2024/06/07 06:02
/// @file testOptRemoteFile.cpp/// @brief HttpSendRequest's demo/// when i search HttpSendRequest happen 12007 error, i found below demo by MS/// @ref http://support.microsoft.com/kb/839873/zh-cn#include "stdafx.h"#include <Windows.h>#include <Wininet.h>#pragma comment(lib, "wininet.lib")using namespace std;#define BUFFER_SIZE1024int _tmain(int argc, _TCHAR* argv[]){DWORDdwRc = 0;HINTERNEThOpen = NULL;HINTERNEThConnect = NULL;HINTERNEThReq = NULL;hOpen = InternetOpenW(L"SimpleGet", INTERNET_OPEN_TYPE_PRECONFIG, NULL, 0, 0);if(!hOpen){dwRc = GetLastError();wprintf(L"failed : InternetOpenW rc = %d\r\n", dwRc);goto _tmain_END;}// try the following in InternetConnect to see the problem. hConnect = InternetConnectW(hOpen, L"www.microsoft.com/learning/",INTERNET_DEFAULT_HTTP_PORT,NULL,NULL,INTERNET_SERVICE_HTTP,0,0);if (!hConnect){dwRc = GetLastError();wprintf(L"failed : InternetConnect rc = %d\r\n", dwRc);goto _tmain_END;}hReq = HttpOpenRequestW(hConnect, L"GET", L"", L"HTTP/1.1", NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);if (!hReq){dwRc = GetLastError();wprintf(L"failed : HttpOpenRequestW rc = %d\r\n", dwRc);goto _tmain_END;}if (HttpSendRequest(hReq, NULL, 0, NULL, 0)){DWORDdwSize = 0;charData[BUFFER_SIZE] = {'\0'};DWORDdwCode = 0;DWORDdwCodeSize = 0;dwCodeSize = sizeof(DWORD);if (!HttpQueryInfo(hReq, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &dwCode, &dwCodeSize, NULL)){dwRc = GetLastError();wprintf(L"failed : HttpQueryInfo rc = %d\r\n", dwRc);goto _tmain_END;}else{wprintf(L"OK : HttpQueryInfo Status Code = %d\r\n", dwCode);}do{InternetReadFile(hReq, (LPVOID)Data, sizeof(Data) - 1, &dwSize);if (0!=dwSize){Data[dwSize] = '\0';/// Data mabye binary stream, not be a stringfor (int i = 0; i < dwSize; i++){if (0 == (i % 16))wprintf(L"\r\n");wprintf(L"%2.2x ", (BYTE)Data[i]);}wprintf(L"\r\n");}ZeroMemory(Data, sizeof(Data));}while (dwSize !=0);}else{dwRc = GetLastError();wprintf(L"failed : SendRequest error rc = %d\r\n", dwRc);}_tmain_END:wprintf(L"END, press any key to quit\r\n");getwchar();return 0;}/**run resultOK : HttpQueryInfo Status Code = 4003c 48 54 4d 4c 3e 3c 48 45 41 44 3e 0a 3c 54 4954 4c 45 3e 52 65 71 75 65 73 74 20 45 72 72 6f72 3c 2f 54 49 54 4c 45 3e 0a 3c 2f 48 45 41 443e 0a 3c 42 4f 44 59 3e 0a 3c 46 4f 4e 54 20 6661 63 65 3d 22 48 65 6c 76 65 74 69 63 61 22 3e0a 3c 62 69 67 3e 3c 73 74 72 6f 6e 67 3e 3c 2f73 74 72 6f 6e 67 3e 3c 2f 62 69 67 3e 3c 42 523e 0a 3c 2f 46 4f 4e 54 3e 0a 3c 62 6c 6f 63 6b71 75 6f 74 65 3e 0a 3c 54 41 42 4c 45 20 62 6f72 64 65 72 3d 30 20 63 65 6c 6c 50 61 64 64 696e 67 3d 31 20 77 69 64 74 68 3d 22 38 30 25 223e 0a 3c 54 52 3e 3c 54 44 3e 0a 3c 46 4f 4e 5420 66 61 63 65 3d 22 48 65 6c 76 65 74 69 63 6122 3e 0a 3c 62 69 67 3e 52 65 71 75 65 73 74 2045 72 72 6f 72 20 28 69 6e 76 61 6c 69 64 5f 7265 71 75 65 73 74 29 3c 2f 62 69 67 3e 0a 3c 4252 3e 0a 3c 42 52 3e 0a 3c 2f 46 4f 4e 54 3e 0a3c 2f 54 44 3e 3c 2f 54 52 3e 0a 3c 54 52 3e 3c54 44 3e 0a 3c 46 4f 4e 54 20 66 61 63 65 3d 2248 65 6c 76 65 74 69 63 61 22 3e 0a 59 6f 75 7220 72 65 71 75 65 73 74 20 63 6f 75 6c 64 20 6e6f 74 20 62 65 20 70 72 6f 63 65 73 73 65 64 2e20 52 65 71 75 65 73 74 20 63 6f 75 6c 64 20 6e6f 74 20 62 65 20 68 61 6e 64 6c 65 64 0a 3c 2f46 4f 4e 54 3e 0a 3c 2f 54 44 3e 3c 2f 54 52 3e0a 3c 54 52 3e 3c 54 44 3e 0a 3c 46 4f 4e 54 2066 61 63 65 3d 22 48 65 6c 76 65 74 69 63 61 223e 0a 54 68 69 73 20 63 6f 75 6c 64 20 62 65 2063 61 75 73 65 64 20 62 79 20 61 20 6d 69 73 636f 6e 66 69 67 75 72 61 74 69 6f 6e 2c 20 6f 7220 70 6f 73 73 69 62 6c 79 20 61 20 6d 61 6c 666f 72 6d 65 64 20 72 65 71 75 65 73 74 2e 0a 3c2f 46 4f 4e 54 3e 0a 3c 2f 54 44 3e 3c 2f 54 523e 0a 3c 54 52 3e 3c 54 44 3e 0a 3c 46 4f 4e 5420 66 61 63 65 3d 22 48 65 6c 76 65 74 69 63 6122 20 53 49 5a 45 3d 32 3e 0a 3c 42 52 3e 0a e5a6 82 e6 9c 89 e4 bb bb e4 bd 95 e7 96 91 e9 97ae ef bc 8c e8 af b7 e8 81 94 e7 b3 bb 49 54 2053 75 70 70 6f 72 74 3a 20 4c 79 6e 63 3a 49 5473 75 70 70 6f 72 74 20 20 e5 9c a8 e7 ba bf e694 af e6 8c 81 3a 20 68 74 74 70 3a 2f 2f 69 7473 75 70 70 6f 72 74 2e 6c 65 6e 6f 76 6f 2e 636f 6d 2f 20 20 20 e7 94 b5 e8 af 9d 3a 20 56 4f49 50 3a 20 36 30 31 2d 38 30 30 30 20 6f 72 2065 78 74 65 72 6e 61 6c 20 28 38 36 29 20 31 302d 35 38 38 36 2d 38 30 30 30 20 45 2d 6d 61 696c 20 3a 20 49 54 73 75 70 70 6f 72 74 40 6c 656e 6f 76 6f 2e 63 6f 6d 2e 0a 3c 2f 46 4f 4e 543e 0a 3c 2f 54 44 3e 3c 2f 54 52 3e 0a 3c 2f 5441 42 4c 45 3e 0a 3c 2f 62 6c 6f 63 6b 71 75 6f74 65 3e 0a 3c 2f 46 4f 4e 54 3e 0a 3c 2f 42 4f44 59 3e 3c 2f 48 54 4d 4c 3e 0aEND, press any key to quit*/

0 0
原创粉丝点击