360每日自動簽到,領取積分 (java httpclient4.x)

如何登錄360,並每日自動簽到
此次的難點主要集中在登錄這裏了,開始抓包發現360登錄驗證很麻煩,可是後來發現一個簡單的方法。
由於我安裝了360安全衛士,發現點擊那個金幣的按鈕能直接驗證登錄,哈哈~因此httpclient直接get這個url就能建立cookie完成登錄了~
上面說的不明白,看圖:html

  1 import java.io.IOException;
  2 import java.util.Map;
  3 import java.util.regex.Matcher;
  4 import java.util.regex.Pattern;
  5  
  6 import net.sf.json.JSONObject;
  7  
  8 import org.apache.http.Header;
  9 import org.apache.http.HttpEntity;
 10 import org.apache.http.HttpResponse;
 11 import org.apache.http.client.ClientProtocolException;
 12 import org.apache.http.client.methods.HttpGet;
 13 import org.apache.http.client.methods.HttpPost;
 14 import org.apache.http.client.params.CookiePolicy;
 15 import org.apache.http.client.params.HttpClientParams;
 16 import org.apache.http.impl.client.DefaultHttpClient;
 17 import org.apache.http.util.EntityUtils;
 18 import org.apache.log4j.Logger;
 19  
 20 import com.kae.common.CustomerHttpClient;
 21 import com.kae.common.DummyRedirectHandler;
 22  
 23 /**
 24  * 
 25  * @ClassName: Jifen360
 26  * @Description: 360積分
 27  * @author kaedeen.com
 28  * @date 2013-11-22 下午9:28:24
 29  * 
 30  */
 31  
 32 public class Jifen360 {
 33      
 34     private static final Logger logger = Logger.getLogger(Jifen360.class);
 35      
 36     private static DefaultHttpClient httpclient = null;// HttpClient對象
 37  
 38     private static HttpResponse response = null;
 39  
 40     private String url = "";
 41  
 42     public Jifen360(String url) {
 43         this.url = url;
 44     }
 45  
 46     /**
 47      * 簽到
 48      * 
 49      * @return
 50      */
 51     @SuppressWarnings({ "deprecation","rawtypes" })
 52     public boolean signIn() {
 53         boolean flag = false;
 54  
 55         httpclient = new DefaultHttpClient();
 56         // 設定cookie策略
 57         HttpClientParams.setCookiePolicy(httpclient.getParams(),
 58                 CookiePolicy.BROWSER_COMPATIBILITY);
 59  
 60         httpclient.setRedirectHandler(new DummyRedirectHandler());
 61  
 62         HttpGet httpGet = new HttpGet(url);
 63  
 64         try {
 65             response = httpclient.execute(httpGet);
 66         } catch (ClientProtocolException e) {
 67             // TODO Auto-generated catch block
 68             logger.error("Jifen360 signIn ClientProtocolException:"+e.toString());
 69         } catch (IOException e) {
 70             // TODO Auto-generated catch block
 71             logger.error("Jifen360 signIn IOException:"+e.toString());
 72         } finally {
 73             httpGet.abort();
 74         }
 75  
 76         String token = getToken();
 77  
 78         // 請求籤到連接
 79         String signInUrl="http://jifen.360.cn/safe/ajax_signin.html?token="+token;
 80          
 81         HttpPost httppost = new HttpPost(signInUrl);
 82         httppost.setHeader("Accept", "*/*");
 83         httppost.setHeader("Accept-Language","zh-CN,zh;q=0.8,en;q=0.6,ja;q=0.4,zh-TW;q=0.2");
 84         httppost.setHeader("Content-Type", "application/x-www-form-urlencoded");
 85         httppost.setHeader("Host", "jifen.360.cn");
 86         httppost.setHeader("Origin", "http://jifen.360.cn");
 87         httppost.setHeader("Proxy-Connection", "keep-alive");
 88         httppost.setHeader("Referer", "http://jifen.360.cn/safe?src=safe");
 89         httppost.setHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36");
 90         httppost.setHeader("X-Requested-With", "XMLHttpRequest");
 91          
 92         try {
 93             //獲取簽到應答內容
 94             response = httpclient.execute(httppost);
 95             HttpEntity httpEntity = response.getEntity();
 96             String responseJsonStr = EntityUtils.toString(httpEntity);
 97              
 98             //請求示例
 99             //{"errno":0,"errmsg":"","data":{"dayslen":1,"score":2,"signin_count":608904}} 成功
100             //{"errno":1,"errmsg":"\u60a8\u4eca\u5929\u5df2\u7ecf\u7b7e\u8fc7\u5230\u4e86~","data":""} 失敗
101              
102             logger.info("Jifen360 請求籤到連接 responseJsonStr:"+responseJsonStr);
103              
104             Map map = JSONObject.fromObject(responseJsonStr);
105             int code=(Integer)map.get("errno");
106              
107             if(0==code){
108                 flag=true;
109                 logger.info("Jifen360 簽到 成功!");
110             }else{
111                 logger.info("Jifen360 今天已經簽到了!");
112             }
113              
114         }catch (Exception e) {
115             logger.error("Jifen360 signIn 請求籤到連接 失敗:"+e.toString());
116             return false;
117         } finally {
118             httppost.abort();
119         }
120          
121          
122         return flag;
123     }
124  
125     /**
126      * 獲取302跳轉的
127      * 
128      * @return
129      */
130     private String getRedirectLocation() {
131         Header locationHeader = response.getFirstHeader("Location");
132         if (locationHeader == null) {
133             return null;
134         }
135         return locationHeader.getValue();
136     }
137  
138     /**
139      * 獲取360 token
140      * 
141      * @return
142      */
143     private String getToken() {
144  
145         String redirectUrl = getRedirectLocation();
146  
147         String token = "";
148  
149         String responseBody = CustomerHttpClient.get(httpclient, redirectUrl);
150         // System.out.println(responseBody);
151  
152         Pattern pattern1 = Pattern.compile("token\":\"(.*)\",\"mall_tpl");
153         Matcher m1 = pattern1.matcher(responseBody);
154         if (m1.find()) {
155             token = m1.group(1);
156             logger.info("Jifen360 360 token:"+token);
157         } else {
158             logger.error("Jifen360 360 獲取360 token失敗!");
159         }
160  
161         return token;
162     }
163  
164 }

相關文章
相關標籤/搜索