我想读一些外部xml,但我必须通过代理连接,但我不知道该怎么做。我有以下代码,xmlurl
包含外部xml的路径:
if (content > 0)
{
using (XmlTextReader xml = new XmlTextReader(xmlurl))
{
while (xml.Read())
{
Console.WriteLine(xml.Name);
}
}
}
我有另外的HttpWebRequest
件代码:
if(Convert.ToBoolean(ConfigurationManager.AppSettings["behindproxy"]) == true)
{
WebProxy proxy = new WebProxy();
Uri proxyUri = new Uri("srvisa01");
proxy.Address = proxyUri;
proxy.BypassProxyOnLocal = true;
proxy.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["proxyusername"].ToString(), ConfigurationManager.AppSettings["proxypassword"].ToString());
}
但我不确定这会在这种情况下有效。
帮助表示赞赏。
亲切的问候
克里斯
答案 0 :(得分:4)
我通过一些坚定的谷歌搜索找到了这个问题的答案:
XmlTextReader xml;
WebRequest web;
web = WebRequest.Create(xmlurl);
if(Convert.ToBoolean(ConfigurationManager.AppSettings["behindproxy"].ToString()))
{
WebProxy prxy = new WebProxy();
Uri prxyUri = new Uri("http://xxx:8080");
prxy.Address = prxyUri;
prxy.BypassProxyOnLocal = true;
prxy.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["proxyusername"].ToString(), ConfigurationManager.AppSettings["proxypassword"].ToString());
web.Proxy = prxy;
}
var response = web.GetResponse().ToString();
xml = new XmlTextReader(response);
希望这个答案有助于窥视:)