0

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

        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);
4

2 回答 2

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 的一种方法

于 2012-05-06T18:00:42.120 回答
1
        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
    }
于 2012-05-07T21:45:28.313 回答