模拟浏览器的UserAgent获取信息
新手!大神莫喷!!
现在很多都要去模拟一些浏览器,比如手机浏览器,qq google 等。最近试了一下,为了方便以后查找和学习,故而写一下笔记
现在先要确定你要模拟的浏览器,获取该浏览器的UserAgent可以在该浏览器输入:http://www.atool.org/useragent.php来获取。也可以通过浏览器的F12 里面的网络来查看get或post,来查看
本人用的是Java,接下来就可以编写自己的代码了
Java代码,例如:
...(代码)
HttpClient httpClient = new DefaultHttpClient();// 创建httpClient对象
HttpGet httpget = new HttpGet(url);
httpget.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
httpget.setHeader("Accept-Charset", "GB2312,utf-8;q=0.7,*;q=0.7");
httpget.setHeader("Accept-Encoding", "gzip,deflate,sdch");
httpget.setHeader("Accept-Language", "zh-CN,zh;q=0.8,en;q=0.6");
httpget.setHeader("Cache-Control", "max-age=0");
httpget.setHeader("Connection", "keep-alive");
// httpget.setHeader("Cookie", "");
httpget.setHeader("Host", "你要获取的网站的host");
httpget.setHeader("User-Agent", "你浏览器的User-Agent");
HttpResponse response = httpClient.execute(httpget);
System.out.println(response.getStatusLine());
...(代码)
然后结合自己的需求就可以运行了!
写得不好的希望大家指正一下!多交流!