新浪微博模拟登陆 java

新浪微博模拟登陆 java

第一篇博客把经历记录下来,最近要做微博模拟登陆,网上找了大量的代码登陆都很繁琐,终于皇天不负有心人,找到了段简单的代码,也学习了。

package us.codecraft.jobhunter;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

import org.apache.commons.codec.binary.Base64;

public class weiboLogin
{
    public static void main(String[] args) throws Exception {
        System.err.println("开始登陆,获取tiket");
        // 设置微博用户名以及密码
        String ticket = requestAccessTicket("微博账号", "密码");
        if (ticket != "false") {
            System.err.println("获取成功:" + ticket);
            System.err.println("开始获取cookies");
            String cookies = sendGetRequest(ticket, null);
            System.err.println("cookies获取成功:" + cookies);
            System.err.println("开始发送微博");
            sendWeiBoMessage("java robot by iswin", cookies);
            System.err.println("发送微博成功");
        } else
            System.err.println("ticket获取失败,请检查用户名或者密码是否正确!");

    }

    public static String sendGetRequest(String url, String cookies)
            throws MalformedURLException, IOException {
        HttpURLConnection conn = (HttpURLConnection) new URL(url)
                .openConnection();
        conn.setRequestProperty("Cookie", cookies);
        conn.setRequestProperty("Referer",
                "http://login.sina.com.cn/signup/signin.php?entry=sso");
        conn.setRequestProperty(
                "User-Agent",
                "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:34.0) Gecko/20100101 Firefox/34.0");
        conn.setRequestProperty("Content-Type",
                "application/x-www-form-urlencoded");
        BufferedReader read = new BufferedReader(new InputStreamReader(
                conn.getInputStream(), "gbk"));
        String line = null;
        StringBuilder ret = new StringBuilder();
        while ((line = read.readLine()) != null) {
            ret.append(line).append("\n");
        }
        StringBuilder ck = new StringBuilder();
        try {
            for (String s : conn.getHeaderFields().get("Set-Cookie")) {
                ck.append(s.split(";")[0]).append(";");
            }

        } catch (Exception e) {
        }
        return ck.toString();
    }

    public static String requestAccessTicket(String username, String password)
            throws MalformedURLException, IOException {
        username = Base64.encodeBase64String(username.replace("@", "%40")
                .getBytes());
        HttpURLConnection conn = (HttpURLConnection) new URL(
                "https://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.15)")
                .openConnection();
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Referer",
                "http://login.sina.com.cn/signup/signin.php?entry=sso");
        conn.setRequestProperty(
                "User-Agent",
                "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:34.0) Gecko/20100101 Firefox/34.0");
        conn.setRequestProperty("Content-Type",
                "application/x-www-form-urlencoded");
        DataOutputStream out = new DataOutputStream(conn.getOutputStream());
        out.writeBytes(String
                .format("entry=sso&gateway=1&from=null&savestate=30&useticket=0&pagerefer=&vsnf=1&su=%s&service=sso&sp=%s&sr=1280*800&encoding=UTF-8&cdult=3&domain=sina.com.cn&prelt=0&returntype=TEXT",
                        URLEncoder.encode(username), password));
        out.flush();
        out.close();
        BufferedReader read = new BufferedReader(new InputStreamReader(
                conn.getInputStream(), "gbk"));
        String line = null;
        StringBuilder ret = new StringBuilder();
        while ((line = read.readLine()) != null) {
            ret.append(line).append("\n");
        }
        String res = null;
        try {
            res = ret.substring(ret.indexOf("https:"),
                    ret.indexOf(",\"https:") - 1).replace("\\", "");
        } catch (Exception e) {
            res = "false";
        }
        return res;
    }

    @SuppressWarnings("deprecation")
    public static String sendWeiBoMessage(String message, String cookies)
            throws MalformedURLException, IOException {
        HttpURLConnection conn = (HttpURLConnection) new URL(
                "http://www.weibo.com/aj/mblog/add?ajwvr=6").openConnection();
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Cookie", cookies);
        conn.setRequestProperty("Referer",
                "http://www.weibo.com/u/2955825224/home?topnav=1&wvr=6");
        conn.setRequestProperty("X-Requested-With", "XMLHttpRequest");
        conn.setRequestProperty(
                "User-Agent",
                "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:34.0) Gecko/20100101 Firefox/34.0");
        conn.setRequestProperty("Content-Type",
                "application/x-www-form-urlencoded");
        DataOutputStream out = new DataOutputStream(conn.getOutputStream());
        out.writeBytes("location=v6_content_home&appkey=&style_type=1&pic_id=&text="
                + URLEncoder.encode(message)
                + "&pdetail=&rank=0&rankid=&module=stissue&pub_type=dialog&_t=0");
        out.flush();
        out.close();
        BufferedReader read = new BufferedReader(new InputStreamReader(
                conn.getInputStream(), "gbk"));
        String line = null;
        StringBuilder ret = new StringBuilder();
        while ((line = read.readLine()) != null) {
            ret.append(line).append("\n");
        }
        return ret.toString();
    }
}

原文出处:http://www.iswin.org/a/40

你可能感兴趣的:(java,java,文章标题)