.NET Core 2.0中的默认代理 [英] Default proxy in .net core 2.0

查看:277
本文介绍了.NET Core 2.0中的默认代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到几个有关核心2.0的问题,有关如何使HttpClient使用系统上配置的默认代理.但没有找到正确答案的地方.发布此问题,希望可能遇到此问题的人现在已经找到了解决方案.

I saw couple of questions asked about core 2.0 on how to make HttpClient to use default proxy configured on the system. But no where found right answer. Posting this question hoping someone who might have encountered this issue might have found the solution by now.

在.net Framework版本中,我在web.config中使用了以下配置,并且对我有用.

In .net framework versions I've used the following configuration in my web.config and it worked for me.

  <system.net>
    <defaultProxy useDefaultCredentials="true"></defaultProxy>
  </system.net>

但是在.net core 2.0中,我已经从公司的内部网向外部api发出了Web请求,我的代码因407失败,需要代理身份验证.

But in .net core 2.0 where I've make a web request to external api from my company's intranet my code is failing with 407, proxy authentication required.

经过一些研究,我认为无法使HttpClient使用通过IE中的WPAD配置的默认代理设置.有人可以在这里纠正我的理解吗?

After little bit of research I am of the opinion that it is not possible to make your HttpClient to use default proxy settings configured via WPAD in IE. Can someone correct my understanding here?

在此页面上 https://github.com/dotnet/corefx/issues/7037

是这样说的:

"HttpClientHandler.UseProxy属性的默认值为true.而HttpClientHandler.Proxy的默认值为NULL,这意味着将使用默认代理."

"The default for HttpClientHandler.UseProxy property is true. And the default value of HttpClientHandler.Proxy is NULL which means to use the default proxy."

但是我没有观察到这种行为.

But I don't observe this behavior.

更新:

我终于可以通过指定代理服务器地址然后调用HttpClient来调用外部Web api.仍然想知道如何在IE中使用默认代理设置.

I am finally able to call external web api by specifying the proxy server address and then making the HttpClient call. Still wondering how to use default proxy setup in IE.

 using (var handler = new HttpClientHandler {
                    Credentials = new System.Net.NetworkCredential(user, password, domain),

                    UseProxy = true,
                    Proxy = new System.Net.WebProxy(new Uri("http://xxxxxxx:8080"), true)                                       
                })
{

    handler.Proxy.Credentials = new NetworkCredential("xxxx", "yyyyy", "cccccc");                    
    using (var httpClient = new HttpClient(handler))
    {
        var request = new HttpRequestMessage()
        {
            RequestUri = new Uri(destinationUrl),
            Method = HttpMethod.Post
        };

        request.Content = new StringContent(requestXml, Encoding.UTF8, "text/xml");             

        HttpResponseMessage response = await httpClient.SendAsync(request);

        Task<Stream> streamTask = response.Content.ReadAsStreamAsync();
    }
}   

如果有人对发现我如何能够找到代理服务器感兴趣,我在.net 4.0中编写了以下代码,并找出了所使用的代理.

If any one interested in finding out how I was able to find out the proxy server was, I wrote the following code in .net 4.0 and found out the proxy used.

var proxy = WebRequest.GetSystemWebProxy();
var url = proxy.GetProxy(new Uri("http://google.com"));

谢谢

推荐答案

我希望这是您正在寻找的答案:

I hope this is the answer you're looking for: Default Proxy issues #28780

如果您只想使用默认系统代理,并且需要在HTTP请求期间将默认凭据传递给该代理(因为该代理是经过身份验证的代理),请执行以下操作:

If you simply want to use the default system proxy and need to pass default credentials to that proxy (because the proxy is an authenticated proxy) during HTTP requests, then do this:

var handler = new HttpClientHandler();
handler.DefaultProxyCredentials = CredentialCache.DefaultCredentials;
var client = new HttpClient(handler);

这篇关于.NET Core 2.0中的默认代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆