开发者

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.


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).


System.Net.HttpWebRequest.Create(url);

docs are here


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜