Advertisement
Guest User

Untitled

a guest
Oct 11th, 2013
2,038
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.97 KB | None | 0 0
  1. // pochttpclient.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "windows.h"
  6. #include "winhttp.h"
  7. #include "wchar.h"
  8.  
  9.  
  10.  
  11. // SSL (Secure Sockets Layer) example
  12. // compile for console
  13.  
  14. void main()
  15. {
  16.     HINTERNET hOpen = 0;
  17.     HINTERNET hConnect = 0;
  18.     HINTERNET hRequest = 0;
  19.     IStream *stream = NULL;
  20.     HRESULT hr;
  21.  
  22.     while (1)
  23.     {
  24.         hOpen = WinHttpOpen(L"Aurora Console App", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
  25.                 WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
  26.  
  27.         if (!hOpen) {
  28.             wprintf(L"WinHttpOpen failed (0x%.8X)\n", GetLastError());
  29.             break;
  30.         }
  31.  
  32.         hConnect = WinHttpConnect(hOpen, L"www.codeproject.com", INTERNET_DEFAULT_HTTPS_PORT, 0);
  33.  
  34.         if (!hConnect) {
  35.             wprintf(L"WinHttpConnect failed (0x%.8X)\n", GetLastError());
  36.             break;
  37.         }
  38.  
  39.         LPCWSTR types[2];
  40.         types[0] = L"text/html";
  41.         types[1] = 0;
  42.  
  43.         // use flag WINHTTP_FLAG_SECURE to initiate SSL
  44.         hRequest = WinHttpOpenRequest(hConnect, L"GET", L"KB/IP/NagTPortScanner.aspx",
  45.                 NULL, WINHTTP_NO_REFERER, types, WINHTTP_FLAG_SECURE);
  46.  
  47.         if (!hRequest)
  48.         {
  49.             wprintf(L"WinHttpOpenRequest failed (0x%.8X)\n", GetLastError());
  50.             break;
  51.         }
  52.  
  53.         if (!WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0))
  54.         {
  55.             wprintf(L"WinHttpSendRequest failed (0x%.8X)\n", GetLastError());
  56.             break;
  57.         }
  58.         if (!WinHttpReceiveResponse(hRequest, 0))
  59.         {
  60.             wprintf(L"WinHttpReceiveResponse failed (0x%.8X)\n", GetLastError());
  61.             break;
  62.         }
  63.         // query remote file size, set haveContentLength on success and dwContentLength to the length
  64.         wchar_t szContentLength[32];
  65.         DWORD cch = 64;
  66.         DWORD dwHeaderIndex = WINHTTP_NO_HEADER_INDEX;
  67.  
  68.         BOOL haveContentLength = WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_CONTENT_LENGTH, NULL,
  69.                 &szContentLength, &cch, &dwHeaderIndex);
  70.  
  71.         DWORD dwContentLength;
  72.         if (haveContentLength) dwContentLength = _wtoi(szContentLength);
  73.  
  74.         // read the response into memory stream
  75.         hr = CreateStreamOnHGlobal(0, true, &stream);
  76.         if (hr) {
  77.             wprintf(L"CreateStreamOnHGlobal failed (0x%.8X)\n", hr);
  78.             break;
  79.         }
  80.         // allocate buffer for streaming received data
  81.         unsigned char* p = new unsigned char[4096];
  82.         if (!p)
  83.         {
  84.             wprintf(L"failed to allocate buffer\n");
  85.             break;
  86.         }
  87.         // to receive all data, we need to enter a loop
  88.         DWORD dwReceivedTotal = 0;
  89.         while (WinHttpQueryDataAvailable(hRequest, &cch) && cch)
  90.         {
  91.             if (cch > 4096) cch = 4096;
  92.             dwReceivedTotal += cch;
  93.  
  94.             // display number of received bytes
  95.             if (haveContentLength)
  96.             {
  97.                 wprintf(L"received %d of %d (%d%%)%c", dwReceivedTotal, dwContentLength,
  98.                     dwReceivedTotal*100/dwContentLength, 13);
  99.             }
  100.             else {
  101.                 wprintf(L"received %d (unknown length)%c", dwReceivedTotal, 10);
  102.             }
  103.  
  104.             WinHttpReadData(hRequest, p, cch, &cch);
  105.             // write into stream
  106.             hr = stream->Write(p, cch, NULL);
  107.             if (hr)
  108.             {
  109.                 wprintf(L"failed to write data to stream (0x%.8X)\n", hr);
  110.             }
  111.         }
  112.  
  113.         delete p;
  114.         wprintf(L"\n\nreceived all data.\n");
  115.         // terminate the sream with a NULL
  116.         p = NULL;
  117.         stream->Write(&p, 1, NULL);
  118.         // get pointer to stream bytes
  119.         wprintf(L"getting HGLOBAL from stream...\n");
  120.         HGLOBAL hgl;
  121.         hr = GetHGlobalFromStream(stream, &hgl);
  122.         if (hr) {
  123.             wprintf(L"GetHGlobalFromStream failed (0x%.8X)\n", hr);
  124.             break;
  125.         }
  126.         wprintf(L"locking memory...\n");
  127.         p = (unsigned char*)GlobalLock(hgl);
  128.         if (!p)
  129.         {
  130.             wprintf(L"GlobalLock failed (0x%.8X)\n", GetLastError());
  131.             break;
  132.         }
  133.         wprintf(L"displaying received data...\n");
  134.         // terminate the string at 1024 bytes (MessageBox lag)
  135.         //if (dwReceivedTotal > 1024) dwReceivedTotal = 1024;
  136.         //*p[dwReceivedTotal] = 0;
  137.  
  138.         MessageBox(0, (LPCWSTR)p, L"", 0);
  139.         GlobalUnlock(hgl);
  140.  
  141.         break;
  142.     }
  143.     // delete stream and close handles
  144.     if (stream) stream->Release();
  145.     if (hRequest) WinHttpCloseHandle(hRequest);
  146.     if (hConnect) WinHttpCloseHandle(hConnect);
  147.     if (hOpen) WinHttpCloseHandle(hOpen);
  148.     system("pause");
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement