为了账号安全,请及时绑定邮箱和手机立即绑定

System.Data.DataSet.ReadXml - 需要代理身份验证

System.Data.DataSet.ReadXml - 需要代理身份验证

C#
慕沐林林 2021-11-14 15:39:43
我有一段旧代码(单个 .aspx 文件),需要通过代理获取。这段代码曾经有效,但现在公司加强了安全性。有问题的代码行是:dataSet.ReadXml(url);网址是 https。它在 .NET 2.0 版上运行 - 这无法升级。我无法更改 web.config 文件。我需要向 .aspx 文件添加什么才能使其工作?我得到的错误是:The remote server returned an error: (407) Proxy Authentication Required.脚本中没有“连接到网络”代码。编辑根据丹的评论,我试过这个:HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);IWebProxy proxy = request.Proxy;                    WebProxy myProxy = new WebProxy();Uri newUri = new Uri("http://10.79.30.190:8080");// Associate the newUri object to 'myProxy' object so that new myProxy settings can be set.myProxy.Address = newUri;// Create a NetworkCredential object and associate it with the // Proxy property of request object.myProxy.Credentials = new NetworkCredential("username", "password");request.Proxy = myProxy;HttpWebResponse response = (HttpWebResponse)request.GetResponse();string responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();System.Data.DataSet dataSet = new System.Data.DataSet();dataSet.ReadXml(responseString);但我仍然遇到同样的错误编辑另一种尝试:HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);IWebProxy proxy = request.Proxy;                    WebProxy myProxy = new WebProxy();Uri newUri = new Uri("http://10.79.30.190:8080");// Associate the newUri object to 'myProxy' object so that new myProxy settings can be set.myProxy.Address = newUri;// Create a NetworkCredential object and associate it with the // Proxy property of request object.myProxy.Credentials = new NetworkCredential("username", "password");request.Proxy = myProxy;HttpWebResponse response = (HttpWebResponse)request.GetResponse();System.Data.DataSet dataSet = new System.Data.DataSet();dataSet.ReadXml(new StreamReader(response.GetResponseStream()));但我仍然遇到同样的错误
查看完整描述

1 回答

?
慕森王

TA贡献1777条经验 获得超3个赞

这意味着,您的代理服务器凭据不正确,尝试解决此问题的最佳解决方案是:


首先,将此行添加到您的 Web.Config:


<system.net>

    <defaultProxy useDefaultCredentials="true" >

    </defaultProxy>

</system.net>

其次,是通过代码:


service.Proxy = WebRequest.DefaultWebProxy;

service.Credentials = System.Net.CredentialCache.DefaultCredentials; ;

service.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;

第三,是通过代码在两个位置设置凭据:


HttpWebRequest webRequest = WebRequest.Create(uirTradeStream) as HttpWebRequest;

webRequest.Proxy = WebRequest.DefaultWebProxy;

webRequest.Credentials = new NetworkCredential("user", "password", "domain");

webRequest.Proxy.Credentials = new NetworkCredential("user", "password", "domain");

这里最适合你。


查看完整回答
反对 回复 2021-11-14
  • 1 回答
  • 0 关注
  • 147 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信