WP7 - 使用Web客户端/ HttpWebRequest的检索HTTPS XML [英] Retrieve XML from https using WebClient/HttpWebRequest - WP7

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

问题描述

我试图从服务器检索XML文档并在本地保存它作为一个字符串。在桌面的.Net我也没必要,我只是做了:

 字符串xmlFilePath =htt​​ps://开头MYIP /;
        XDOC的XDocument = XDocument.Load(xmlFilePath);
 

但是在WP7这个返回:

 无法打开serveraddress。 URI参数必须是指向Silverlight应用程序的XAP包里面的内容相对路径。如果您需要加载从任意开放的内容,请使用Web客户端/ HttpWebRequest的看到加载XML内容的文档。
 

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

 远程服务器返回错误:NOTFOUND。
 

是不是因为XML是一个HTTPS的路径?还是因为我的路径不以.XML结束?如何才能知道?感谢您的帮助。

这里的code:

 公共部分MainPage的类:PhoneApplicationPage
{
    Web客户端的客户端=新的Web客户端();
    字符串的基本URI =htt​​ps://开头MYIP:MyPort上/服务;
    MainPage的公()
    {
        的InitializeComponent();
        client.DownloadStringCompleted + =
            新DownloadStringCompletedEventHandler(
            client_DownloadStringCompleted);
    }

    私人无效的button1_Click(对象发件人,RoutedEventArgs E)
    {
        client.DownloadStringAsync
          (新的URI(基本URI));
    }

    无效client_DownloadStringCompleted(对象发件人,DownloadStringCompletedEventArgs E)
    {
        如果(e.Error == NULL)
            resultBlock.Text =使用Web客户端:+ e.Result;

        其他
            resultBlock.Text = e.Error.Message;
    }

    私人无效Button2_Click(对象发件人,RoutedEventArgs E)
    {
        HttpWebRequest的要求=
          (HttpWebRequest的)HttpWebRequest.Create(新的URI(基本URI));
        request.BeginGetResponse(新的AsyncCallback(ReadCallback)
        请求);
    }

    私人无效ReadCallback(IAsyncResult的asynchronousResult)
    {
        HttpWebRequest的要求=
          (HttpWebRequest的)asynchronousResult.AsyncState;
        HttpWebResponse响应=
          (HttpWebResponse)request.EndGetResponse(asynchronousResult);
        使用(StreamReader的streamReader1 =
          新的StreamReader(response.GetResponseStream()))
        {
            字符串resultString = streamReader1.ReadToEnd();
            resultBlock.Text =使用HttpWebRequest的:+ resultString;
        }
    }
}
 

解决方案

我倒觉得你过于复杂的事情。下面是一个非常简单的例子,从请求的URI的XML文档通过HTTPS。

据异步下载XML作为一个字符串,然后使用 XDocument.Parse()加载它。

 私人无效button2_Click(对象发件人,RoutedEventArgs E)
    {
        Web客户端WC =新的Web客户端();
        wc.DownloadStringCompleted + = HttpsCompleted;
        wc.DownloadStringAsync(新的URI(HTTPS://domain/path/file.xml));
    }

    私人无效HttpsCompleted(对象发件人,DownloadStringCompletedEventArgs E)
    {
        如果(e.Error == NULL)
        {
            XDOC的XDocument = XDocument.Parse(e.Result,LoadOptions.None);

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

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

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.

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

The remote server returned an error: NotFound.

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.

Here's the code:

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

解决方案

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

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

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

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