summaryrefslogtreecommitdiff
path: root/tests/winhttp/test1.cpp
blob: 39cbbe48aa9811bf2ad344139b0de48738c1ab19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#define WIN32_MEAN_AND_LEAN
#include <windows.h>
#include <WinHttp.h>

#include <iostream>
#include <string>

using namespace std;

int main( void )
{
	HINTERNET session = WinHttpOpen( L"WinHTTP crawler/0.0.1",
		WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
		WINHTTP_NO_PROXY_NAME,
		WINHTTP_NO_PROXY_BYPASS, 0 );
	if( !session ) {
		cerr << "WinHttpOpen failed: " << GetLastError( ) << endl;
		return 1;
	}
	
	HINTERNET connect = WinHttpConnect( session, L"www.andreasbaumann.cc",
		INTERNET_DEFAULT_HTTP_PORT, 0 );
	if( !connect ) {
		cerr << "WinHttpConnect failed: " << GetLastError( ) << endl;
		WinHttpCloseHandle( session );
		return 1;
	}
	
	HINTERNET request = WinHttpOpenRequest( connect, L"GET", L"/index.shtml",
		NULL, WINHTTP_NO_REFERER, NULL, NULL );
	if( !request ) {
		cerr << "WinHttpOpenRequest failed: " << GetLastError( ) << endl;
		WinHttpCloseHandle( request );
		WinHttpCloseHandle( session );
		return 1;
	}
	
	if( !WinHttpSendRequest( request, WINHTTP_NO_ADDITIONAL_HEADERS,
			0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0 ) ) {
		cerr << "WinHttpSendRequest failed: " << GetLastError( ) << endl;
		WinHttpCloseHandle( connect );
		WinHttpCloseHandle( request );
		WinHttpCloseHandle( session );
		return 1;
	}
	
	if( !WinHttpReceiveResponse( request, NULL ) ) {
		cerr << "WinHttpReceiveResponse failed: " << GetLastError( ) << endl;
		WinHttpCloseHandle( connect );
		WinHttpCloseHandle( request );
		WinHttpCloseHandle( session );
		return 1;
	}
	
	DWORD size;
	string content;
	size_t pos = 0;
	do {
		size = 0;
		if( !WinHttpQueryDataAvailable( request, &size ) ) {
			cerr << "WinHttpQueryDataAvailable failed: " << GetLastError( ) << endl;
			WinHttpCloseHandle( connect );
			WinHttpCloseHandle( request );
			WinHttpCloseHandle( session );
			return 1;
		}
		
		LPSTR buf = new char[size+1];
		if( !buf ) {
			cerr << "Out of memory allocating receive buffer" << endl;
			WinHttpCloseHandle( connect );
			WinHttpCloseHandle( request );
			WinHttpCloseHandle( session );
			return 1;
		}
		ZeroMemory( buf, size+1 );
		
		DWORD received;
		if( !WinHttpReadData( request, (LPVOID)buf, size, &received ) ) {
			cerr << "WinHttpReadData failed: " << GetLastError( ) << endl;
			WinHttpCloseHandle( connect );
			WinHttpCloseHandle( request );
			WinHttpCloseHandle( session );
			return 1;
		}
		
		content.insert( pos, buf, received );	
		pos += received;
		
		delete buf;
	} while( size > 0 );
	
	cout << "Received bytes: " << content.size( ) << endl;
	cout << content << endl;
	
	WinHttpCloseHandle( request );
	WinHttpCloseHandle( connect );
	WinHttpCloseHandle( session );
	
	return 0;
}