XMLDocument.Load(url) through a proxy

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); 

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);

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.

Tags:

C#

Xml

Proxy