- package com.mop.core;
- import java.io.IOException;
- import java.net.URISyntaxException;
- import java.util.HashMap;
- import java.util.Map;
- import org.apache.http.HttpException;
- import org.apache.http.client.HttpClient;
- import org.apache.http.impl.client.DefaultHttpClient;
- import com.mop.util.HttpUtil;
- /**
- * 猫扑打卡小应用
- *
- * @author jeson.sha
- * @website www.ij2ee.com
- */
- public class Mop {
- private static final String LOGIN_URL = "http://passport.mop.com/Login?url=http://www.mop.com&charset=utf-8";
- private static final String USER_INFO = "http://passport.mop.com/common/user-info?callback=jsonp1338724243937";
- private static final String PUNCH = "http://passport.mop.com/punch-the-clock/punch?callback=jsonp1338724243939";
- private static final String CHARSET="utf-8";
- /**
- * @param args
- * @throws InterruptedException
- * @throws HttpException
- * @throws IOException
- * @throws URISyntaxException
- * @throws IllegalStateException
- */
- public static void main(String[] args) throws IllegalStateException, URISyntaxException, IOException, HttpException, InterruptedException {
- HttpClient client = new DefaultHttpClient();
- String userName = "猫家三少";
- Mop mop = new Mop();
- mop.login(client,userName,"111111");
- boolean isLogin = mop.getIsCanGetUserInfo(client,userName);
- if(!isLogin){
- System.out.println("登录失败");
- }
- if(mop.punch(client)){
- System.out.println("打卡成功");
- }else{
- System.out.println("打卡失败");
- }
- }
- /**
- * 打卡积分
- * 成功会返回 jsonp1338724243939({"mpPlused":20,"status":200}) 分数 和状态
- * 失败则返回 jsonp1338724243939({"status":404})
- * 区别就是 有没 mpPlused json属性
- * @param client
- * @return 是否打卡成功
- * @throws IllegalStateException
- * @throws URISyntaxException
- * @throws IOException
- * @throws HttpException
- * @throws InterruptedException
- */
- private boolean punch(HttpClient client) throws IllegalStateException, URISyntaxException, IOException, HttpException, InterruptedException {
- String punchRes = HttpUtil.doGet(client, PUNCH, CHARSET);
- return punchRes.indexOf("mpPlused")!=-1;
- }
- /**
- * 可否获取用户信息
- * 成功JSON则会出现登录名 所以这里就用是否存在用户名判断
- * @param client
- * @param userName
- * @return
- * @throws IllegalStateException
- * @throws URISyntaxException
- * @throws IOException
- * @throws HttpException
- * @throws InterruptedException
- */
- private boolean getIsCanGetUserInfo(HttpClient client,String userName) throws IllegalStateException, URISyntaxException, IOException, HttpException, InterruptedException {
- boolean isCanGetUserInfo = false;
- String res = HttpUtil.doGet(client, USER_INFO, CHARSET);
- isCanGetUserInfo = res.indexOf(userName)!=-1;
- return isCanGetUserInfo;
- }
- /**
- * 登录
- * 没有header 或header有误 则会导致登录失败
- * @param client
- * @param userName
- * @param password
- * @throws IllegalStateException
- * @throws URISyntaxException
- * @throws IOException
- * @throws HttpException
- * @throws InterruptedException
- */
- private void login(HttpClient client,String userName,String password) throws IllegalStateException, URISyntaxException, IOException, HttpException, InterruptedException{
- Map<String, String> param = new HashMap<String, String>();
- param.put("user_name", userName);
- param.put("password", password);
- Map<String, String> loginHeader = new HashMap<String, String>();
- loginHeader.put("Content-Type","application/x-www-form-urlencoded");
- loginHeader.put("User-Agent","Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; QQWubi 133; CIBA; .NET CLR 2.0.50727)");
- loginHeader.put("Host","passport.mop.com");
- loginHeader.put("Referer","http://www.mop.com/");
- loginHeader.put("Cookie","mopst_ssid=13387192652653136; mopst_ssid_time=1338719327796; base_domain_629695a417eb49ada2a6bc2f67b3f7ef=mop.com; mopst_unique=13387192636401978; mop_locale=0086320500; maxfxbidw2010=935; xnsetting_629695a417eb49ada2a6bc2f67b3f7ef=%7B%22connectState%22%3A2%2C%22oneLineStorySetting%22%3A3%2C%22shortStorySetting%22%3A3%2C%22shareAuth%22%3Anull%7D");
- String postRes = HttpUtil.doPost(client, LOGIN_URL, param, loginHeader,CHARSET);
- System.out.println(postRes);
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/220820135319.html
来源: http://www.codesnippet.cn/detail/220820135319.html