c# - (407) 需要代理身份验证

标签 c# proxy proxy-authentication

我知道这个问题已经被问过很多次了。我已经阅读了这里和其他类似网站上的大部分帖子。

http://social.msdn.microsoft.com/Forums/en-US/ncl/thread/2931d21c-9ca8-4256-b213-915fad4c941b/

没有用。这里的环境

window 服务器 2008 R2 64 位 Visual Studio 2008 .Net Framework 3.5

这是我尝试过的

我让代理使用代码进行身份验证

WebRequest req = WebRequest.Create(requestUri + data);
req.Proxy = new System.Net.WebProxy(<ProxyURL>:<port>",true);
req.Proxy.Credentials = CredentialCache.DefaultCredentials;
WebResponse resp = req.GetResponse();

这行得通,但看到它正在减慢应用程序的速度,我了解到我可以编辑我所做的 machine.config 文件。它也有效!

    <system.net>
      <defaultProxy
      useDefaultCredentials="true">
      <proxy
        proxyaddress="<proxyURL>:<port>"
        bypassonlocal="True"/>
     </defaultProxy>
   </system.net>

至少一两天。然后它开始失败。

然后我把它编辑成这个

    <system.net>
     <defaultProxy
       useDefaultCredentials="true">
       <proxy usesystemdefault="True"/>
       </defaultProxy>
    </system.net>

据我了解,这将使用 IE 设置连接到代理,但仍然无法正常工作。我也试过tihs代码

WebProxy proxy = new WebProxy(<proxy>:<port>);
CredentialCache myCache = new CredentialCache();
myCache.Add(new Uri(requestUri + data), "BASIC", new NetworkCredential(<username>,<password>));
proxy.Credentials = myCache;
request.Proxy = proxy;
request.Method = "GET";

但这并没有奏效。

注意:我可以将 machine.config 文件复制到我的计算机 (Win XP) 并从那里运行 .exe(没有代理代码),它工作正常。

对于 64 位操作系统,我需要做些什么不同的事情吗?我也可以在服务器上打开 IE8 并访问 URI 就好了。目标是预先验证代理,而不必在代码中提供用户名密码。

最佳答案

@David Moore 是对的。如果 IE 在您手动浏览时工作正常,则只需添加 req.Proxy.Credentials = CredentialCache.DefaultCredentials; 即可正常工作。

这是从 MSDN 中获取的对我有用的修改代码。

using System;
using System.Diagnostics;
using System.IO;
using System.Net;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            string urlDemo = "http://en.wikipedia.org/wiki/Main_Page";
            // Create a request for the URL. 
            WebRequest request = WebRequest.Create(urlDemo);
            // If required by the server, set the proxy credentials.
            request.Proxy.Credentials = CredentialCache.DefaultCredentials;
            // Get the response.
            WebResponse response = request.GetResponse();
            // Display the status.
            Console.WriteLine(((HttpWebResponse)response).StatusDescription);
            // Get the stream containing content returned by the server.
            Stream dataStream = response.GetResponseStream();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader(dataStream);
            // Read the content.
            string responseFromServer = reader.ReadToEnd();
            // Display the content.
            Console.WriteLine(responseFromServer);
            Console.ReadLine();
            // Clean up the streams and the response.
            reader.Close();
            response.Close();

        }
    }
}

希望对您有所帮助;-)

关于c# - (407) 需要代理身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15710733/

相关文章:

c# - 如何更改可选面板的边框颜色?

authentication - 使用 AWS CloudFront 时,如何向公众隐藏自定义源服务器?

java - Camel Http4 使用基本身份验证和代理身份验证

shell - 使用用户名和密码连接 ADB 代理

C# : Cancelling MySqlCommand using CancellationToken giving NULLReferenceException

c# - 当 SqlBulkCopy 不返回任何错误时调试问题

C# 扩展方法返回值不设置变量

java - WireMock 可以回放来自多个域的请求吗?

python - 使 Python 3.x Slack (slackclient) 使用企业代理

tcp - WSO2 ESB 4.9.0 : what means error 101500