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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14434996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:48:20  来源:igfitidea点击:

Remote Server returned an error: (407) Proxy Authentication Required

c#httpwebrequest

提问by Tim

I am trying to open a url from my winforms application and i am getting "407 Proxy Authentication Required" error. I am able to open a sample application that was deployed on IIS in my machine. but if i try to access any other URL getting this error. Here is the source code. Any suggestions please.

我正在尝试从我的 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();

回答by Magnus

That would indicate that the proxy set in your system settings requires you to log in before you're able to use it. If it works in your browser, you've most likely done so in the past. Either disable the proxy in your system's network settings, or add the appropriate headers to authenticate against the proxy.

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

回答by Anwuna

Try this, it worked for me

试试这个,它对我有用

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()); 
        }
    }

回答by Ronel Gonzales

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

}