CEF中使用代理的方法

您所在的位置:网站首页 乔丹最高得分 CEF中使用代理的方法

CEF中使用代理的方法

2023-05-26 20:51| 来源: 网络整理| 查看: 265

CEF3使用类似Google Chrome一样的方式,通过命令行参数传递代理配置。

--proxy-server=host:port

     Specify the HTTP/SOCKS4/SOCKS5 proxy server to use for requests. An individual proxy

     server is specified using the format:

       [://][:]

     Where is the protocol of the proxy server, and is one of:

       "http", "socks", "socks4", "socks5".

     If the is omitted, it defaults to "http". Also note that "socks" is equivalent to

     "socks5".

     Examples:

       --proxy-server="foopy:99"

           Use the HTTP proxy "foopy:99" to load all URLs.

       --proxy-server="socks://foobar:1080"

           Use the SOCKS v5 proxy "foobar:1080" to load all URLs.

       --proxy-server="sock4://foobar:1080"

           Use the SOCKS v4 proxy "foobar:1080" to load all URLs.

       --proxy-server="socks5://foobar:66"

           Use the SOCKS v5 proxy "foobar:66" to load all URLs.

     It is also possible to specify a separate proxy server for different URL types, by prefixing

     the proxy server specifier with a URL specifier:

     Example:

       --proxy-server="https=proxy1:80;http=socks4://baz:1080"

           Load https://* URLs using the HTTP proxy "proxy1:80". And load http://*

           URLs using the SOCKS v4 proxy "baz:1080".

--no-proxy-server

     Disables the proxy server.

--proxy-auto-detect

     Autodetect  proxy  configuration.

--proxy-pac-url=URL

     Specify proxy autoconfiguration URL.

如果代理请求授权,CefRequestHandler::GetAuthCredentials()回调会被调用。如果isProxy参数为true,则需要返回用户名和密码。

bool MyHandler::GetAuthCredentials(

   CefRefPtr browser,

   CefRefPtr frame,

   bool isProxy,

   const CefString& host,

   int port,

   const CefString& realm,

   const CefString& scheme,

   CefRefPtr callback) {

 if (isProxy) {

   // Provide credentials for the proxy server connection.

   callback->Continue("myuser", "mypass");

   return true;

 }

 return false;

}

CEF任意请求代理

准确说是2015年10月7日之后)的CEF加入另一种更加灵活的方式,即任意请求代理

这种方式的原理是在进行每次请求的时候CEF给应用一次机会让应用可以修改请求相关的参数。要实现这一点,我们需要自己的CefRequestHandler,然后重载OnBeforeBrowse和GetAuthCredentials(不一定需要)两个方法,定义如下:

```

class MyRequestHandler final : public CefRequestHandler {public:

    virtual bool OnBeforeBrowse(CefRefPtr browser,

                                CefRefPtr frame,

                                CefRefPtr request,                                bool is_redirect);    // 可选,根据是否需要认证

    virtual bool GetAuthCredentials(CefRefPtr browser,

                                    CefRefPtr frame,                                    bool isProxy,                                    const CefString& host,                                    int port,                                    const CefString& realm,                                    const CefString& scheme,

                                    CefRefPtr callback);private:    

    IMPLEMENT_REFCOUNTING(MyRequestHandler);

};

```

```

bool MyRequestHandler::OnBeforeBrowse(

    CefRefPtr browser,

    CefRefPtr frame,

    CefRefPtr request,

    bool is_redirect) {

    CefRefPtr context =

        browser->GetHost()->GetRequestContext();

    CefString error;

    CefRefPtr dict = CefDictionaryValue::Create();

    dict->SetString("mode", "fixed_servers");

    dict->SetString("server", "myproxy:808");

    CefRefPtr value = CefValue::Create();

    value->SetDictionary(dict);

    context->SetPreference("proxy", value, error);    return false;

}

```

其中mode可以取值以下任一:

fixed_servers

pac_script

auto_detect

system

direct

而当mode为fixed_servers时需要指定server参数,当mode为pac_script时需要指定pac_url参数。

如果代理需要认证,那么需要同时实现:

`

bool MyRequestHandler::GetAuthCredentials(

    CefRefPtr browser,

    CefRefPtr frame,    bool isProxy,    const CefString& host,    int port,    const CefString& realm,    const CefString& scheme,

    CefRefPtr callback) {    if (isProxy) {

        callback->Continue("myuser", "mypass");        return true;

    }    return false;

}

`



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3