Jash
Jash

Reputation: 962

How to open a Web Page in Windows Phone 8 and get the HTML?

I am new to Windows Phone 8 Programming. I have a requirement where I want to load an Web page in Windows Phone 8 and then read its HTML content for Parsing. I am currently using WebBrowser control to navigate to a expected URI, but i am not able to fetch its HTML.

Code -

WebBrowser browser = new WebBrowser();
browser.Navigate(new Uri("http://XYZ.com"));

Note: I am not creating any Windows Phone APP. Basically this is a Unit test. And i want to test a website, which can be opened in IE in Windows Phone 8.

Any help is much appreciated.

Thanks in advance.

Upvotes: 3

Views: 4820

Answers (3)

computingfreak
computingfreak

Reputation: 5129

Alternatively, you can use HTTPRequest and HTTPResponse as a safe way to test.

Upvotes: 0

Jash
Jash

Reputation: 962

Finally got a solution, but not sure whether this is the most efficient way.

We need to register a event - LoadCompleted and then we can call the SaveToString() method which stores the HTML of web page in string

Code -

private void button1_Click(object sender, RoutedEventArgs e)
{
    Deployment.Current.Dispatcher.BeginInvoke(() =>
        {
            string site;
            site = textBox1.Text;
            webBrowser1.Navigate(new Uri(site, UriKind.Absolute));
            webBrowser1.LoadCompleted += webBrowser1_LoadCompleted;
        });
}

private void webBrowser1_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
        {
            string s = webBrowser1.SaveToString();
        }

We can then convert the string to HTML document which should be easy enough.

Upvotes: 2

MattD
MattD

Reputation: 4420

For just general C#, you can pull the HTML code into a string like this, which you can then parse through:

WebClient client = new WebClient();
String htmlCode = client.DownloadString("http://XYZ.com");

Upvotes: 2

Related Questions