Sunday, January 13, 2013

HttpWebRequest.UserAgent is set, but not working in response

HttpWebRequest.UserAgent is set, but not working in response

I'm developing an App for Windows Phone 7.1.1

I'm using HttpWebRequest to get HTML code of a website The problem is, it's alsway get the "Desktop" version of HTML code After google, I find out it's controlled by UserAgent string

So, I set the UserAgent of HttpWebRequest

But it's still not get the "Mobile" version of HTML code

here is my code

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(link, UriKind.Absolute));         request.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0)";         request.BeginGetResponse(new AsyncCallback(ResponseReady), request); 

This is code for ResponseReady

private void ResponseReady(IAsyncResult asyncResult)     {         try         {             HttpWebRequest request = asyncResult.AsyncState as HttpWebRequest;             HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult);             Stream responseStream = response.GetResponseStream();             StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);              HtmlDocument htmlDocument = new HtmlDocument();              string result = reader.ReadToEnd();  blah blah blah 

The Response is always the same as not UserAgent at all (Desktop version)

Answers & Comments...

Answer: 1

My problems is solved

Just use WebViewer / WebBrowser control to load the link. Then use OuterHtml to get the HTML Code of that website. Really simple. You can set visibility of that control to Colapse to hide it on the screen

by : Tuan Tranhttp://stackoverflow.com/users/1244111




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog