4

我正在尝试从我的 winforms 应用程序打开一个 url,我收到“需要 407 代理身份验证”错误。我可以在我的机器上打开一个部署在 IIS 上的示例应用程序。但如果我尝试访问任何其他 URL 得到此错误。这是源代码。请有任何建议。

string url = "http://google.co.in";


HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebRequest.DefaultWebProxy.Credentials = CredentialCache.DefaultCredentials;
request.Method = "POST";

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

Console.WriteLine(response.StatusDescription);

Stream dataStream = response.GetResponseStream();

StreamReader reader = new StreamReader(dataStream);

string responseFromServer = reader.ReadToEnd();

Console.WriteLine(responseFromServer);
MessageBox.Show(responseFromServer);

reader.Close();
dataStream.Close();
response.Close();
4

3 回答 3

2

试试这个,它对我有用

string url = "http://google.co.in"; 
IWebProxy proxy = new WebProxy("your proxy server address", port number ); // port number is of type integer 
        proxy.Credentials = new NetworkCredential("your user name", "your password");

        try
        {
                WebClient client = new WebClient();
                client.Proxy = proxy;

                string resp = client.DownloadString(url);
                // more processing code 
                }
            }
        }
        catch (WebException ex)
        {
            MessageBox.Show(ex.ToString()); 
        }
    }
于 2013-06-27T12:23:03.090 回答
2

这表明您的系统设置中设置的代理要求您先登录才能使用它。如果它在您的浏览器中有效,那么您过去很可能已经这样做了。在系统的网络设置中禁用代理,或添加适当的标头以针对代理进行身份验证。

于 2013-01-21T08:43:53.517 回答
0
IWebProxy proxy = new WebProxy("proxy address", port number); 
proxy.Credentials = new NetworkCredential("username", "password");

using (var webClient = new System.Net.WebClient())
{
    webClient.Proxy = proxy;

    webClient.DownloadString("url");

}
于 2014-06-18T10:24:26.940 回答