简体   繁体   English

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

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

I have the following code to load an XML doc from a website (hosted by one.com). 我有以下代码从网站(由one.com托管)加载XML文档。 Issue: i get an error "Unable to connect to the remote server". 问题:我收到一个错误“无法连接到远程服务器”。 I checked several posted regarding the same error message, but the suggestions don't work. 我检查了几条关于同一错误消息的帖子,但建议不起作用。 If I enter the URL in my web browser it views the XML file. 如果在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

First download the XML data and then load them in the XmlDocument object 首先下载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

this is because there must be proxy set in your browser and while accessing the xml file using your code you are not using proxy. 这是因为必须在浏览器中设置代理,并且在使用代码访问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);

Thanks for the answers. 感谢您的回答。 I've tried both, but it still didn't work. 我都尝试过,但仍然没有用。 I had a chat with an operator of one.com and it appears they don't support asp, .NET, C#. 我与one.com的运营商聊天,看来他们不支持asp,.net,C#。 So, that's why neither of the above codes are running. 因此,这就是以上两个代码均未运行的原因。

Thanks anyway for the effort. 无论如何,谢谢您的努力。

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

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