Guest User

httptest

a guest
Apr 10th, 2014
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.83 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <windows.h>
  3. #include <winhttp.h>
  4. #pragma comment(lib, "winhttp.lib")
  5.  
  6. LPCTSTR userAgent = _T("Mozilla/5.0 (Windows NT 6.2; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0");
  7. LPCTSTR server = _T("www.google.com");
  8.  
  9. int _tmain(int argc, _TCHAR* argv[])
  10. {
  11.   DWORD dwSize = 0;
  12.   DWORD dwDownloaded = 0;
  13.   LPSTR pszOutBuffer;
  14.   BOOL  bResults = FALSE;
  15.   HINTERNET  hSession = NULL,
  16.              hConnect = NULL,
  17.              hRequest = NULL;
  18.  
  19.   getchar();
  20.   // Use WinHttpOpen to obtain a session handle.
  21.   hSession = WinHttpOpen( userAgent,  
  22.                           WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
  23.                           WINHTTP_NO_PROXY_NAME,
  24.                           WINHTTP_NO_PROXY_BYPASS, 0 );
  25.  
  26.   // Specify an HTTP server.
  27.   if( hSession )
  28.     hConnect = WinHttpConnect( hSession, server,
  29.                                INTERNET_DEFAULT_HTTPS_PORT, 0 );
  30.  
  31.   // Create an HTTP request handle.
  32.   if( hConnect )
  33.     hRequest = WinHttpOpenRequest( hConnect, L"GET", NULL,
  34.                                    NULL, WINHTTP_NO_REFERER,
  35.                                    WINHTTP_DEFAULT_ACCEPT_TYPES,
  36.                                    WINHTTP_FLAG_SECURE );
  37.  
  38.   // Send a request.
  39.   if( hRequest )
  40.     bResults = WinHttpSendRequest( hRequest,
  41.                                    WINHTTP_NO_ADDITIONAL_HEADERS, 0,
  42.                                    WINHTTP_NO_REQUEST_DATA, 0,
  43.                                    0, 0 );
  44.  
  45.  
  46.   // End the request.
  47.   if( bResults )
  48.     bResults = WinHttpReceiveResponse( hRequest, NULL );
  49.  
  50.   // Keep checking for data until there is nothing left.
  51.   if( bResults )
  52.   {
  53.     do
  54.     {
  55.       // Check for available data.
  56.       dwSize = 0;
  57.       if( !WinHttpQueryDataAvailable( hRequest, &dwSize ) )
  58.         printf( "Error %u in WinHttpQueryDataAvailable.\n",
  59.                 GetLastError( ) );
  60.  
  61.       // Allocate space for the buffer.
  62.       pszOutBuffer = new char[dwSize+1];
  63.       if( !pszOutBuffer )
  64.       {
  65.         printf( "Out of memory\n" );
  66.         dwSize=0;
  67.       }
  68.       else
  69.       {
  70.         // Read the data.
  71.         ZeroMemory( pszOutBuffer, dwSize+1 );
  72.  
  73.         if( !WinHttpReadData( hRequest, (LPVOID)pszOutBuffer,
  74.                               dwSize, &dwDownloaded ) )
  75.           printf( "Error %u in WinHttpReadData.\n", GetLastError( ) );
  76.         else
  77.           printf( "%s", pszOutBuffer );
  78.  
  79.         // Free the memory allocated to the buffer.
  80.         delete [] pszOutBuffer;
  81.       }
  82.     } while( dwSize > 0 );
  83.   }
  84.  
  85.  
  86.   // Report any errors.
  87.   if( !bResults )
  88.     printf( "Error %d has occurred.\n", GetLastError( ) );
  89.  
  90.   // Close any open handles.
  91.   if( hRequest ) WinHttpCloseHandle( hRequest );
  92.   if( hConnect ) WinHttpCloseHandle( hConnect );
  93.   if( hSession ) WinHttpCloseHandle( hSession );
  94.  
  95.   getchar();
  96.     return 0;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment