[英]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.