我正在尝试弄清楚在使用System.Net.WebClient类时如何可靠地处理代理身份验证错误(HTTP407状态代码)。
在现场,我们看到许多用户收到407代理身份验证WebException,但我不确定什么是好的默认策略。在.Net 2.0/3.5中,代理身份验证设置应继承自Internet Explorer系统设置。Firefox、Opera和Chrome使用相同的设置。
下面是我们使用的基本代码:
using System.Net;
string url = "http://www.mysite.com";
WebClient webClient = new WebClient();
byte[] data = webClient.DownloadFile(url);当此代码失败时,我们打开用户的浏览器并将其发送到帮助页面。从我们的web日志中,我们知道这些客户可以在他们的浏览器中成功连接。也许他们在进入我们的帮助页面之前手动输入了他们的代理用户名和密码?我们不知道。
似乎我们可以使用WebClient.UseDefaultCredentials,但如果WebClient使用系统设置,这似乎是多余的。
任何帮助都是非常感谢的。
发布于 2009-07-30 03:11:04
如果代理身份验证使用BASIC或DIGEST,则Internet Explorer不会永久缓存/重用代理身份验证凭据。对于协商/NTLM,将提供默认凭据。
因此,即使.NET继承了IE的设置,您也不会获得对基本/摘要代理身份验证的任何“免费”支持,除非您碰巧在IE中运行;您需要提示用户或提供配置屏幕。
Fiddler (www.fiddler2.com)在Rules菜单上有"Request Proxy Authentication“选项,您可以使用该选项来模拟此场景进行测试。
发布于 2009-07-29 20:21:44
我们通过添加一个允许用户选择“使用代理”的配置对话框解决了这个问题。使用默认凭据b重试。)
如果代理身份验证是通过"default credentials“(Windows用户)完成的,则IE也会对身份验证错误作出反应,并在这种情况下发送默认凭据。如果此操作不起作用,则会打开一个凭据对话框。我不确定是否所有的浏览器都是这样处理的--但你可以简单地用fiddler试一试,这样你就能知道发生了什么。
发布于 2012-12-04 19:49:44
我知道这是一篇老文章,但我在尝试通过代理服务器在SSIS2008R2 (SQL Server Integration Services)脚本任务(VB.NET代码)中使用WebClient将XML文件下载到通过VB.NET保护的远程站点(也需要身份验证)时,遇到了类似的问题。
花了一段时间才找到解决方案,这篇文章在代理方面起到了帮助作用。下面是为我工作的脚本代码。可能对搜索类似的人有用。
Dim objWebClient As WebClient = New WebClient()
Dim objCache As New CredentialCache()
'https://www.company.net/xxxx/resources/flt
Dim strDownloadURL As String = Dts.Variables("FileURL").Value.ToString
'apiaccount@company.net
Dim strLogin As String = Dts.Variables("FileLogin").Value.ToString
'sitepassword
Dim strPass As String = Dts.Variables("FilePass").Value.ToString
'itwsproxy.mycompany.com
Dim strProxyURL As String = Dts.Variables("WebProxyURL").Value.ToString
'8080
Dim intProxyPort As Integer = Dts.Variables("WebProxyPort").Value
'Set Proxy & Credentials as a Network Domain User acc to get through the Proxy
Dim wp As WebProxy = New WebProxy(strProxyURL, intProxyPort)
wp.Credentials = New NetworkCredential("userlogin", "password", "domain")
objWebClient.Proxy = wp
'Set the Credentials for the Remote Server not the Network Proxy
objCache.Add(New Uri(strDownloadURL), "Basic", New NetworkCredential(strLogin, strPass))
objWebClient.Credentials = objCache
'Download file, use Flat File Connectionstring to save the file
objWebClient.DownloadFile(strDownloadURL, Dts.Connections("XMLFile").ConnectionString)https://stackoverflow.com/questions/1202708
复制相似问题