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