请求https的代理错误

时间:2012-08-14 08:17:28

标签: c# https proxy httpwebrequest

我正在使用这段小代码来访问远程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

1 个答案:

答案 0 :(得分: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;
}