偽裝雖易測試不易之微信瀏覽器


前言

     眾所周知,微信項目html5界面的開發時間會相對較長(為什么這么說就不解釋了),以及在微信瀏覽器內會出現一些無法在web端檢測出的問題,筆者有了模擬微信瀏覽器的想法,google之后,發現有方法來偽裝瀏覽器,也就是利用瀏覽器的user Agent,(每款瀏覽器都有自己不同的user Agent ,而且通過user Agent可以判斷瀏 覽器版本、所用的 操作系統等參數,當用戶通過瀏覽器向服務器發起請求時,請求頭(header)中就會包含 User Agent,服務器端可以獲取該值)。

插件下載:

筆者以火狐為例來詳解如何模擬微信瀏覽器:
首先,下載火狐瀏覽器的User Agent Switcher 插件,下載地址:https://addons.mozilla.org/zh-CN/firefox/addon/user-agent-switcher/ 插件如圖:
                           
安裝成功后可在工具選項中看到Default User Agent選項,如下圖:
 
                              

獲取微信瀏覽器的 User Agent

                    
 
經筆者的測試, MicroMessenger  是微信瀏覽器特定的標識,所以上圖中的if語句即可判斷請求是否為微信瀏覽器發起。
注意一下上圖中的 agent,下文中要用到

搭建微信瀏覽器

筆者用自己的GT-I9300獲取的agent為: Mozilla/5.0 (Linux; U; Android 4.1.2; zh-cn; GT-I9300 Build/JZO54K) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30 MicroMessenger/5.2.380
打開Default User Agent下的
                                 
添加新的user agent 
                              
將user agent切換為剛建的GT-9300 進行測試:
                              
自己比較一下 www.baidu.com原來的樣子,如圖算是添加成功了。
 

模擬微信瀏覽器請求

這是筆者之前所用的方法,基本作用跟前文差不多,希望對各位有所幫助
 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 need.com";
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方式請求該URL
31           httpget.setHeader("User-Agent",userAgent );
32           try {
33                // 得到responce對象
34                HttpResponse responce = httpClient.execute(httpget);
35                // 返回碼
36                int returnCode = responce.getStatusLine().getStatusCode();
37                // 是200證明正常 其他就不對
38                if (returnCode== HttpStatus.SC_OK) {
39                     // 獲得相應實體
40                     HttpEntity entity = responce.getEntity();
41                     if (entity != null) {
42                          html = new String(EntityUtils.toString(entity));// 獲得html源代碼
43                     }
44                }
45           } catch (Exception e) {
46                System.out.println("出現出現異常");
47                e.printStackTrace();
48           } finally {
49                httpClient.getConnectionManager().shutdown();
50           }
51           return html;
52      }
53 }

總結

本文只是自我的一個總結,如果對你有所幫助是我的榮幸,文章不妥之處希望指正,大神勿噴請通過留言或關注微信公眾帳號codenewbie支持小八哥!若有不妥之處,歡迎指點。

轉帖請注明本文出自小八哥的博客(http://www.cnblogs.com/Codenewbie),請尊重他人的辛勤勞動成果,謝謝!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM