CEF中使用代理的方法

CEF中使用代理的方法,第1张

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:80http=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

}

`

CefSharp可以使用C#和.Net停止request,还可以使用以下方法:

1、使用CefSharp API调用停止Request:

CefSharp API允许您调用CefSharp.IRequestHandler.OnBeforeResourceLoad()方法来拦截请求,并在此方法中调用CefSharp.IRequestHandler.OnResourceResponse()方法来停止请求。

2、使用CefSharp自定义Request拦截器:

CefSharp还允许您使用CefSharp.IResourceRequestHandler实现来自定义拦截器,这样您可以自定义拦截器来拦截请求,并调用CefSharp.IResourceRequestHandler.OnResourceResponse()方法来停止请求。

3、使用CefSharp自定义Request拦截器:

CefSharp还允许您使用CefSharp.IWebRequestFilter实现来自定义拦截器,这样您可以使用CefSharp.IWebRequestFilter.OnBeforeSendHeaders()方法来拦截请求,并调用CefSharp.IWebRequestFilter.OnBeforeSendHeaders()方法来停止请求。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11863853.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存