我想从移动版网站
中检索html源代码String url = "m.kitco.com";
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
HttpResponse response = client.execute(request);
InputStream in = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
然而,我可以得到的源代码只是完整的网站源代码而不是移动版本。似乎Web服务器会自动将链接转发到完整站点。
如何强制它检索移动网站的html源代码?
答案 0 :(得分:3)
您需要添加Android用户代理,例如
request.setHeader("User-Agent", "Mozilla/5.0 (Linux; U; Android 4.0.3; ko-kr; LG-L160L Build/IML74K) AppleWebkit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30");