WinHttpRequest SetProxy 方法详解,设置代理服务器信息

WinHttpRequest SetProxy 方法详解,设置代理服务器信息。

IWinHttpRequest::SetProxy Method

The SetProxy method sets proxy server information.

语法:

HRESULT SetProxy(
    [in]            HTTPREQUEST_PROXY_SETTING ProxySetting,
    [in, optional]  VARIANT ProxyServer,
    [in, optional]  VARIANT BypassList
);

参数:

ProxySetting[in]

The flags that control this method. Can be one of the following values.

HTTPREQUEST_PROXYSETTING_DEFAULT

Default proxy setting. Equivalent to HTTPREQUEST_PROXYSETTING_PRECONFIG.

HTTPREQUEST_PROXYSETTING_PRECONFIG

Indicates that the proxy settings should be obtained from the registry. This assumes that Proxycfg.exe has been run. If Proxycfg.exe has not been run and HTTPREQUEST_PROXYSETTING_PRECONFIG is specified, then the behavior is equivalent to HTTPREQUEST_PROXYSETTING_DIRECT.

HTTPREQUEST_PROXYSETTING_DIRECT

Indicates that all HTTP and HTTPS servers should be accessed directly. Use this command if there is no proxy server.

HTTPREQUEST_PROXYSETTING_PROXY

When HTTPREQUEST_PROXYSETTING_PROXY is specified, varProxyServer should be set to a proxy server string and varBypassList should be set to a domain bypass list string. This proxy configuration applies only to the current instance of the WinHttpRequest object.

HTTPREQUEST_PROXYSETTING_DEFAULT = 0    '默认代理设置,相当于:HTTPREQUEST_PROXYSETTING_PRECONFIG
HTTPREQUEST_PROXYSETTING_PRECONFIG = 0  '从注册表中读取代理服务器设置
HTTPREQUEST_PROXYSETTING_DIRECT = 1     '所有 HTTP 和 HTTPS 服务器应该直接访问,如果没有代理服务器,则使用此命令
HTTPREQUEST_PROXYSETTING_PROXY = 2      '指定代理配置,这个代理配置只适用于WinHttpRequest对象的当前实例。

ProxyServer[in, optional]

Set to a proxy server string when ProxySetting equals HTTPREQUEST_PROXYSETTING_PROXY.

BypassList[in, optional]

Set to a domain bypass list string when ProxySetting equals HTTPREQUEST_PROXYSETTING_PROXY.

返回值

The return value is S_OK on success or an error value otherwise.

备注

Enables the calling application to specify use of default proxy information (configured by the proxy configuration tool) or to override Proxycfg.exe. This method must be called before calling the Send method. If this method is called after the Send method, it has no effect.

IWinHttpRequest passes these parameters to Microsoft Windows HTTP Services (WinHTTP).

Note For Windows XP and Windows 2000, see the Run-Time Requirements section of the WinHTTP Start Page.

示例

The following example shows how to set the proxy settings for a particular proxy server, open an HTTP connection, send an HTTP request, and read the response text. This example must be run from a command prompt. These proxy settings work only if you have a proxy server named "proxy_server" that uses port 80 and your computer can bypass the proxy server when the host name ends with ".microsoft.com".

#include <windows.h>
#include <stdio.h>
#include <objbase.h>

#include "httprequest.h"

#pragma comment(lib, "ole32.lib")
#pragma comment(lib, "oleaut32.lib")

// IID for IWinHttpRequest.
const IID IID_IWinHttpRequest =
{
  0x06f29373,
  0x5c5a,
  0x4b54,
  {0xb0, 0x25, 0x6e, 0xf1, 0xbf, 0x8a, 0xbf, 0x0e}
};

int main()
{
    // Variable for return value
    HRESULT    hr;

    // Initialize COM
    hr = CoInitialize( NULL );

    IWinHttpRequest *  pIWinHttpRequest = NULL;

    BSTR            bstrResponse = NULL;
    VARIANT         varFalse;
    VARIANT         varEmpty;
    VARIANT         varProxy;
    VARIANT         varUrl;

    CLSID           clsid;

    VariantInit(&varFalse);
    V_VT(&varFalse)   = VT_BOOL;
    V_BOOL(&varFalse) = VARIANT_FALSE;

    VariantInit(&varEmpty);
    V_VT(&varEmpty) = VT_ERROR;

    VariantInit(&varProxy);
    VariantInit(&varUrl);

    hr = CLSIDFromProgID(L"WinHttp.WinHttpRequest.5.1", &clsid);

    if (SUCCEEDED(hr))
    {
        hr = CoCreateInstance(clsid, NULL,
                              CLSCTX_INPROC_SERVER,
                              IID_IWinHttpRequest,
                              (void **)&pIWinHttpRequest);
    }
    if (SUCCEEDED(hr))
    {   // Specify proxy and URL.
                varProxy.vt = VT_BSTR;
                varProxy.bstrVal = SysAllocString(L"proxy_server:80");
                varUrl.vt = VT_BSTR;
                varUrl.bstrVal = SysAllocString(L"*.microsoft.com");
                hr = pIWinHttpRequest->SetProxy(HTTPREQUEST_PROXYSETTING_PROXY,
                                    varProxy, varUrl);
        }
    if (SUCCEEDED(hr))
    {   // Open WinHttpRequest.
            BSTR bstrMethod  = SysAllocString(L"GET");
                BSTR bstrUrl = SysAllocString(L"http://microsoft.com");
        hr = pIWinHttpRequest->Open(bstrMethod, bstrUrl, varFalse);
                SysFreeString(bstrMethod);
                SysFreeString(bstrUrl);
    }
    if (SUCCEEDED(hr))
    {   // Send Request.
        hr = pIWinHttpRequest->Send(varEmpty);
    }
    if (SUCCEEDED(hr))
    {   // Get Response text.
                hr = pIWinHttpRequest->get_ResponseText(&bstrResponse);
    }
    if (SUCCEEDED(hr))
    {   // Print the response to a console.
                wprintf(L"%.256s",bstrResponse);
    }

        // Release memory.
    if (pIWinHttpRequest)
        pIWinHttpRequest->Release();
        if (varProxy.bstrVal)
                SysFreeString(varProxy.bstrVal);
        if (varUrl.bstrVal)
                SysFreeString(varUrl.bstrVal);
    if (bstrResponse)
        SysFreeString(bstrResponse);

        CoUninitialize();
        return 0;
}

The following scripting example shows how to set the proxy settings for a particular proxy server, open an HTTP connection, send an HTTP request, and read the response text. These proxy settings work only if you have a proxy server named "proxy_server" that uses port 80 and your computer can bypass the proxy server when the host name ends with ".microsoft.com".

// HttpRequest SetCredentials flags.
HTTPREQUEST_PROXYSETTING_DEFAULT   = 0;
HTTPREQUEST_PROXYSETTING_PRECONFIG = 0;
HTTPREQUEST_PROXYSETTING_DIRECT    = 1;
HTTPREQUEST_PROXYSETTING_PROXY     = 2;

// Instantiate a WinHttpRequest object.
var WinHttpReq = new ActiveXObject("WinHttp.WinHttpRequest.5.1");

// Use proxy_server for all requests outside of
// the microsoft.com domain.
WinHttpReq.SetProxy( HTTPREQUEST_PROXYSETTING_PROXY,
                     "proxy_server:80",
                     "*.microsoft.com");

// Initialize an HTTP request.
WinHttpReq.Open("GET", "http://www.microsoft.com", false);

// Send the HTTP request.
WinHttpReq.Send();

// Display the response text.
WScript.Echo( WinHttpReq.ResponseText);

配置要求

Minimum supported client

Windows XP, Windows 2000 Professional with SP3

Minimum supported server

Windows Server 2003, Windows 2000 Server with SP3

Redistributable

WinHTTP 5.0 and Internet Explorer 5.01 or later on Windows XP and Windows 2000.

IDL

HttpRequest.idl

Library

Winhttp.lib

DLL

Winhttp.dll


注意下面这个代码是错误的,需要调整!!!

WinHttpRequest SetProxy 方法设置代理服务器实例(VB 6.0 ):

Private Sub Form_Load()
    Dim WinHttp  As Object
    Set WinHttp = CreateObject("WinHttp.WinHttpRequest.5.1")
    '---------------------------------------------------------------------------
    'HTTPREQUEST_PROXYSETTING_DEFAULT = 0                                        '默认代理设置,相当于:HTTPREQUEST_PROXYSETTING_PRECONFIG
    'HTTPREQUEST_PROXYSETTING_PRECONFIG = 0                                      '从注册表中读取代理服务器设置
    'HTTPREQUEST_PROXYSETTING_DIRECT = 1                                         '所有 HTTP 和 HTTPS 服务器应该直接访问,如果没有代理服务器,则使用此命令
    'HTTPREQUEST_PROXYSETTING_PROXY = 2                                          '指定代理配置,这个代理配置只适用于WinHttpRequest对象的当前实例。
    WinHttp.SetProxy 2, "103.3.47.35:8080"                                      ', "*.microsoft.com"
    'WinHttp.SetProxy HTTPREQUEST_PROXYSETTING_PROXY, "代理IP:端口", "[可以为空,值为目标地址域名]"
    '---------------------------------------------------------------------------
    WinHttp.Open "GET", "http://iframe.ip138.com/city.asp", False               'GET 或 POST, Url, False 同步方式;True 异步方式
    WinHttp.SetRequestHeader "Accept", "*/*"                                    '接受数据类型
    WinHttp.SetRequestHeader "Accept-Language", "zh-cn,zh"                      '用户系统语言
    WinHttp.SetRequestHeader "User-Agent", "Mozilla/6.0"                        '用户浏览器信息
    WinHttp.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded" '编码方式
    WinHttp.SetRequestHeader "Connection", "Close"                              'Close = 不保持连接,Keep-Alive = 保持连接(持久连接)
    WinHttp.Send
    MsgBox WinHttp.ResponseText
End Sub


上一篇: linux——cp、 通配符、mv命令实例
下一篇: wordpress调用当前分类的全部子分类
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: vb
相关日志:
评论: 0 | 引用: 0 | 查看次数: 143
发表评论
昵 称:
密 码: 游客发言不需要密码.
邮 箱: 邮件地址支持Gravatar头像,邮箱地址不会公开.
网 址: 输入网址便于回访.
内 容:
验证码:
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 开启 | [img]标签 关闭

 广告位

↑返回顶部↑