我正在为 Windows Phone 7.1.1 开发应用程序
我HttpWebRequest
用来获取网站的HTML代码问题是,它总是获取“桌面”版本的HTML代码谷歌后,我发现它是由UserAgent字符串控制的
所以,我设置UserAgent
了HttpWebRequest
但它仍然没有得到“移动”版本的 HTML 代码
这是我的代码
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);
这是 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
响应始终与根本不是 UserAgent 相同(桌面版)