使用 HttpClient 类时如何对代理进行身份验证? [英] How to authenticate against a proxy when using the HttpClient class?

查看:38
本文介绍了使用 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.net>
    <defaultProxy enabled="true" useDefaultCredentials="true">
        <proxy usesystemdefault="True" />
    </defaultProxy>
</system.net>

http://msdn.microsoft.com/en-us/library/dkwyc043.aspx

这篇关于使用 HttpClient 类时如何对代理进行身份验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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