47

我正在使用需要身份验证的代理,即在浏览器中,如果我尝试打开一个页面,它将立即要求提供凭据。我在我的程序中提供了相同的凭据,但它因 HTTP 407 错误而失败。

这是我的代码:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);

IWebProxy proxy = WebRequest.GetSystemWebProxy();
CredentialCache cc = new CredentialCache();
NetworkCredential nc = new NetworkCredential();

nc.UserName = "userName";
nc.Password = "password";
nc.Domain = "mydomain";
cc.Add("http://20.154.23.100", 8888, "Basic", nc);
proxy.Credentials = cc;
//proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
request.Proxy = proxy;
request.Proxy.Credentials = cc;
request.Credentials = cc;
request.PreAuthenticate = true;

我已经尝试了所有可能的事情,但似乎我错过了一些东西。是不是像,我必须提出两个请求?首先没有凭据,一旦我从服务器收到关于需要凭据的回复,是否使用凭据发出相同的请求?

4

7 回答 7

80

这种方法可以避免硬编码或配置代理凭证的需要,这可能是可取的。

把它放在你的应用程序配置文件中——可能是 app.config。Visual Studio 将在构建时将其重命名为 yourappname.exe.config,并且最终会出现在您的可执行文件旁边。如果您没有应用程序配置文件,只需使用 Visual Studio 中的添加新项添加一个。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.net>
    <defaultProxy useDefaultCredentials="true" />
  </system.net>
</configuration>
于 2012-08-02T23:11:03.127 回答
29

我遇到了一个非常相似的情况,默认情况下 HttpWebRequest 没有获取正确的代理详细信息,并且设置 UseDefaultCredentials 也不起作用。然而,在代码中强制设置是一种享受:

IWebProxy proxy = myWebRequest.Proxy;
if (proxy != null) {
    string proxyuri = proxy.GetProxy(myWebRequest.RequestUri).ToString();
    myWebRequest.UseDefaultCredentials = true;
    myWebRequest.Proxy = new WebProxy(proxyuri, false);
    myWebRequest.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
}

并且因为这使用了默认凭据,所以它不应该向用户询问他们的详细信息。

于 2013-08-02T09:30:57.483 回答
18

这是使用代理和凭据的正确方法..

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);

IWebProxy proxy = request.Proxy;                    
if (proxy != null)
{
    Console.WriteLine("Proxy: {0}", proxy.GetProxy(request.RequestUri));
}
else
{
    Console.WriteLine("Proxy is null; no proxy will be used");
}

WebProxy myProxy = new WebProxy();
Uri newUri = new Uri("http://20.154.23.100:8888");
// Associate the newUri object to 'myProxy' object so that new myProxy settings can be set.
myProxy.Address = newUri;
// Create a NetworkCredential object and associate it with the 
// Proxy property of request object.
myProxy.Credentials = new NetworkCredential("userName", "password");
request.Proxy = myProxy;

谢谢大家的帮助... :)

于 2012-03-07T14:58:22.953 回答
4

这个问题多年来一直困扰着我,对我来说唯一的解决方法是要求我们的网络团队在我们的防火墙上设置例外,以便某些 URL 请求不需要在不理想的代理上进行身份验证。

最近我将项目从 3.5 升级到 .NET 4,代码刚刚开始使用代理的默认凭据,没有凭据的硬编码等。

request.Proxy.Credentials = CredentialCache.DefaultCredentials;
于 2015-05-21T11:18:35.057 回答
1

由于受密码保护的代理服务器,我遇到了类似的问题,并且在那里找不到太多信息 - 希望这对某人有所帮助。我想获取客户浏览器使用的凭据。但是,当代理拥有自己的用户名和密码时,CredentialCache.DefaultCredentials 和 DefaultNetworkCredentials 不起作用,即使我输入了这些详细信息以确保 Internet Explorer 和 Edge 可以访问。

最后我的解决方案是使用一个名为“CredentialManagement.Standard”的nuget包和以下代码:

using WebClient webClient = new WebClient();    
var request = WebRequest.Create("http://google.co.uk");
var proxy = request.Proxy.GetProxy(new Uri("http://google.co.uk"));

var cmgr = new CredentialManagement.Credential() { Target = proxy.Host };
if (cmgr.Load())
{
    var credentials = new NetworkCredential(cmgr.Username, cmgr.Password);
    webClient.Proxy.Credentials = credentials;
    webClient.Credentials = credentials;
}

这会从“凭据管理器”中获取凭据 - 可以通过 Windows 找到 - 单击开始,然后搜索“凭据管理器”。在浏览器提示时手动输入的代理凭据将位于 Windows 凭据部分。

于 2020-12-17T17:07:37.267 回答
0

你可以这样使用,它的工作原理!

        WebProxy proxy = new WebProxy
        {
            Address = new Uri(""),
            Credentials = new NetworkCredential("", "")
        };

        HttpClientHandler httpClientHandler = new HttpClientHandler
        {
            Proxy = proxy,
            UseProxy = true
        };

        HttpClient client = new HttpClient(httpClientHandler);

        HttpResponseMessage response = await client.PostAsync("...");
于 2020-02-27T14:58:29.607 回答
-1

试试这个

        var YourURL = "http://yourUrl/";

         HttpClientHandler handler = new HttpClientHandler() 
         { 
             Proxy = new WebProxy("http://127.0.0.1:8888"), 
             UseProxy = true, 
         }; 

         Console.WriteLine(YourURL); 

         HttpClient client = new HttpClient(handler); 
于 2015-11-05T16:09:18.250 回答