2010-08-11 84 views
4

我试图从服务器检索XML文档并将其作为字符串本地存储。在桌面的.Net我没必要,我只是做:使用WebClient/HttpWebRequest从https检索XML - WP7

 string xmlFilePath = "https://myip/"; 
     XDocument xDoc = XDocument.Load(xmlFilePath); 

但是在WP7这将返回:

Cannot open 'serveraddress'. The Uri parameter must be a relative path pointing to content inside the Silverlight application's XAP package. If you need to load content from an arbitrary Uri, please see the documentation on Loading XML content using WebClient/HttpWebRequest. 

所以我开始使用Web客户端/ HttpWebRequest的例子来自here,但现在它返回:

The remote server returned an error: NotFound. 

是因为XML是https路径吗?或者因为我的路径不以.XML结尾?我如何发现?谢谢你的帮助。

下面的代码:

public partial class MainPage : PhoneApplicationPage 
{ 
    WebClient client = new WebClient(); 
    string baseUri = "https://myip:myport/service"; 
    public MainPage() 
    { 
     InitializeComponent(); 
     client.DownloadStringCompleted += 
      new DownloadStringCompletedEventHandler(
      client_DownloadStringCompleted); 
    } 

    private void Button1_Click(object sender, RoutedEventArgs e) 
    { 
     client.DownloadStringAsync 
      (new Uri(baseUri)); 
    } 

    void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) 
    { 
     if (e.Error == null) 
      resultBlock.Text = "Using WebClient: " + e.Result; 

     else 
      resultBlock.Text = e.Error.Message; 
    } 

    private void Button2_Click(object sender, RoutedEventArgs e) 
    { 
     HttpWebRequest request = 
      (HttpWebRequest)HttpWebRequest.Create(new Uri(baseUri)); 
     request.BeginGetResponse(new AsyncCallback(ReadCallback), 
     request); 
    } 

    private void ReadCallback(IAsyncResult asynchronousResult) 
    { 
     HttpWebRequest request = 
      (HttpWebRequest)asynchronousResult.AsyncState; 
     HttpWebResponse response = 
      (HttpWebResponse)request.EndGetResponse(asynchronousResult); 
     using (StreamReader streamReader1 = 
      new StreamReader(response.GetResponseStream())) 
     { 
      string resultString = streamReader1.ReadToEnd(); 
      resultBlock.Text = "Using HttpWebRequest: " + resultString; 
     } 
    } 
} 
+0

如果您只是将代码中的网址剪切并粘贴到浏览器中,您会看到什么? – Lazarus 2010-08-11 11:50:16

+0

由Firefox正确格式化的XML页面。像这样:http://www.w3schools.com/xml/simple.xml – JoeBeez 2010-08-11 11:54:51

回答

14

我宁愿认为你过于复杂的事情。以下是一个非常简单的示例,它通过HTTPS从URI请求XML文档。

它以异步方式将XML下载为字符串,然后使用XDocument.Parse()加载它。

private void button2_Click(object sender, RoutedEventArgs e) 
    { 
     WebClient wc = new WebClient(); 
     wc.DownloadStringCompleted += HttpsCompleted; 
     wc.DownloadStringAsync(new Uri("https://domain/path/file.xml")); 
    } 

    private void HttpsCompleted(object sender, DownloadStringCompletedEventArgs e) 
    { 
     if (e.Error == null) 
     { 
      XDocument xdoc = XDocument.Parse(e.Result, LoadOptions.None); 

      this.textBox1.Text = xdoc.FirstNode.ToString(); 
     } 
    } 
+0

这几乎是我结束了,但异常仍然发生。这些代码在你那正常吗?如果是这样,我很确定它必须是我的HTTPS证书不可信。 – JoeBeez 2010-08-11 14:44:42

+0

@JoeBeez是的,讨厌这样说,但“在我的机器上工作”。这可能是一个证书问题。您的示例代码还包括指定一个端口。如果您将https指定为协议和非标准端口,则可能是问题所在。 (我知道HTTPS在使用其他平台上的默认端口时会遇到问题。) – 2010-08-11 15:31:04

+0

是的,当我回家时,端口将成为我的第二件事,为您的帮助而欢呼。 – JoeBeez 2010-08-11 15:54:36