summaryrefslogtreecommitdiff
path: root/tests/winhttp
diff options
context:
space:
mode:
authorAndreas Baumann <abaumann@yahoo.com>2012-08-12 12:27:31 +0200
committerAndreas Baumann <abaumann@yahoo.com>2012-08-12 12:27:31 +0200
commit5e46acf128f2a016bbc7dfb063ededa197795a7a (patch)
treeb4a6ec15b4bba6fae66c9581e8252a1996f5758b /tests/winhttp
parent9678c28677997dbd34b35b04463af350a31b17ac (diff)
downloadcrawler-5e46acf128f2a016bbc7dfb063ededa197795a7a.tar.gz
crawler-5e46acf128f2a016bbc7dfb063ededa197795a7a.tar.bz2
added a test for WinHttp, quite a nice web library native to Windows
Diffstat (limited to 'tests/winhttp')
-rwxr-xr-xtests/winhttp/Makefile.W3233
-rwxr-xr-xtests/winhttp/test1.cpp101
2 files changed, 134 insertions, 0 deletions
diff --git a/tests/winhttp/Makefile.W32 b/tests/winhttp/Makefile.W32
new file mode 100755
index 0000000..a77b105
--- /dev/null
+++ b/tests/winhttp/Makefile.W32
@@ -0,0 +1,33 @@
+TOPDIR = ..\..
+
+SUBDIRS =
+
+!INCLUDE $(TOPDIR)\makefiles\nmake\platform.mk
+
+INCLUDE_CXXFLAGS = \
+ /D_WIN32_WINNT=0x504
+
+INCLUDE_DIRS = \
+ /I.
+
+INCLUDE_LDFLAGS = \
+
+INCLUDE_LIBS = \
+ WinHttp.lib
+
+TEST_CPP_BINS = \
+ test1.exe
+
+OBJS =
+
+!INCLUDE $(TOPDIR)\makefiles\nmake\sub.mk
+
+test1.exe: test1.obj
+
+local_all:
+
+local_clean:
+
+local_distclean:
+
+local_test:
diff --git a/tests/winhttp/test1.cpp b/tests/winhttp/test1.cpp
new file mode 100755
index 0000000..e565cac
--- /dev/null
+++ b/tests/winhttp/test1.cpp
@@ -0,0 +1,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 CrawlingWolf/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;
+}