Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <windows.h>
- #include <winhttp.h>
- #pragma comment(lib, "winhttp.lib")
- LPCTSTR userAgent = _T("Mozilla/5.0 (Windows NT 6.2; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0");
- LPCTSTR server = _T("www.google.com");
- int _tmain(int argc, _TCHAR* argv[])
- {
- DWORD dwSize = 0;
- DWORD dwDownloaded = 0;
- LPSTR pszOutBuffer;
- BOOL bResults = FALSE;
- HINTERNET hSession = NULL,
- hConnect = NULL,
- hRequest = NULL;
- getchar();
- // Use WinHttpOpen to obtain a session handle.
- hSession = WinHttpOpen( userAgent,
- WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
- WINHTTP_NO_PROXY_NAME,
- WINHTTP_NO_PROXY_BYPASS, 0 );
- // Specify an HTTP server.
- if( hSession )
- hConnect = WinHttpConnect( hSession, server,
- INTERNET_DEFAULT_HTTPS_PORT, 0 );
- // Create an HTTP request handle.
- if( hConnect )
- hRequest = WinHttpOpenRequest( hConnect, L"GET", NULL,
- NULL, WINHTTP_NO_REFERER,
- WINHTTP_DEFAULT_ACCEPT_TYPES,
- WINHTTP_FLAG_SECURE );
- // Send a request.
- if( hRequest )
- bResults = WinHttpSendRequest( hRequest,
- WINHTTP_NO_ADDITIONAL_HEADERS, 0,
- WINHTTP_NO_REQUEST_DATA, 0,
- 0, 0 );
- // End the request.
- if( bResults )
- bResults = WinHttpReceiveResponse( hRequest, NULL );
- // Keep checking for data until there is nothing left.
- if( bResults )
- {
- do
- {
- // Check for available data.
- dwSize = 0;
- if( !WinHttpQueryDataAvailable( hRequest, &dwSize ) )
- printf( "Error %u in WinHttpQueryDataAvailable.\n",
- GetLastError( ) );
- // Allocate space for the buffer.
- pszOutBuffer = new char[dwSize+1];
- if( !pszOutBuffer )
- {
- printf( "Out of memory\n" );
- dwSize=0;
- }
- else
- {
- // Read the data.
- ZeroMemory( pszOutBuffer, dwSize+1 );
- if( !WinHttpReadData( hRequest, (LPVOID)pszOutBuffer,
- dwSize, &dwDownloaded ) )
- printf( "Error %u in WinHttpReadData.\n", GetLastError( ) );
- else
- printf( "%s", pszOutBuffer );
- // Free the memory allocated to the buffer.
- delete [] pszOutBuffer;
- }
- } while( dwSize > 0 );
- }
- // Report any errors.
- if( !bResults )
- printf( "Error %d has occurred.\n", GetLastError( ) );
- // Close any open handles.
- if( hRequest ) WinHttpCloseHandle( hRequest );
- if( hConnect ) WinHttpCloseHandle( hConnect );
- if( hSession ) WinHttpCloseHandle( hSession );
- getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment