CEF中使用代理的方法

https://www.jianshu.com/p/0e5b25994daf

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:

       [<proxy-scheme>://]<proxy-host>[:<proxy-port>]

     Where <proxy-scheme> is the protocol of the proxy server, and is one of:

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

     If the <proxy-scheme> 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<CefBrowser> browser,

   CefRefPtr<CefFrame> frame,

   bool isProxy,

   const CefString& host,

   int port,

   const CefString& realm,

   const CefString& scheme,

   CefRefPtr<CefAuthCallback> 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<CefBrowser> browser,

                                CefRefPtr<CefFrame> frame,

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

    virtual bool GetAuthCredentials(CefRefPtr<CefBrowser> browser,

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

                                    CefRefPtr<CefAuthCallback> callback);private:    

    IMPLEMENT_REFCOUNTING(MyRequestHandler);

};

```

```

bool MyRequestHandler::OnBeforeBrowse(

    CefRefPtr<CefBrowser> browser,

    CefRefPtr<CefFrame> frame,

    CefRefPtr<CefRequest> request,

    bool is_redirect) {

    CefRefPtr<CefRequestContext> context =

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

    CefString error;

    CefRefPtr<CefDictionaryValue> dict = CefDictionaryValue::Create();

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

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

    CefRefPtr<CefValue> 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<CefBrowser> browser,

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

    CefRefPtr<CefAuthCallback> callback) {    if (isProxy) {

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

    }    return false;

}



作者:fakine
链接:https://www.jianshu.com/p/0e5b25994daf
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值