360每日自动签到,领取积分 (java httpclient4.x)
如何登陆360,并每日自动签到
这次的难点主要集中在登陆这里了,开始抓包发现360登陆验证很麻烦,但是后来发现一个简单的方法。
因为我安装了360安全卫士,发现点击那个金币的按钮能直接验证登陆,哈哈~所以httpclient直接get这个url就能创建cookie完成登陆了~
上面说的不明白,看图:
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 }
不定期会发布一些实用的Java开发文章