远程服务器返回错误:(407)需要代理身份验证

时间:2013-01-21 08:40:49

标签: c# httpwebrequest

我正在尝试从我的winforms应用程序中打开一个URL,并且我收到“407 Proxy Authentication Required”错误。我能够在我的机器上打开部署在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();

3 个答案:

答案 0 :(得分:2)

这表示系统设置中的代理集要求您在能够使用它之前登录。如果它在您的浏览器中有效,那么您最有可能在过去这样做过。在系统的网络设置中禁用代理,或添加相应的标头以对代理进行身份验证。

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

答案 2 :(得分: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");

}