使用 HttpClient 类时如何对代理进行身份验证?

编程入门 行业动态 更新时间:2024-10-22 19:47:17
本文介绍了使用 HttpClient 类时如何对代理进行身份验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个基于 .NET 4 的控制台应用程序,它使用 HttpClient 库(通过 NuGet 获得)通过 Internet 从公共 API 检索数据.控制台应用程序位于代理后面.它所在的 Windows 计算机在 Internet Explorer 中具有正确的代理设置.当控制台应用程序尝试访问外部世界时,它会获得 407 状态 - 需要代理身份验证".

I have a console app built on .NET 4 that uses the HttpClient library (obtained via NuGet) to retrieve data from a public API over the internet. The console app sits behind a proxy. The Windows machine it sits on has the correct proxy settings in Internet Explorer. When the console application attempts to access the outside world it is given a 407 status - "proxy authentication required".

我还没有编写特定的代码来处理代理.我从 407 错误假设应用程序指向代理没有问题,但代理没有对请求进行身份验证.

I've written no specific code to deal with a proxy yet. I'm assuming from the 407 error that the application is directing to the proxy no problem, but the proxy isn't authenticating the request.

我的问题是,是否可以将 HttpClient 设置为使用登录用户的凭据来验证代理?如果是这样,如何?

My question is, is it possible to set the HttpClient to use the credentials of the logged in user to authenticate the proxy? If so, how?

var client = new HttpClient(webRequestHandler); client.PostAsync(RequestUri, MyContent);

推荐答案

我通过 app.config 中的代理配置非常简单地解决了我的问题.

I managed to solve my problem very simply through proxy configuration in app.config.

<system> <defaultProxy enabled="true" useDefaultCredentials="true"> <proxy usesystemdefault="True" /> </defaultProxy> </system>

msdn.microsoft/en-us/library/dkwyc043.aspx

更多推荐

使用 HttpClient 类时如何对代理进行身份验证?

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

发布评论

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

>www.elefans.com

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