How to read XML from the Internet using a web proxy?

This is a continuation of this question: How to load XML into a DataTable?

I want to read an XML file on the Internet in a DataTable. The XML file is here: http://rates.fxcm.com/RatesXML

If I do this:

public DataTable GetCurrentFxPrices(string url) { WebProxy wp = new WebProxy("http://mywebproxy:8080", true); wp.Credentials = CredentialCache.DefaultCredentials; WebClient wc = new WebClient(); wc.Proxy = wp; MemoryStream ms = new MemoryStream(wc.DownloadData(url)); DataSet ds = new DataSet("fxPrices"); ds.ReadXml(ms); DataTable dt = ds.Tables["Rate"]; return dt; } 

It works great. I am struggling with how to use the default proxy installed in Internet Explorer. I do not want to hard code proxies. I also want the code to work if the proxy is not specified in Internet Explorer.

+4
source share
3 answers

You can use Console.WriteLine (System.Net.WebProxy.GetDefaultProxy (). Address.AbsoluteUri); ...

+2
source

Add the following parameter to your app.config / web.config to automatically use the default proxy server:

 <system.net> <defaultProxy useDefaultCredentials="true"/> </system.net> 
+2
source
 #region Function to get x-rate via proxy public string fncProxyGetRate(string countryCode)// use 'GBP' for British Pounds { string rtnTxt = ""; try { string url = "http://rss.timegenie.com/forex.xml"; string proxyUrl = "http://xxx.xxx.xx:8080/"; string myXratePath = "/forex/data/code[text()='" + countryCode + "']"; WebProxy wp = new WebProxy(proxyUrl, true); wp.Credentials = CredentialCache.DefaultCredentials; WebClient wc = new WebClient(); wc.Proxy = wp; MemoryStream ms = new MemoryStream(wc.DownloadData(url)); XmlTextReader rdr = new XmlTextReader(ms); XmlDocument doc = new XmlDocument(); doc.Load(rdr); rtnTxt = doc.SelectSingleNode(myXratePath).ParentNode.SelectSingleNode("rate").InnerXml; } catch (Exception ex) { rtnTxt = ex.Message; } return rtnTxt; } #endregion 
+1
source

Source: https://habr.com/ru/post/1335090/


All Articles