c# - 代理验证错误

标签 c# .net network-programming proxy-authentication

HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create("https://www.example.com");
NetworkCredential nc = new NetworkCredential("myname", "mypass");
WebProxy myproxy = new WebProxy("192.168.1.1:8080", false);
myHttpWebRequest.Proxy = myproxy;
myHttpWebRequest.Proxy = WebRequest.DefaultWebProxy;
myHttpWebRequest.Method = "GET";

HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
MessageBox.Show("Ok");

我正在使用此代码连接网站(C#.net 桌面应用程序)。但我收到此错误消息:

The remote server returned an error: (407) Proxy Authentication Required.

我该如何解决这个问题?

最佳答案

您当前未在代理中使用凭据。这是一个 example adapted from MSDN of how to use your NetworkCredential :

class Downloader
{
    static void Main(string[] args)
    {
        NetworkCredential nc = new NetworkCredential(args[0], args[1]);
        WebProxy proxy = new WebProxy(args[2], false);
        proxy.Credentials = nc;

        WebRequest request = new WebRequest(args[3]);
        request.Proxy = proxy;
        using (WebResponse response = request.GetResponse())
        {
            Console.WriteLine(
                @"{0} - {1} bytes",
                response.ContentType,
                response.ContentLength);
        }
    }
}

当我编译并运行这个完整的例子时:

C:\cs>csc proxy.cs
Microsoft (R) Visual C# 2010 Compiler version 4.0.30319.1
Copyright (C) Microsoft Corporation. All rights reserved.


C:\cs>proxy user pass http://proxy:80 http://www.google.com
text/html; charset=ISO-8859-1 - 31398 bytes

C:\cs>

当然,我为我的工作帐户使用了我的实际用户/通行证和代理。

关于c# - 代理验证错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8223396/

相关文章:

c# - 我不明白持久独立性的意义

asp.net - 如何提高 .net wcf 服务的性能

c# - 从 IList<string> 或 IEnumerable<string> 创建逗号分隔列表

c# - C# 'for' 循环中的多重初始化

c# - 使用 JSON.NET 将 C# 对象转换为 JSON 时出现额外的 '\'

c# - 可调整大小的窗口上的框架应显示滚动条

c# - 加入并包含在 Entity Framework 中

java - Android 上的持久 HttpURLConnections

c - 网络编程 : what happen if network disconnect after select and before send

c - 为什么客户端调用shutdown(sockfd, SHUT_RD)后,客户端程序中的recv()可以接收到发送给客户端的消息?