System.Net.WebClient与代理身份验证407错误 [英] System.Net.WebClient vs. Proxy Authentication 407 error

查看:52
本文介绍了System.Net.WebClient与代理身份验证407错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试弄清楚如何可靠地处理代理身份验证错误(HTTP使用System.Net.WebClient类时显示407状态代码.

I'm trying to figure out how to robustly handle proxy authentication errors (HTTP 407 status code) when using the System.Net.WebClient class.

在该字段中,我们看到许多用户收到407代理身份验证WebException,但是我不确定什么是好的默认策略.在.Net中2.0/3.5,应该从中继承代理身份验证设置Internet Explorer系统设置.Firefox,Opera和Chrome使用这些相同的设置.

In the field, we see many users receiving a 407 proxy authentication WebException, but I'm not sure what a good default strategy is. In .Net 2.0/3.5, the proxy authentication settings are supposed to be inherited from the Internet Explorer system settings. Firefox, Opera and Chrome use these same settings.

这是我们正在使用的基本代码:

Here's the basic code we are using:

using System.Net;

string url = "http://www.mysite.com";
WebClient webClient = new WebClient();
byte[] data = webClient.DownloadFile(url);

此代码失败时,我们打开用户的浏览器并将其发送给帮助页.从我们的网络日志中,我们知道这些客户可以成功连接他们的浏览器.也许他们是在手动输入代理用户名,然后密码才能进入我们的帮助页面?我们不知道.

When this code fails, we open the user's browser and send them to a help page. From our web logs, we know these customers can successfully connect in their browsers. Perhaps they are manually entering their proxy user name and password before they get to our help page? We don't know.

似乎我们可以使用WebClient.UseDefaultCredentials,但这如果WebClient仍在使用系统设置,则似乎是多余的.

It seems that we could use WebClient.UseDefaultCredentials, but this seems redundant if WebClient is using the system settings anyway.

感谢您的帮助.

推荐答案

如果代理身份验证使用BASIC或DIGEST,则Internet Explorer不会持久缓存/重复使用代理身份验证凭据.对于协商/NTLM",将提供默认凭据.

Internet Explorer does not persistently cache/reuse proxy authentication credentials if the proxy auth uses BASIC or DIGEST. For Negotiate/NTLM, default credentials will be provided.

因此,即使.NET继承自IE设置,除非您恰好在IE中运行,否则您将无法获得对Basic/Digest代理身份验证的任何免费"支持.您需要提示用户或提供配置屏幕.

Hence, even though .NET inherits from IE settings, you won't get any "free" support for proxy authentication for Basic/Digest unless you happen to be running in IE; you'll need to prompt the user or provide a configuration screen.

Fiddler(www.fiddler2.com)在规则"菜单上具有请求代理身份验证"选项,可用于模拟此场景进行测试.

Fiddler (www.fiddler2.com) has the "Request Proxy Authentication" option on the Rules menu that you can use to simulate this scenario for testing.

这篇关于System.Net.WebClient与代理身份验证407错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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