我正在开发一个用于网页抓取的WP 8应用程序。出于某些原因,我不知道为什么,我的应用程序崩溃了。
以下是我的示例代码:
private void Load(object sender, RoutedEventArgs e)
{
try
{
HttpWebRequest req = (HttpWebRequest)WebRequest.CreateHttp("http://" + "www.google.com");
req.BeginGetResponse(new AsyncCallback(ResponseCallback), req);
}
catch (WebException ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK);
}
}
private async void ResponseCallback(IAsyncResult asyncres)
{
try
{
HttpWebRequest wreq = (HttpWebRequest)asyncres.AsyncState;
HttpWebResponse wres = (HttpWebResponse)wreq.EndGetResponse(asyncres);
StreamReader sr = new StreamReader(wres.GetResponseStream());
string result = await sr.ReadToEndAsync();
//HTML View
HTML.Text = result;
//Readable
string read;
read = Regex.Replace(result, "<script.*?</script>", "", RegexOptions.Singleline | RegexOptions.IgnoreCase);
read = Regex.Replace(read, "<style.*?</style>", "", RegexOptions.Singleline | RegexOptions.IgnoreCase);
read = Regex.Replace(read, "</?[a-z][a-z0-9]*[^<>]*>", "");
read = Regex.Replace(read, "<!--(.|\\s)*?-->", "");
read = Regex.Replace(read, "<!(.|\\s)*?>", "");
read = Regex.Replace(read, "[\t\r\n]", " ");
readable.Text = read;
}
catch (WebException ex)
{
//MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK);
}
}
但是我试图通过HTML和可读文本同时获取电子邮件,URL和脚本(所以如果这可能是个问题?)
此外,我无法在我的Windows Phone 8仿真器中运行Internet(尝试了很多事情仍然无法正常工作!)所以我需要在物理设备上检查这一点,并且应用程序崩溃了。
我选择了网络功能。 (如果需要任何其他功能,请告诉我)
请帮帮我,我在这里缺少什么?
谢谢,
此致
鲁曼
答案 0 :(得分:0)
这可能是因为您正在与非UI线程(响应回调)上的UI元素进行交互。
尝试将代码与UI元素交互放在Dispatcher.BeginInvoke
方法中,如下所示:
private async void ResponseCallback(IAsyncResult asyncres)
{
try
{
HttpWebRequest wreq = (HttpWebRequest)asyncres.AsyncState;
HttpWebResponse wres = (HttpWebResponse)wreq.EndGetResponse(asyncres);
StreamReader sr = new StreamReader(wres.GetResponseStream());
string result = await sr.ReadToEndAsync();
//HTML View
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
HTML.Text = result;
});
//Readable
string read;
read = Regex.Replace(result, "<script.*?</script>", "", RegexOptions.Singleline | RegexOptions.IgnoreCase);
read = Regex.Replace(read, "<style.*?</style>", "", RegexOptions.Singleline | RegexOptions.IgnoreCase);
read = Regex.Replace(read, "</?[a-z][a-z0-9]*[^<>]*>", "");
read = Regex.Replace(read, "<!--(.|\\s)*?-->", "");
read = Regex.Replace(read, "<!(.|\\s)*?>", "");
read = Regex.Replace(read, "[\t\r\n]", " ");
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
readable.Text = read;
});
}
catch (WebException ex)
{
//MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK);
}
}
答案 1 :(得分:0)
@Olivier的答案应该适合你。关于模拟器上的Internet,打开Network and Sharing Center
并检查是否存在名为Windows Phone Emulator Internal Switch
的连接或与此相关的内容。如果没有,请从左侧面板转到Change Adapter Settings
。再次寻找与Emulator
相关的内容。右键单击并启用它(如果已禁用)。
这是我能想到的唯一原因。