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

发布于 2024-12-19 11:16:42 字数 1563 浏览 2 评论 0原文

我正在尝试使用 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); 
            }); 
        } 
    } 

I'm trying to read a web page using WP7 using the Windows Phone 7.1 SDK (Mango). I have a URL which I would like to read and later perform some function on it. I tried using HttpWebRequest and WebClient but wasn't successful in obtaining a response. Any suggestions? Update: Find below the code I used. The issue is that I'm able to read just a partial response of the page.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

莫多说 2024-12-26 11:16:42
 WebClient wc = new WebClient();
 wc.DownloadStringCompleted += (s, e) =>
 {
      string Html = e.Result;
 };
 wc.DownloadStringAsync(new Uri("http://google.com"));
 WebClient wc = new WebClient();
 wc.DownloadStringCompleted += (s, e) =>
 {
      string Html = e.Result;
 };
 wc.DownloadStringAsync(new Uri("http://google.com"));
漫雪独思 2024-12-26 11:16:42

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

I figured out that System.Diagnostics.Debug.WriteLine is the culprit. I added a breakpoint to my code and found that the entire html markup is available in the string. For some reason, System.Diagnostics.Debug.WriteLine does not print the entire string. Thanks for the help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文