我们如何使用WP7 Mango阅读网页?
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发现 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.