CEF中使用代理的方法

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;

}

`

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容