文章目录

网上给的登陆新浪微博的例子过程都比较繁琐,抓包发现新浪有个接口只需要两步就可以完成,发出来大家分享下。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package org.iswin.weibo;

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;

/***
* @blog http://www.iswin.org
* @author iswin
*/
public class Main {
public static void main(String[] args) throws Exception {
System.err.println("开始登陆,获取tiket");
// 设置微博用户名以及密码
String ticket = requestAccessTicket("weibo@iswin.org", "123456");
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();
}
}
文章目录