繁体   English   中英

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

[英]C# load XML: Unable to connect to the remote server

我有以下代码从网站(由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

首先下载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#。 因此,这就是以上两个代码均未运行的原因。

无论如何,谢谢您的努力。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM