我们如何使用WP7 Mango阅读网页?

时间:2011-12-03 09:46:56

标签: windows-phone-7 httpwebrequest webclient

我正在尝试使用Windows Phone 7.1 SDK(Mango)使用WP7阅读网页。我有一个URL,我想阅读,然后在其上执行一些功能。我尝试使用HttpWebRequest和WebClient,但没有成功获得响应。有什么建议?更新:在下面找到我使用的代码。问题是我只能读取页面的部分响应。

    private void btnGo_Click(object sender, RoutedEventArgs e) 
    { 
        try 
        { 
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(txtURL.Text); //txtURL loads URL
            var result = (IAsyncResult)request.BeginGetResponse(ResponseCallback, request); 
        } 
        catch (Exception ex) 
        { 
            MessageBox.Show(ex.Message); 
        } 
    } 

    private static void ResponseCallback(IAsyncResult result) 
    { 
        try 
        { 
            var request = (HttpWebRequest)result.AsyncState; 
            var response = request.EndGetResponse(result); 

            using (var stream = response.GetResponseStream()) 
            using (var reader = new StreamReader(stream)) 
            { 

                var contents = reader.ReadToEnd(); 

                textToBeDisplayed = contents; 
                if (textToBeDisplayed != "") 
                { 
                    Deployment.Current.Dispatcher.BeginInvoke(() => 
                    { 
                        System.Diagnostics.Debug.WriteLine(textToBeDisplayed); 
                    }); 
                } 
            } 
        } 
        catch (Exception ex) 
        { 
            Deployment.Current.Dispatcher.BeginInvoke(() => 
            { 
                MessageBox.Show(ex.Message); 
            }); 
        } 
    } 

2 个答案:

答案 0 :(得分:2)

 WebClient wc = new WebClient();
 wc.DownloadStringCompleted += (s, e) =>
 {
      string Html = e.Result;
 };
 wc.DownloadStringAsync(new Uri("http://google.com"));

答案 1 :(得分:0)

我发现System.Diagnostics.Debug.WriteLine是罪魁祸首。我在代码中添加了一个断点,发现整个html标记在字符串中可用。出于某种原因,System.Diagnostics.Debug.WriteLine不会打印整个字符串。谢谢你的帮助。