2

我正在使用这段小代码来访问远程 Uri:

Uri uri = "http://www.myurl.com";
WebRequest wreq = WebRequest.Create(uri);
((HttpWebRequest)wreq).UserAgent = "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.75 Safari/537.1";
wreq.Proxy = WebRequest.DefaultWebProxy;

这适用于所有“http”Uri。但是,当切换到以下类型的 Uri (https) 时,我收到请求身份验证的代理错误 407(异常日志表明凭据错误)。你知道我该如何处理吗?

Uri uri = "https://www.myurl.com";
WebRequest wreq = WebRequest.Create(uri);
((HttpWebRequest)wreq).UserAgent = "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.75 Safari/537.1";
wreq.Proxy = WebRequest.DefaultWebProxy;

最好的祝福,

Al_th

4

1 回答 1

1

试试这个

private string GetPageSource(string url)
{
    string htmlSource = string.Empty;
    try
    {
        System.Net.WebProxy myProxy = new System.Net.WebProxy("Proxy IP", 8080);
        using (System.Net.WebClient client = new System.Net.WebClient())
        {
            client.Proxy = myProxy;
            client.Proxy.Credentials = new System.Net.NetworkCredential("username", "password");
            htmlSource = client.DownloadString(url);
        }
    }
    catch (WebException ex)
    {
        // log any exceptions
    }
    return htmlSource;
}
于 2012-08-14T08:45:39.690 回答