首页 > 代码库 > Java 猫扑(mop)打卡小应用

Java 猫扑(mop)打卡小应用

唉 mop又没打卡,前面十几天全没啦,像我们这些IT码农虽然天天上网,但是总是忘记打卡,这不一失足生成千古恨,失败了撒。好不容易每次打卡都能得几百份的,唉。
?1. [代码][Java]代码   
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){http://www.huiyi8.com/bgm/?
            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);
    }
 
}