HTTP 客户端未向代理服务器 C# 进行身份验证

我正在尝试使用凭据从客户端站点的 Windows 服务对代理服务器进行身份验证,但不断收到以下响应:

StatusCode: 407, ReasonPhrase: 'Proxy Authentication Required', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: {
Pragma: no-cache
Proxy-Connection: close
Connection: close
Cache-Control: no-cache
Proxy -验证:NTLM
Set-Cookie:BCSI-CS-2737f33ff5b5f739=2;路径=/
内容长度:1351
内容类型:文本/html;字符集=utf-8
}

我的本地代理配置完全不同,我无法重现该问题。这是我用来与服务器进行身份验证的代码。(如代码中所述,它基于:HttpClient 和使用代理 - 不断获得 407

 private HttpClient GenerateHttpClientWithProxySettings()

{

    //Code based on https://stackoverflow.com/questions/29856543/httpclient-and-using-proxy-constantly-getting-407


    // First create a proxy object

    var proxy = new WebProxy()

    {

        Address = new Uri($"{Settings.ProxyAddress}:{Settings.PortNumber}"),

        BypassProxyOnLocal = false,

        UseDefaultCredentials = false,

        // *** These creds are given to the proxy server, not the web server ***

        Credentials = new NetworkCredential(

            userName: Settings.ProxyUserName,

            password: Settings.ProxyPassword)

    };


    //Debug:

    Settings.log.Debug($"{Settings.ProxyAddress}:{Settings.PortNumber}");

    Settings.log.Debug(Settings.ProxyUserName);

    Settings.log.Debug(Settings.ProxyPassword);


    // Now create a client handler which uses that proxy

    var httpClientHandler = new HttpClientHandler()

    {

        Proxy = proxy,

        UseProxy = true,

        AllowAutoRedirect = true,

    };

对我在这里做错的任何帮助将不胜感激。


HUX布斯
浏览 184回答 1
1回答

幕布斯7119047

从响应头Proxy-Authenticate: NTLM代理接受 Windows 凭据而不是用户名/密码。试试这个var proxy = new WebProxy(){    ...    UseDefaultCredentials = true    /*Credentials = new NetworkCredential(        userName: Settings.ProxyUserName,        password: Settings.ProxyPassword)*/};
打开App,查看更多内容
随时随地看视频慕课网APP