zoukankan      html  css  js  c++  java
  • Cefsharp 75 设置代理

    方法1:

    CefSharp.CefSharpSettings.Proxy = new CefSharp.ProxyOptions("ip", "端口号", "用户名", "密码"); 

    这种方法的缺点是无法动态改变代理IP。

    方法2:在程序运行期间,动态修改代理IP

    ChromiumWebBrowser wb = new ChromiumWebBrowser("https://www.baidu.com/");
    wb.RequestHandler = new MyRequestHandler("rsel", "xanz");
    
    public class MyRequestHandler : DefaultRequestHandler
            {
                string _proxyUserName = "", _proxyUserPwd = "";
    
                public MyRequestHandler() { }
    
                public MyRequestHandler(string proxyUser,string proxyPwd)
                {
                    _proxyUserName = proxyUser;
                    _proxyUserPwd = proxyPwd;
                }
    
                public override bool GetAuthCredentials(IWebBrowser chromiumWebBrowser, IBrowser browser, string originUrl, bool isProxy, string host, int port, string realm, string scheme, IAuthCallback callback)
                {
                    if (isProxy == true)
                    {
                        callback.Continue(_proxyUserName, _proxyUserPwd);
    
                        return true;
                    }
    
                    return false;
                }
    
                
               
            }
    
    async private void SetProxy(ChromiumWebBrowser cwb, string address)
    
            {
                await Cef.UIThreadTaskFactory.StartNew(delegate
    
                {
                    var rc = cwb.GetBrowser().GetHost().RequestContext;
                    Dictionary<string, string> v = new Dictionary<string, string>();
                    if (!string.IsNullOrEmpty(address))
                    {
    
    
                        v["mode"] = "fixed_servers";
    
                        v["server"] = address;
                    }
                    else
                        v["mode"] = "direct";//设为direct表示取消代理
                    string error;
                    
                    bool success = rc.SetPreference("proxy", v, out error);
    
                });
    
            }
    
    需要设置/取消代理的时候,调用SetProxy方法即可。
  • 相关阅读:
    20171012
    BZOJ[2563] 阿狸和桃子的游戏
    BZOJ[1028] [JSOI2007]麻将
    BZOJ[1972] [Sdoi2010]猪国杀
    BZOJ[1033] [ZJOI2008] 杀蚂蚁antbuster
    P5651 基础最短路练习题
    P3047 [USACO12FEB]Nearby Cows G
    P6190 魔法
    P2391 白雪皑皑 / BZOJ 2054 疯狂的馒头
    CSP 2020 J/S 初赛游记
  • 原文地址:https://www.cnblogs.com/gulangduxiao/p/13472804.html
Copyright © 2011-2022 走看看