Question

I am developing a WP 8 app for web scraping. For some reasons I don't know why, my app crashes in WP.

Here is my sample code:

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

However I am trying to get Emails, URLs and Scripts also at the same time with HTML and Readable text (so if that might be a problem ?)

Also I am not able to run Internet in my Windows Phone 8 Emulator (tried many things still doesn't work !) so I need to check this on physical device, and on that the application is crashing.

I have selected Networking Capability. (Please let me know if any other capability is required)

Please help me out, what am I missing here ?

Thanks,

Regards,

Rumman

Was it helpful?

Solution

That's probably because you are interacting with UI elements on a non-UI thread (the response callback).

Try to put the code interacting with UI elements in a Dispatcher.BeginInvoke method, like so:

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

OTHER TIPS

Answer of @Olivier should work for you. Regarding Internet on Emulator, open Network and Sharing Center and check if there is a connection named Windows Phone Emulator Internal Switch or something related to this. If not, go to Change Adapter Settings from left panel. Again look for something related to Emulator there. Right click and Enable it if it is disabled.

This is the only reason i can think of.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top