模拟微信浏览器.docVIP

  • 9
  • 0
  • 约2.66千字
  • 约 6页
  • 2017-01-01 发布于重庆
  • 举报
模拟微信浏览器

伪装虽易测试不易之微信浏览器 前言 html5界面的开发时间会相对较长(为什么这么说就不解释了),以及在微信浏览器内会出现一些无法在web端检测出的问题,笔者有了模拟微信浏览器的想法,google之后,发现有方法来伪装浏览器,也就是利用浏览器的user Agent,(每款浏览器都有自己不同的user Agent ,而且通过user Agent可以判断浏览器版本、所用的操作系统等参数,当用户通过浏览器向服务器发起请求时,请求头(header)中就会包含User Agent,服务器端可以获取该值)。 插件下载: 安装成功后可在工具选项中看到Default?User Agent选项,如下图:?? ? ? ? ? ? ? ? ? ? ? ? ? ? ??? 获取微信浏览器的 User Agent?经笔者的测试,MicroMessenger?是微信浏览器特定的标识,所以上图中的if语句即可判断请求是否为微信浏览器发起。注意一下上图中的agent,下文中要用到 搭建微信浏览器 添加新的user agent?? ? ? ? ? ? ? ? ? ? ? ? ? ? ??? 将user agent切换为刚建的GT-9300 进行测试:???? ??????????????????????????? 自己比较一下原来的样子,如图算是添加成功了。? 模拟微信浏览器请求 1 package sedion.wq.MonitorWechattest; 2 3 import org.apache.http.HttpEntity; 4 import org.apache.http.HttpResponse; 5 import org.apache.http.HttpStatus; 6 import org.apache.http.client.HttpClient; 7 import org.apache.http.client.methods.HttpGet; 8 import org.apache.http.impl.client.DefaultHttpClient; 9 import org.apache.http.util.EntityUtils;10 11 /**12 * 模拟微信浏览器请求13 */14 public class MonitorWechatBrowser {15 public static void main(String[] args) {16 String url = http://www.where is your ;17 String userAgent=Mozilla/5.0 (Linux; U; Android 4.1.2; zh-cn; GT-I9300 Build/JZO54K) +18 AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30 MicroMessenger/5.2.380;19 String html = getHttpClientHtml(url, UTF-8);20 System.out.println(html);21 }22 23 24 /**25 * 根据URL获得所有的html信息26 */27 public static String getHttpClientHtml(String url,String code,String userAgent) {28 String html = null;29 HttpClient httpClient = new DefaultHttpClient();// 创建httpClient对象30 HttpGet httpget = new HttpGet(url);// 以get方式请求该URL31 httpget.setHeader(User-Agent,userAgent );32 try {33 // 得到responce对象34 HttpResponse responce = httpClient.execute(httpget);35 // 返回码36 int returnCode = responce.getStatusL

文档评论(0)

1亿VIP精品文档

相关文档