[Winhttp]_[C/C++]_[使用win32 SDK的Windows HTTP Services(WinHTTP)通过代理下载网页]
2012-12-19 15:56
666 查看
场景:
1.使用win32 SDK的话编译出来的程序比第3方库更小更容易控制。
2.缺点就是不能跨平台,在linux上边又需要重新写一套实现。
3.优点就是可以对win32 SDK使用更熟悉。
程序没怎么整理,有时间再弄。
1.使用win32 SDK的话编译出来的程序比第3方库更小更容易控制。
2.缺点就是不能跨平台,在linux上边又需要重新写一套实现。
3.优点就是可以对win32 SDK使用更熟悉。
程序没怎么整理,有时间再弄。
// test_http.cpp : 定义控制台应用程序的入口点。 // #include <stdio.h> #include "windows.h" #include "Winhttp.h" void WinDownload() { DWORD dwSize = 0; DWORD dwDownloaded = 0; LPSTR pszOutBuffer; BOOL bResults = FALSE; HINTERNET hSession = NULL, hConnect = NULL, hRequest = NULL; // Use WinHttpOpen to obtain a session handle. hSession = WinHttpOpen( L"WinHTTP Example/1.0", WINHTTP_ACCESS_TYPE_NAMED_PROXY, L"192.168.0.1:808", L"192.168.0.1:808", 0 ); //hSession = WinHttpOpen( L"WinHTTP Example/1.0", // WINHTTP_ACCESS_TYPE_NO_PROXY, // WINHTTP_NO_PROXY_NAME, // WINHTTP_NO_PROXY_BYPASS, 0 ); // Specify an HTTP server. if( hSession ) hConnect = WinHttpConnect( hSession, L"www.w3school.com.cn", INTERNET_DEFAULT_HTTP_PORT, 0 ); // Create an HTTP request handle. if( hConnect ) hRequest = WinHttpOpenRequest( hConnect, L"GET", L"/example/xmle/note.xml", NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_FLAG_REFRESH ); // 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 ); } int main(int argc, char* argv[]) { WinDownload(); return 0; }
相关文章推荐
- [Winhttp]_[C/C++]_[使用win32 SDK的Windows HTTP Services(WinHTTP)通过代理下载网页]
- 在MFC中使用WinInnet通过http从读取网页信息
- windows winhttp的编程例子,但是头文件和库文件,我的机子环境上没有,只有下载了头文件和库文件后才可以使用。
- Windows下使用C++(Win32SDK)编程无需提权读取硬盘序列号(XP、Win7和Win10都可用)
- 在windows下使用git通过代理下载项目
- Windows 和 Linux下使用socket下载网页页面内容(可设置接收/发送超时)的代码(用socket解释http,不错)
- iOS使用协议代理封装系统下载HttpRequest
- 面向对象的Windows编程实战(上)(使用C++和Win32 API)
- 使用c++技术实现下载网页
- C++使用POST方法向网页提交数据-----C++发送HTTP数据获取Google天气预报
- Windows游戏设计(三)- 黑白棋游戏 - 使用Win32 SDK
- HttpWebRequest 下载网页Html代码 POST方式(站内使用了form方式) System.Net.WebException (417) Expectation failed
- C++ http get下载网页源码
- Windows 和 Linux下使用socket下载网页页面内容(可设置接收/发送超时)的代码
- 使用python在win下通过IE组件获得Ajax执行后网页源代码
- python使用re, os, httplib, urllib批量下载网页上的图片
- C++通过http下载二进制文件
- nodejs下载器,通过chrome代理下载http资源
- (二)使用apache的commons-httpclient-3.1.jar之通过Java实现发起HTTP请求【使用代理】
- 使用WinInet方式访问网页与使用WinHttp对象方式访问有什么不同