c#加载XML:无法连接到远程服务器

本文关键字:服务器 连接 加载 XML | 更新日期: 2023-09-27 18:11:46

我有以下代码从网站(由one.com托管)加载XML文档。问题:我得到一个错误"无法连接到远程服务器"。我检查了几个关于相同错误信息的帖子,但建议不起作用。如果我在web浏览器中输入URL,它将查看XML文件。

public partial class WebForm1 : System.Web.UI.Page
    {
            private XmlDocument dbKAA;
            private XmlElement root;
    public WebForm1()
    {
    }
        protected void Page_Load(object sender, EventArgs e)
        {
            //LOAD XML
            XmlDocument dbKAA = new XmlDocument();
            dbKAA.Load("http://www.something.com/XMLfile.xml");
            root = dbKAA.DocumentElement

c#加载XML:无法连接到远程服务器

首先下载XML数据,然后将它们加载到XmlDocument对象

    HttpClient client = new HttpClient();
    string url = "http://(urlHere)";
    HttpResponseMessage response = await client.GetAsync(url);
    string xmlData = await response.Content.ReadAsStringAsync();
    XmlDocument dbKAA = new XmlDocument();
    dbKAA.Load(xmlData);
    root = dbKAA.DocumentElement

这是因为在您的浏览器中必须设置代理,而在使用您的代码访问XML文件时您没有使用代理。

WebProxy webpro = new WebProxy(ProxyAddress);
webpro.Credentials = new NetworkCredential(ProxyUID, ProxyPwd);
WebClient wclient = new WebClient(){ Proxy =webpro};
MemoryStream mstream = new MemoryStream(wc.DownloadData("http://www.something.com/XMLfile.xml"));
XmlTextReader xtr = new XmlTextReader(mstream);
XDoc = XDocument.Load(xtr);

谢谢你的回答。我两种都试过了,但还是不行。我和one.com的一个运营商聊了聊,他们似乎不支持asp、。net和c#。所以,这就是为什么上面两个代码都没有运行的原因。

谢谢你的努力。