使用 WebClient/HttpWebRequest 从 https 检索 XML - WP7 [英] Retrieve XML from https using WebClient/HttpWebRequest - WP7

查看:17
本文介绍了使用 WebClient/HttpWebRequest 从 https 检索 XML - WP7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to retrieve an XML document from a server and store it locally as a string. In desktop .Net I didn't need to, I just did:

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

但是在 WP7 上返回:

However on WP7 this returns:

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.

所以我开始使用 这里的 WebClient/HttpWebRequest 示例,但现在它返回:

So I set about using the WebClient/HttpWebRequest example from here, but now it returns:

The remote server returned an error: NotFound.

是不是因为 XML 是 https 路径?还是因为我的路径不是以 .XML 结尾?我怎么知道?感谢您的帮助.

Is it because the XML is a https path? Or because my path doesn't end in .XML? How do I find out? Thanks for any help.

代码如下:

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

推荐答案

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

I rather think you've overcomplicated things. Below is a very simple example which requests an XML document from a URI over HTTPS.

它以字符串的形式异步下载 XML,然后使用 XDocument.Parse() 加载它.

It downloads the XML asynchronously as a string and then uses XDocument.Parse() to load it.

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

这篇关于使用 WebClient/HttpWebRequest 从 https 检索 XML - WP7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆