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-Authenticate: NTLM
Set-Cookie: BCSI-CS-2737f33ff5b5f739=2; Path=/
Content-Length: 1351
Content-Type: text/html; charset=utf-8
}

我的本地代理配置完全不同,我无法重新创建该问题.这是我用来验证服务器的代码. (如代码中所述,它基于:HttpClient and using proxy – constantly getting 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,
    };

    //// Omit this part if you don't need to authenticate with the web server:
    //if (needServerAuthentication)
    //{
    //    httpClientHandler.PreAuthenticate = true;
    //    httpClientHandler.UseDefaultCredentials = false;

    //    // *** These creds are given to the web server, not the proxy server ***
    //    httpClientHandler.Credentials = new NetworkCredential(
    //        userName: serverUserName,
    //        password: serverPassword);
    //}

    // Finally, create the HTTP client object
    HttpClient client = new HttpClient(handler: httpClientHandler, disposeHandler: true);
    return client;
}


//Get
private async Task<ReturnedData> GetRequestWithProxy(string url)
{
    ReturnedData result = new ReturnedData();
    using (HttpClient client = GenerateHttpClientWithProxySettings())
    {
        using (HttpResponseMessage message = await client.GetAsync(url))
        {
            Settings.log.Debug("[[message:]]");
            Settings.log.Debug(message.ToString());
            result.StatusCode = message.StatusCode.ToString();
            result.Succceded = message.IsSuccessStatusCode;
            using (HttpContent content = message.Content)
            {
                Settings.log.Debug("[[content:]]");
                Settings.log.Debug(content.ToString());
                result.Content = await message.Content.ReadAsStringAsync();
                return result;
            }
        }
    }
}


// Post 
private async Task<ReturnedData> PostRequestWithProxy(string url, dynamic payload)
{
    ReturnedData result = new ReturnedData();
    string stringPayload = await Task.Run(() => JsonConvert.SerializeObject(payload));
    // Wrap our JSON inside a StringContent which then can be used by the HttpClient class
    using (var httpContent = new StringContent(stringPayload, Encoding.UTF8, "application/json"))
    {
        using (HttpClient client = GenerateHttpClientWithProxySettings())
        {
            using (HttpResponseMessage message = await client.PostAsync(url, httpContent))
            {
                Settings.log.Debug(message.ToString());
                result.StatusCode = message.StatusCode.ToString();
                result.Succceded = message.IsSuccessStatusCode;
                using (HttpContent content = message.Content)
                {
                    Settings.log.Debug(content.ToString());
                    result.Content = await message.Content.ReadAsStringAsync();
                    return result;
                }
            }
        }
    }
}

任何有关我在这里做错的帮助将不胜感激.

谢谢!

最佳答案 从响应标题

Proxy-Authenticate: NTLM

代理接受Windows凭据而不是用户名/密码.

试试这个

var proxy = new WebProxy()
{
    ...
    UseDefaultCredentials = true
    /*Credentials = new NetworkCredential(
        userName: Settings.ProxyUserName,
        password: Settings.ProxyPassword)*/
};
点赞