.NET Core 2.0中的默认代理

编程入门 行业动态 更新时间:2024-10-26 04:26:52
本文介绍了.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.

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

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

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

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

But in 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?

在此页面上 github/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("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(); } }

如果有人对发现我如何能够找到代理服务器感兴趣,我在 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 4.0 and found out the proxy used.

var proxy = WebRequest.GetSystemWebProxy(); var url = proxy.GetProxy(new Uri("google"));

谢谢

推荐答案

我希望这是您正在寻找的答案:默认代理问题#28780

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中的默认代理

本文发布于:2023-11-08 11:02:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1569196.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:NET   Core

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!