0

我需要用我的 WP7 应用程序下载整个桌面站点的 HTML 代码。我在正常的 Win32 应用程序中使用了以下代码,但在 WP 中不起作用。特别是,我GetReponseHtttpWebReqest. 而且我不能使用WebBrowser,因为它不加载桌面站点,只加载移动站点。有什么帮助吗?

        request = HttpWebRequest.Create(URL)
        response = request.GetResponse
        sr = New IO.StreamReader(response.GetResponseStream)

        Source = sr.ReadToEnd
4

1 回答 1

0

在 wp7 中只允许异步请求。也许 WebClient 对您来说是最简单的。

这是一个例子:

private void loadFeedButton_Click(object sender, System.Windows.RoutedEventArgs e)
{
    WebClient webClient = new WebClient();

    webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);

    webClient.DownloadStringAsync(new System.Uri("http://windowsteamblog.com/windows_phone/b/windowsphone/rss.aspx"));
}

// Event handler which runs after the feed is fully downloaded.
private void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
    if (e.Error != null)
    {
        Deployment.Current.Dispatcher.BeginInvoke(() =>
        {
            // Showing the exact error message is useful for debugging. In a finalized application, 
            // output a friendly and applicable string to the user instead. 
            MessageBox.Show(e.Error.Message);
        });
    }
    else
    {
       this.State["feed"] = e.Result;          
    }
}
于 2012-10-14T12:34:54.500 回答