XMLDocument.Load(url) through a proxy

2020-06-02 11:42发布

I have a bit of code that basically reads an XML document using the XMLDocument.Load(uri) method which works fine, but doesn't work so well if the call is made through a proxy.

I was wondering if anyone knew of a way to make this call (or achieve the same effect) through a proxy?

标签: c# xml proxy
5条回答
Explosion°爆炸
2楼-- · 2020-06-02 12:01

This is the code that I ended up using:

WebProxy wp = new WebProxy(Settings.Default.ProxyAddress);
wp.Credentials = new NetworkCredential(Settings.Default.ProxyUsername, Settings.Default.ProxyPassword);
WebClient wc = new WebClient();
wc.Proxy = wp;

MemoryStream ms = new MemoryStream(wc.DownloadData(url));
XmlTextReader rdr = new XmlTextReader(ms);
return XDocument.Load(rdr); 
查看更多
SAY GOODBYE
3楼-- · 2020-06-02 12:03

Use lomaxx's answer but change

MemoryStream ms = new MemoryStream(wc.DownloadData(url));
XmlTextReader rdr = new XmlTextReader(url);

to

MemoryStream ms = new MemoryStream(wc.DownloadData(url));
XmlTextReader rdr = new XmlTextReader(ms);
查看更多
疯言疯语
4楼-- · 2020-06-02 12:05

You can't configure XMLDocument to use proxy. You can use WebRequest or WebClient class to load data via proxy and pass obtained response stream to XMLDocument

Also you can try to use XmlTextReader class. It allows you set network credentials. For details see:

Supplying Authentication Credentials to XmlResolver when Reading from a File

查看更多
The star\"
5楼-- · 2020-06-02 12:10

Do you have to provide credentials to the proxy?

If so, this should help: "Supplying Authentication Credentials to XmlResolver when Reading from a File" http://msdn.microsoft.com/en-us/library/aa720674.aspx

Basically, you...

  1. Create an XmlTextReader using the URL
  2. Set the Credentials property of the reader's XmlResolver
  3. Create an XmlDocument instance and pass the reader to the Load method.
查看更多
相关推荐>>
6楼-- · 2020-06-02 12:16

You need to use WebProxy and WebRequest to download the xml, then parse it.

查看更多
登录 后发表回答