猿问

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

我有一段旧代码(单个 .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()));

但我仍然遇到同样的错误


慕沐林林
浏览 148回答 1
1回答

慕森王

这意味着,您的代理服务器凭据不正确,尝试解决此问题的最佳解决方案是:首先,将此行添加到您的 Web.Config:<system.net>&nbsp; &nbsp; <defaultProxy useDefaultCredentials="true" >&nbsp; &nbsp; </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");这里最适合你。
随时随地看视频慕课网APP
我要回答