2014-05-11 51 views
0

我正在開發一個用於網頁抓取的WP 8應用程序。出於某些原因,我不知道爲什麼,我的應用程序在WP中崩潰。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); 
     } 
    } 

不過,我想獲得的電子郵件,URL和腳本也同時用HTML和可讀的文本(如果覺得這可能是一個問題嗎?)

另外我無法在我的Windows Phone 8模擬器中運行Internet(嘗試了很多東西仍然無法使用!),所以我需要在物理設備上檢查此問題,並且應用程序崩潰。

我選擇了網絡功能。 (請讓我知道是否需要其他能力)

請幫我一下,我在這裏錯過了什麼?

感謝,

問候,

魯曼

+0

哦天啊,再次用RegExp解析HTML:( –

+0

不用擔心它會適用於我的項目!:p 但是,這裏的問題是應用程序崩潰:( –

回答

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

這解決了我的問題,非常感謝:) –

0

@Olivier的答案應該爲你工作。關於模擬器上的互聯網,打開Network and Sharing Center並檢查是否有連接名爲Windows Phone Emulator Internal Switch或與此相關的內容。如果沒有,請從左側面板轉到Change Adapter Settings。在那裏再次尋找與Emulator相關的東西。右鍵單擊並啓用它,如果它被禁用。

這是我能想到的唯一原因。

相關問題