Java 模拟登录新浪微博(Cookie)

最近需要对微博的数据进行一些收集, 首先从登录开始

1. 通过GET请求login

public static String login(String username, String password)
        throws MalformedURLException, IOException {
    username = Base64.encodeBase64String(username.replace("@", "%40")
            .getBytes());
    HttpURLConnection connection = (HttpURLConnection) new URL(
            "https://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.15)")
            .openConnection();
    connection.setDoOutput(true);
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Referer",
            "http://login.sina.com.cn/signup/signin.php?entry=sso");
    connection.setRequestProperty(
            "User-Agent",
            "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:54.0) Gecko/20100101 Firefox/54.0");
    connection.setRequestProperty("Content-Type",
            "application/x-www-form-urlencoded");
    DataOutputStream out = new DataOutputStream(connection.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 reader = new BufferedReader(new InputStreamReader(
            connection.getInputStream(), "gbk"));
    String line = null;
    StringBuilder builder = new StringBuilder();
    while ((line = reader.readLine()) != null) {
        builder.append(line).append("\n");
    }
    String res = null;
    try {
        res = builder.substring(builder.indexOf("https:"),
                builder.indexOf(",\"https:") - 1).replace("\\", "");
    } catch (Exception e) {
        res = "false";
    }
    return res;
}

2. 登陆后获取cookie

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 reader = new BufferedReader(new InputStreamReader(
            conn.getInputStream(), "gbk"));
    String line = null;
    StringBuilder builder = new StringBuilder();
    while ((line = reader.readLine()) != null) {
        builder.append(line).append("\n");
    }
    StringBuilder cookie = new StringBuilder();
    try {
        for (String s : conn.getHeaderFields().get("Set-Cookie")) {
            cookie.append(s.split(";")[0]).append(";");
        }

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

其实到这一步, 目的就已经达成了.  下面是通过cookie发送微博的代码, 已测

public static String sendWeiBoMessage(String message, String cookies)
        throws MalformedURLException, IOException {
    Date date = new Date();
    Long time = date.getTime();
    String url = "http://weibo.com/p/aj/v6/mblog/add?ajwvr=6&domain=100505&__rnd=" + time;
    HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
    conn.setDoInput(true);
    conn.setDoOutput(true);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Cookie", cookies);
    conn.setRequestProperty("Referer",
            "http://weibo.com/p/1005052568466544/home?from=page_100505_profile&wvr=6&mod=data&is_all=1");
    conn.setRequestProperty("X-Requested-With", "XMLHttpRequest");
    conn.setRequestProperty(
            "User-Agent",
            "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:54.0) Gecko/20100101 Firefox/54.0");
    conn.setRequestProperty("Content-Type",
            "application/x-www-form-urlencoded");
    DataOutputStream out = new DataOutputStream(conn.getOutputStream());
    out.writeBytes("location=page_100505_home&text="
            + URLEncoder.encode(message)
            + "&appkey=&style_type=1&pic_id=&tid=&pdetail=1005052568466544&rank=0&rankid=&pub_source=page_2&longtext=1&topic_id=1022:&pub_type=dialog&_t=0");
    out.flush();
    out.close();
    BufferedReader reader = new BufferedReader(new InputStreamReader(
            conn.getInputStream(), "gbk"));
    String line = null;
    StringBuilder builder = new StringBuilder();
    while ((line = reader.readLine()) != null) {
        builder.append(line).append("\n");
    }
    return builder.toString();
}


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值