簡體   English   中英

使用WebClient / HttpWebRequest - WP7從https檢索XML

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

我正在嘗試從服務器檢索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.

所以我從這里開始使用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;
        }
    }
}

我寧願認為你過於復雜。 下面是一個非常簡單的示例,它通過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();
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM