【问题标题】:How to add remote proxy to all WebClient requests如何将远程代理添加到所有 WebClient 请求
【发布时间】:2012-05-06 16:53:46
【问题描述】:

这是我的代码,确保这些请求通过远程代理传递的最佳方法是什么?

        String openUrl = @"www.site.com/page.html";
        WebClient myClient = new WebClient();

        myClient.UseDefaultCredentials = true;
        IWebProxy theProxy = myClient.Proxy;
        if (theProxy != null)
        {
            theProxy.Credentials = CredentialCache.DefaultCredentials;
        }

        myClient.Proxy = WebRequest.DefaultWebProxy;
        string webPageString = myClient.DownloadString(openUrl);
        HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
        doc.LoadHtml(webPageString);

【问题讨论】:

  • 您希望每个请求都通过您的代理服务器吗?
  • 不,只有在执行此功能时,我希望通过外部代理服务器路由此特定请求

标签: c# winforms proxy webclient


【解决方案1】:

我认为你的代码是错误的...
这是一个工作:

WebProxy p = null;
string proxyAddressAndPort ="I.P.ADD.RES:port";
string proxyUserName ="%username%";
string proxyPassword ="%password%";
ICredentials cred;
cred = new NetworkCredential(proxyUserName, proxyPassword);
p = new WebProxy(proxyAddressAndPort, true, null, cred);
WebRequest.DefaultWebProxy = p;

作为一种使这些请求通过远程代理传递的方法,以便像 myip.net 一样使用 smth

【讨论】:

    【解决方案2】:
            HtmlWeb hw = new HtmlWeb();
            hw.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)";
            hw.PreRequest = new HtmlAgilityPack.HtmlWeb.PreRequestHandler(p.ProxyOnPreRequest); // this is proxy request
            HtmlAgilityPack.HtmlDocument doc = hw.Load(openUrl);
    
        public bool ProxyOnPreRequest(HttpWebRequest request)
        {
            WebProxy myProxy = new WebProxy("203.189.134.17:80");
            request.Proxy = myProxy;
            return true; // ok, go on
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-14
      • 2014-06-04
      • 1970-01-01
      • 2017-09-22
      • 1970-01-01
      • 2021-12-30
      • 2017-04-22
      • 2019-09-04
      相关资源
      最近更新 更多