简体   繁体   English

使用WebClient / HttpWebRequest - WP7从https检索XML

[英]Retrieve XML from https using WebClient/HttpWebRequest - WP7

I'm trying to retrieve an XML document from a server and store it locally as a string. 我正在尝试从服务器检索XML文档并将其作为字符串存储在本地。 In desktop .Net I didn't need to, I just did: 在桌面.Net我不需要,我只是做了:

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

However on WP7 this returns: 但是在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.

So I set about using the WebClient/HttpWebRequest example from here , but now it returns: 所以我从这里开始使用WebClient / HttpWebRequest示例,但现在它返回:

The remote server returned an error: NotFound.

Is it because the XML is a https path? 是因为XML是https路径吗? Or because my path doesn't end in .XML? 或者因为我的路径没有以.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. 下面是一个非常简单的示例,它通过HTTPS从URI请求XML文档。

It downloads the XML asynchronously as a string and then uses XDocument.Parse() to load it. 它以字符串形式异步下载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();
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM