【问题标题】:Retrieve XML from https using WebClient/HttpWebRequest - WP7使用 WebClient/HttpWebRequest 从 https 检索 XML - WP7
【发布时间】:2010-08-11 11:46:22
【问题描述】:

我正在尝试从服务器检索 XML 文档并将其作为字符串存储在本地。在桌面 .Net 中我不需要,我只是这样做了:

        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.

所以我开始使用来自here 的 WebClient/HttpWebRequest 示例,但现在它返回了:

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;
        }
    }
}

【问题讨论】:

  • 如果您只是将 URL 从代码中剪切并粘贴到浏览器中,您会看到什么?
  • 一个 XML 页面,由 Firefox 正确格式化。像这样:w3schools.com/xml/simple.xml

标签: c# .net xml windows-phone-7


【解决方案1】:

我宁愿认为你把事情复杂化了。下面是一个非常简单的示例,它通过 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();
        }
    }

【讨论】:

  • 这几乎就是我最终得到的结果,但是异常仍然发生。这些代码在你那正常吗?如果是这样,我很确定这一定是因为我的 HTTPS 证书不受信任。
  • @JoeBeez 是的,不想这么说,但“在我的机器上工作”。可能是证书问题。您的示例代码还包括指定端口。如果您将 https 指定为协议和非标准端口,这可能是问题所在。 (我知道 HTTPS 在使用其他平台上的默认端口以外的端口时会出现问题。)
  • 是的,当我回家欢呼您的帮助时,港口将是我检查的第二件事。
  • 但问题是,如果我们使用WebClient,它运行在UI线程中。最好选择 HttpWebRequest。
  • @Mahantesh - 因为 Mango,WebClient 在调用它的线程上返回。它不会强制对 UI 线程的响应,除非它从那里开始。以上是最简单形式的示例,而不是最佳实践。
猜你喜欢
  • 2011-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-10
  • 2014-02-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多