Margaret Dax
Margaret Dax

Reputation: 57

Windows Phone 7 download pure HTML as string?

I have been trying to get a clear answer on how to download a page's HTML as a string for months, but have made zero progress. I can mess with all of the parsing myself, and I'll be able to figure out where it needs to fit into the application as I mess around with it. I would be very very grateful if someone could give me a clear code block to download a page's HTML as a string from a given url.

Upvotes: 3

Views: 1979

Answers (3)

Mick N
Mick N

Reputation: 14882

Here's a project I posted that demonstrated two ways of downloading html as a string and the relative merits of each.

WebClient, HttpWebRequest and the UI Thread on Windows Phone 7

HttpWebRequest gives you a nice UI performance benefit for a little extra work.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1501163

Well the simplest way is to use WebClient:

WebClient client = new WebClient();
client.DownloadStringCompleted += YourEventHandler;
client.DownloadStringAsync(uri);

where the event handler then takes whatever action it needs to.

However, it's worth bearing in mind that WebClient does rather a lot of work on the UI thread (despite the "async" part) - if this is for a production app, you probably want to use WebRequest directly. That's significantly more work (you end up with a Stream from the WebResponse, so you need to use the appropriate string encoding and construct a StreamReader around it in order to read a string).

Upvotes: 2

selbie
selbie

Reputation: 104569

System.Net.HttpWebRequest.Create(url);

docs are here

Upvotes: 1

Related Questions