Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#中的代理基本身份验证:HTTP 407错误_C#_Proxy_Httpwebrequest_Basic Authentication_Http Status Code 407 - Fatal编程技术网

C#中的代理基本身份验证:HTTP 407错误

C#中的代理基本身份验证:HTTP 407错误,c#,proxy,httpwebrequest,basic-authentication,http-status-code-407,C#,Proxy,Httpwebrequest,Basic Authentication,Http Status Code 407,我正在使用一个需要身份验证的代理,即,在浏览器中,如果我尝试打开一个页面,它将立即要求提供凭据。我在程序中提供了相同的凭据,但由于HTTP 407错误而失败 这是我的密码: HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL); IWebProxy proxy = WebRequest.GetSystemWebProxy(); CredentialCache cc = new CredentialCache(); Net

我正在使用一个需要身份验证的代理,即,在浏览器中,如果我尝试打开一个页面,它将立即要求提供凭据。我在程序中提供了相同的凭据,但由于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;
我已经尝试了所有可能的事情,但似乎我错过了什么。
是不是我得提出两个要求?首先没有凭据,一旦我从服务器上听到需要凭据的消息,请使用凭据发出相同的请求?

下面是使用代理和凭据的正确方法

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;

谢谢大家的帮助……:)

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

将其放入应用程序配置文件-可能是app.config。Visual Studio将在生成时将其重命名为yourappname.exe.config,它将在可执行文件旁边结束。如果没有应用程序配置文件,只需使用VisualStudio中的AddNewItem添加一个即可

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.net>
    <defaultProxy useDefaultCredentials="true" />
  </system.net>
</configuration>

我遇到了一个非常类似的情况,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;
}

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

这个问题困扰了我多年,我唯一的解决办法是让我们的网络团队在防火墙上设置例外,这样某些URL请求就不需要在不理想的代理上进行身份验证

最近,我将项目从3.5升级到了.NET4,代码刚刚开始使用代理的默认凭据工作,没有对凭据进行硬编码等

request.Proxy.Credentials = CredentialCache.DefaultCredentials;
试试这个


你可以这样使用,它很有效

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

由于有密码保护的代理服务器,我也遇到了类似的问题,在那里找不到太多的信息,希望这对其他人有所帮助。我想获取客户浏览器使用的凭据。但是,当代理拥有自己的用户名和密码时,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凭据部分。

代理服务器需要什么身份验证方案?基本的?NTLM?basic,这就是我在creds缓存中使用basic的原因…只有当您想强制用户发现其代理服务器地址和端口号,并在应用程序中键入用户名和密码,并将其存储在某个位置(以明文或使用必要的可逆加密)时,这才是正确的-不太理想-检查我的答案,寻找一种可能在大多数情况下都能工作的更干净的方法。在应用程序中存储代理地址和凭据不是一个好主意-其他两个答案中的任何一个都比这好这在我的控制台应用程序中工作过,但在我的.NET应用程序中不支持HTTPS。tomfanning的解决方案确实有效。谢谢是的,这是更好的方法。因为您不需要在代码中添加凭据+1.作品seamlessly@tomfanning,我相信如果代理服务器凭据与用户的默认凭据不同,此解决方案将无法工作。例如,如果代理服务器只允许特定帐户,并且每个帐户都有自己的密码。在这种情况下,使用Deafult凭证将不起作用。5年后。。。是的,如果默认凭据(即您的Windows帐户)不是您的代理所需的凭据,则此解决方案当然不起作用。
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;
}