.net core 2.0 proxy requests always result in http 407 (Proxy Authentication Required)

為{幸葍}努か 提交于 2019-12-10 13:35:11

问题


I'm trying to make HTTP requests via a WebProxy in a .net core 2.0 web application. The code I've got works fine in .net framework so I know (believe) its not an environmental issue. I've also tried to make the request using both HttpWebRequest and HttpClient but both mechanisms always result in 407 (Proxy Authentication Required) http error in .net core. Its as if in .net core the credentials I'm supplying are always being ignored.

Here is the code I've been using:

public void Test() 
{
    var cred = new NetworkCredential("xxxxx", "yyyyyy");
    var proxyURI = new Uri("http://xxx.xxx.xxx.xxx:80");
    var destinationURI = new Uri("http://www.bbc.co.uk");
    WebProxy proxy = new WebProxy(proxyURI, false) { UseDefaultCredentials = false, Credentials = cred };

    MakeProxyRequestViaHttpWebRequest(proxy, destinationURI);
    MakeProxyRequestViaHttpClient(proxy, destinationURI);
}

private void MakeProxyRequestViaHttpClient(WebProxy proxy, Uri destination)
{
    HttpClientHandler handler = new HttpClientHandler()
    {
        Proxy = proxy,
        UseProxy = true,
        PreAuthenticate = true,
        UseDefaultCredentials = false
    };

    HttpClient client = new HttpClient(handler);
    HttpResponseMessage response = client.GetAsync(destination).Result;
    if (response.IsSuccessStatusCode)
    {
        HttpContent content = response.Content;
        string htmlData = content.ReadAsStringAsync().Result;
    }
    else
    {
        HttpStatusCode code = response.StatusCode;
    }
}

private void MakeProxyRequestViaHttpWebRequest(WebProxy proxy, Uri destination)
{
    HttpWebRequest req = HttpWebRequest.Create(destination) as HttpWebRequest;
    req.UseDefaultCredentials = false;
    req.Proxy = proxy;
    req.PreAuthenticate = true;

    using (WebResponse response = req.GetResponse())
    {
        using (StreamReader responseStream = new StreamReader(response.GetResponseStream()))
        {
            string htmlData = responseStream.ReadToEnd();
        }
    }
}

I've tried the following in .net core but the result is always 407:

  1. Run the code in a console app
  2. Implement IWebProxy and use that as the proxy
  3. Set default values for other properties on WebProxy, HttpClient, etc. (removed on the example above because it works fine on .net standard)

I've run out of ideas and things to try. I have the following questions:

  1. Does the code need to be different between .net core vs .net framework
  2. Are there additional things that need to go into appsettings.json (ie. the config that would have gone into web.config)
  3. Is there any additional configuration code required in Startup.cs
  4. Should I be looking to use an external library
  5. How would I trouble shoot what the issue is? Fiddler doesn't seem to be helping but then I haven't looked to hard at configuring it.

来源:https://stackoverflow.com/questions/47874202/net-core-2-0-proxy-requests-always-result-in-http-407-proxy-authentication-req

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!