Internet API 使用代理
来源:互联网 发布:传统装修公司 知乎 编辑:程序博客网 时间:2024/05/20 23:56
1. 相关函数:
- HINTERNET WINAPI InternetOpen(
- LPCTSTR lpszAgent,
- DWORD dwAccessType,
- LPCTSTR lpszProxy,
- LPCTSTR lpszProxyBypass,
- DWORD dwFlags);
- BOOL WINAPI InternetSetOption(
- HINTERNET hInternet,
- DWORD dwOption,
- LPVOID lpBuffer,
- DWORD dwBufferLength);
2. 相关结构
- typedef struct {
- DWORD dwAccessType;
- LPCTSTR lpszProxy;
- LPCTSTR lpszProxyBypass;
- } INTERNET_PROXY_INFO, *LPINTERNET_PROXY_INFO;
3. 使用代理服务器
(1)请将dwAccessType设置成INTERNET_OPEN_TYPE_PROXY
(2)设置lpszProxy
(a)代理的格式必须为:[<protocol>=][<scheme>://]<proxy>[:<port>].
(b)其中protocol, scheme://, :port是可选项, 如果忽略这三者, 则它们默认分别为
HTTP, HTTP://, :80. 即默认为HTTP代理.
(c)多个代理必须使用" "(空格)隔开
(d)各种常用代理的使用见如下:
HTTP:
HTTP=HTTP://proxyserver:port
FTP:
FTP:FTP://proxyserver:port
GOPHER
GOPHER=HTTP://proxyserver:port
SOCKS=proxyserver:port
其中前三种都可以在msdn中找到, 但第四种我可是找了N多地方才好不容易找到了. 另外要注意, msdn中明确说明只有安装了IE才能使用SOCKS代理.
么区分SOCK4/SOCK5代理? SOCK5代理一般还需要身份认证的.
- /*The following example code shows how authentication
- could be handled using InternetSetOption. */
- HINTERNET hOpenHandle, hResourceHandle;
- DWORD dwError, dwStatus;
- DWORD dwStatusSize = sizeof(dwStatus);
- char strUsername[64], strPassword[64];
- hOpenHandle = InternetOpen("Example",
- INTERNET_OPEN_TYPE_PRECONFIG,
- NULL, NULL, 0);
- hConnectHandle = InternetConnect(hOpenHandle,
- "www.server.com",
- INTERNET_INVALID_PORT_NUMBER,
- NULL,
- NULL,
- INTERNET_SERVICE_HTTP,
- 0,0);
- hResourceHandle = HttpOpenRequest(hConnectHandle, "GET",
- "/premium/default.htm", NULL, NULL,
- NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
- resend:
- HttpSendRequest(hResourceHandle, NULL, 0, NULL, 0);
- HttpQueryInfo(hResourceHandle, HTTP_QUERY_FLAG_NUMBER ¦
- HTTP_QUERY_STATUS_CODE, &dwStatus, &dwStatusSize, NULL);
- switch (dwStatus)
- {
- case HTTP_STATUS_PROXY_AUTH_REQ: // Proxy Authentication Required
- // Insert code to set strUsername and strPassword.
- // cchUserLength is the length of strUsername and
- // cchPasswordLength is the length of strPassword.
- DWORD cchUserLength, cchPasswordLength;
- // Insert code to safely determine cchUserLength and
- // cchPasswordLength. Insert appropriate error handling code.
- InternetSetOption(hResourceHandle,
- INTERNET_OPTION_PROXY_USERNAME,
- strUsername,
- cchUserLength+1);
- InternetSetOption(hResourceHandle,
- INTERNET_OPTION_PROXY_PASSWORD,
- strPassword,
- cchPasswordLength+1);
- goto resend;
- break;
- case HTTP_STATUS_DENIED: // Server Authentication Required.
- // Insert code to set strUsername and strPassword.
- // cchUserLength is the length of strUsername and
- // cchPasswordLength is the length of strPassword.
- DWORD cchUserLength, cchPasswordLength;
- // Insert code to safely determine cchUserLength and
- // cchPasswordLength. Insert error handling code as
- // appropriate.
- InternetSetOption(hResourceHandle, INTERNET_OPTION_USERNAME,
- strUsername, cchUserLength+1);
- InternetSetOption(hResourceHandle, INTERNET_OPTION_PASSWORD,
- strPassword, cchPasswordLength+1);
- goto resend;
- break;
- }
- // Insert code to read the data from the hResourceHandle
- // at this point.
这段代码就是设置代理的用户名和密码的呀, 其中INTERNET_OPTION_USERNAME设置代理服务器的用户名, INTERNET_OPTION_PASSWORD设置相应的密码.
删繁就简, 如果只是纯粹的设置用户名和密码, 只需要下面的代码就可以了.
InternetSetOption(hResourceHandle, INTERNET_OPTION_USERNAME,
strUsername, cchUserLength+1);
InternetSetOption(hResourceHandle, INTERNET_OPTION_PASSWORD,
strPassword, cchPasswordLength+1);
其中strUsername设置用户名, strPassword指定密码.
详细说明请见msdn:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wininet/wininet/handling_authentication.asp
http://support.microsoft.com/kb/226473/zh-cn
- Internet API 使用代理
- 使用Win32 Internet API的下载文件
- 寻找win32 internet api
- JAVA动态代理--使用JAVA API实现JAVA动态代理
- Internet API实现FTP断点续传
- JDK核心API:使用动态代理实现AOP功能
- 使用动态代理API-给抽象数据类型提供强类型
- angular2的angular-cli如何使用代理api
- Spring+CXF配置HTTP代理访问Internet
- VC通过代理访问internet例子
- bat internet中局域网设置-代理
- INTERNET编程之INTERNET简单使用HTTP
- 代理模式(Proxy Pattern)(三):HeadFirst使用java API创建一个保护代理
- 动态代理-->jdk-api
- nodejs 设置API代理
- UIScrollViewDelegate-代理API详解
- 使用.NET访问 Internet(1)
- 使用.NET访问 Internet(2)
- 添加到购物车
- Foxmail客户端设置outlook邮箱
- CMake与Make
- jprofiler入门配置
- python实用小代码
- Internet API 使用代理
- GCD介绍(一): 基本概念和Dispatch Queue
- android学习二:适配器Adapter的使用
- Linux内核的时钟中断机制
- 如何组织大型JavaScript应用中的代码?(arvin 推荐)
- mfc 基于formview的sdi 打开文档后 窗口变小的问题
- 端口被占用的处理方法
- WPF做界面经典教程
- AIDL的一点理解