Flask接口签名sign原理与实例代码浅析
227
2023-07-10
java实现微博后台登录发送微博
首先你需要有个微博开发者账号,我们需要的是App Key和App Secre及redirect_URI,公司原本就有所以这一步 省下来了,百度搜下有很多。
好了现在开始:
首先我在网上找到了这个代码,这里写链接内容
本来挺高兴的一次性解决了嘛;可是报错 “token刷新失败”,看了下代码
返回的是 200 不是302 ,额有点懵了,不管了先研究研究为什么会这样吧,我将他生成的网址放到浏览器上看了下,进入的是授权页面,不会需要先登录。F12 看了下确实也是 200 ,于是我估计是不是因为没有登录成功的原因,于是开始做微博的后台登录
package com.spider.httpclient;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.cookie.CookieSpec;
import org.apache.http.cookie.CookieSpecProvider;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.cookie.DefaultCookieSpec;
import org.apache.http.impl.cookie.DefaultCookieSpecProvider;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
/**
* 新浪微博登录器,2016年4月7日08:30:16,密码加密方式采用RSA2,加密方式js内
*
* @author tom
*
*/
public class SinaWeiboLogger {
private HttpClient client;
private HttpPost post;
private HttpGet get;
private BasicCookieStore cookieStore;
private String username; // 明文账号
private String password; // 明文密码
private String su; // base64加密后账号
private String sp; // RSA加密后密码
// 预登陆信息 所需数据 开始
private long servertime; // 服务器时间戳
private String nonce; // 服务器返回字符串
private String rsakv; // 服务器返回字符串,rsa加密密码用
private String pubkey; // 服务器返回rsa加密公用密钥,用于rsa加密
private int retcode; //状态值
private String pcid;
private int is_openlock;
private int showpin;
private int exectime;
// 预登陆信息 结束
//登录后返回json数据
private String uid;
private String nick;
private JSONArray domainurls;
//
private StringBuffer headerCookie = new StringBuffer();
//登录密码加密js文件内容
private static String sina_js = "var sinaSSOEncoder=sinaSSOEncoder||{};(function(){var hexcase=0;var chrsz=8;this.hex_sha1=function(s){return binb2hex(core_sha1(str2binb(s),s.length*chrsz));};var core_sha1=function(x,len){x[len>>5]|=0x80<<(24-len%32);x[((len+64>>9)<<4)+15]=len;var w=Array(80);var a=1732584193;var b=-271733879;var c=-1732584194;var d=271733878;var e=-1009589776;for(var i=0;i
public SinaWeiboLogger(String username, String password) {
this.username = username;
this.password = password;
//cookie策略,不设置会拒绝cookie rejected,设置策略保存cookie信息
cookieStore = new BasicCookieStore();
CookieSpecProvider myCookie = new CookieSpecProvider() {
public CookieSpec create(HttpContext context) {
return new DefaultCookieSpec();
}
};
Registry
.build();
client = HttpClients.custom().setDefaultCookieStore(cookieStore).setDefaultCookieSpecRegistry(rg).build();
get = new HttpGet();
post = new HttpPost();
}
/**
* 预登陆,使用用户名base64加密后get请求,得到返回值,使用返回值加密用户密码再次请求
*
* @throws IOException
* @throws ClientProtocolException
* @throws URISyntaxException
*/
private void preLogin() throws ClientProtocolException, IOException, URISyntaxException {
// 1
su = new String(Base64.encodeBase64(URLEncoder.encode(this.username, "UTF-8").getBytes()));
String preLoginUrl = "http://login.sina.com.cn/sso/prelogin.php?entry=account&callback=sinaSSOController.preloginCallBack&su="
+ su + "&rsakt=mod&client=ssologin.js(v1.4.15)&_=" + new Date().getTime();
get.setURI(new URI(preLoginUrl));
get.addHeader("Host", "login.sina.com.cn");
get.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0");
get.addHeader("Accept", "*/*");
get.addHeader("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");
get.addHeader("Accept-Encoding", "gzip, deflate");
get.addHeader("Referer", "http://login.sina.com.cn/");
get.addHeader("Connection", "keep-alive");
HttpResponse resp = client.execute(get);
//保存请求返回cookie,登录后请求其他页面需要带cookie
saveCookie(resp.getAllHeaders(), this.headerCookie);
HttpEntity entity = resp.getEntity();
String cont = EntityUtils.toString(entity);
// System.out.println(cont);
cont = cont.replace("sinaSSOController.preloginCallBack(", "");
cont = cont.replace(")", "");
JSONObject json = JSONObject.parseObject(cont);
this.retcode = json.getIntValue("retcode");
this.servertime = json.getLongValue("servertime");
this.pcid = json.getString("pcid");
this.nonce = json.getString("nonce");
this.pubkey = json.getString("pubkey");
this.rsakv = json.getString("rsakv");
this.is_openlock = json.getIntValue("is_openlock");
this.showpin = json.getIntValue("showpin");
this.exectime = json.getIntValue("exectime");
}
private void saveCookie(Header[] headers, StringBuffer headerCookie) {
for(Header h : headers) {
if (h.getName().equals("Set-Cookie")) {
headerCookie.append(h.getValue()).append(";");
}
}
}
/**
* 登录新浪通行证 账号登录,加密密码,post请求
*
* @return
*
* @throws IOException
* @throws ClientProtocolException
* @throws ScriptException
* @throws NoSuchMethodException
* @throws URISyntaxException
*/
private void accountLogin() throws NoSuchMethodException, ScriptException, ClientProtocolException, IOException, URISyntaxException {
// 2
String loginUrl = "http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.15)&_="
+ new Date().getTime();
post.setURI(new URI(loginUrl));
post.addHeader("Host", "login.sina.com.cn");
post.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0");
post.addHeader("Accept", "*/*");
post.addHeader("Content-Type", "application/x-www-form-urlencoded");
post.addHeader("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");
post.addHeader("Accept-Encoding", "gzip, deflate");
post.addHeader("Referer", "http://login.sina.com.cn/");
post.addHeader("Connection", "keep-alive");
// 使用js加密密码,RSA,调用js内方法
ScriptEngineManager sem = new ScriptEngineManager();
ScriptEngine se = sem.getEngineByName("javascript");
se.eval(sina_js);
if (se instanceof Invocable) {
Invocable iv = (Invocable) se;
sp = (String) iv.invokeFunction("getpass", this.password, this.servertime, this.nonce,
this.pubkey);
}
List
parms.add(new BasicNameValuePair("entry", "account"));
parms.add(new BasicNameValuePair("gateway", "1"));
parms.add(new BasicNameValuePair("from", ""));
parms.add(new BasicNameValuePair("savestate", "0"));
parms.add(new BasicNameValuePair("useticket", "0"));
parms.add(new BasicNameValuePair("pagerefer", ""));
parms.add(new BasicNameValuePair("vsnf", "1"));
parms.add(new BasicNameValuePair("su", su));
parms.add(new BasicNameValuePair("service", "sso"));
parms.add(new BasicNameValuePair("servertime", servertime + ""));
parms.add(new BasicNameValuePair("nonce", nonce));
parms.add(new BasicNameValuePair("pwencode", "rsa2"));
parms.add(new BasicNameValuePair("rsakv", rsakv));
parms.add(new BasicNameValuePair("sp", sp));
parms.add(new BasicNameValuePair("sr", "1366*768"));
parms.add(new BasicNameValuePair("encoding", "UTF-8"));
parms.add(new BasicNameValuePair("cdult", "3"));
parms.add(new BasicNameValuePair("domain", "sina.com.cn"));
parms.add(new BasicNameValuePair("prelt", "51"));
parms.add(new BasicNameValuePair("returntype", "TEXT"));
post.setEntity(new UrlEncodedFormEntity(parms));
HttpResponse resp = client.execute(post);
//保存请求返回cookie,登录后请求其他页面需要带cookie
saveCookie(resp.getAllHeaders(), this.headerCookie);
//请求返回链接
JSONObject jsonObj = JSONObject.parseObject(EntityUtils.toString(resp.getEntity()));
System.out.println(jsonObj.toJSONString());
this.retcode = jsonObj.getIntValue("retcode");
this.uid = jsonObj.getString("uid");
this.nick = jsonObj.getString("nick");
this.domainurls = jsonObj.getJSONArray("crossDomainUrlList");
List
int i = 0;
for (Object url : domainurls) {
get.setURI(new URI(url.toString()+"&callback=sinaSSOController.doCrossDomainCallBack&scriptId=ssoscript"+i+"&client=ssologin.js(v1.4.15)&_="+new Date().getTime()));
resp = client.execute(get);
retMsg.add(EntityUtils.toString(resp.getEntity()));
i++;
}
get.setURI(new URI("http://login.sina.com.cn/member/my.php?entry=sso"));
//添加请求cookie到请求头
get.addHeader("Cookie", this.headerCookie.toString());
resp = client.execute(get);
String cont = readStreamByEncoding(resp.getEntity().getContent(), "GBK");
retMsg.add(cont);
for(String s : retMsg) {
System.out.println(s);
}
}
private String readStreamByEncoding(InputStream in, String encoding) throws IOException {
StringBuffer cont= new StringBuffer();
BufferedReader br = new BufferedReader(new InputStreamReader(in , encoding));
String tmp = "";
while ((tmp = br.readLine())!=null) {
cont.append(tmp);
}
br.close();
return cont.toString();
}
/**
* 登录
*/
public void login() {
try {
this.preLogin();
this.accountLogin();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (ScriptException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
public HttpClient getClient() {
return client;
}
public void setClient(HttpClient client) {
this.client = client;
}
public HttpPost getPost() {
return post;
}
public void setPost(HttpPost post) {
this.post = post;
}
public HttpGet getGet() {
return get;
}
public void setGet(HttpGet get) {
this.get = get;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getSu() {
return su;
}
public void setSu(String su) {
this.su = su;
}
public String getSp() {
return sp;
}
public void setSp(String sp) {
this.sp = sp;
}
public String getNonce() {
return nonce;
}
public void setNonce(String nonce) {
this.nonce = nonce;
}
public String getRsakv() {
return rsakv;
}
public void setRsakv(String rsakv) {
this.rsakv = rsakv;
}
public String getPubkey() {
return pubkey;
}
public void setPubkey(String pubkey) {
this.pubkey = pubkey;
}
public int getRetcode() {
return retcode;
}
public void setRetcode(int retcode) {
this.retcode = retcode;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getNick() {
return nick;
}
public void setNick(String nick) {
this.nick = nick;
}
public static String getSina_js() {
return sina_js;
}
public BasicCookieStore getCookieStore() {
return cookieStore;
}
public void setCookieStore(BasicCookieStore cookieStore) {
this.cookieStore = cookieStore;
}
public static void main(String[] args) {
new SinaWeiboLogger("", "").login();
}
}
登录相当成功,于是我在这代码的基础上加上了些修改,运行后代码报 Connection reset 这个错误;
经过研究发现 其实已经授权成功了,不过在授权成功后页面会进行两次重定向所以报这个错。于是又开始各种百度,这里写链接内容。
这个上面终于找到我要的东西
/**
* 根据给定的链接获取所有的重定向位置
* @param link 给定的链接
* @return
* @throws ClientProtocolException
* @throws IOException
*/
public List
List
CloseableHttpResponse response = null;
try{
HttpClientContext context = HttpClientContext.create();
HttpGet httpGet = new HttpGet(link);
response = httpClient.execute(httpGet, context);
// 获取所有的重定向位置
redirectLocations = context.getRedirectLocations();
} finally{
if(response!=null){
response.close();
}
}
return redirectLocations;
}
就这样再配合微博的SDK,一套从微博登录到发送微博的操作就完成啦,完整代码如下:
package weibo4j.examples.test;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.httpclient.HttpException;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.cookie.CookieSpec;
import org.apache.http.cookie.CookieSpecProvider;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.cookie.DefaultCookieSpec;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import weibo4j.Oauth;
import weibo4j.Timeline;
import weibo4j.http.AccessToken;
import weibo4j.model.Status;
import weibo4j.model.WeiboException;
import weibo4j.util.WeiboConfig;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
/**
* 新浪微博登录器,2016年4月7日08:30:16,密码加密方式采用RSA2,加密方式js内
*
* @author tom
*
*/
public class SinaWeiboLogger {
private HttpClient client;
private HttpPost post;
private HttpGet get;
private BasicCookieStore cookieStore;
private String username; // 明文账号
private String password; // 明文密码
private String su; // base64加密后账号
private String sp; // RSA加密后密码
// 预登陆信息 所需数据 开始
private long servertime; // 服务器时间戳
private String nonce; // 服务器返回字符串
private String rsakv; // 服务器返回字符串,rsa加密密码用
private String pubkey; // 服务器返回rsa加密公用密钥,用于rsa加密
private int retcode; //状态值
private String pcid;
private int is_openlock;
private int showpin;
private int exectime;
// 预登陆信息 结束
//登录后返回json数据
private String uid;
private String nick;
private JSONArray domainurls;
//
String clientId = WeiboConfig.getValue("client_ID") ;
String redirectURI = WeiboConfig.getValue("redirect_URI") ;
String authorizeURL = WeiboConfig.getValue("authorizeURL");
private StringBuffer headerCookie = new StringBuffer();
//登录密码加密js文件内容
private static String sina_js = "var sinaSSOEncoder=sinaSSOEncoder||{};(function(){var hexcase=0;var chrsz=8;this.hex_sha1=function(s){return binb2hex(core_sha1(str2binb(s),s.length*chrsz));};var core_sha1=function(x,len){x[len>>5]|=0x80<<(24-len%32);x[((len+64>>9)<<4)+15]=len;var w=Array(80);var a=1732584193;var b=-271733879;var c=-1732584194;var d=271733878;var e=-1009589776;for(var i=0;i
public SinaWeiboLogger(String username, String password) {
this.username = username;
this.password = password;
//cookie策略,不设置会拒绝cookie rejected,设置策略保存cookie信息
cookieStore = new BasicCookieStore();
CookieSpecProvider myCookie = new CookieSpecProvider() {
public CookieSpec create(HttpContext context) {
return new DefaultCookieSpec();
}
};
Registry
.build();
client = HttpClients.custom().setDefaultCookieStore(cookieStore).setDefaultCookieSpecRegistry(rg).build();
get = new HttpGet();
post = new HttpPost();
}
/**
* 预登陆,使用用户名base64加密后get请求,得到返回值,使用返回值加密用户密码再次请求
*
* @throws IOException
* @throws ClientProtocolException
* @throws URISyntaxException
*/
private void preLogin() throws ClientProtocolException, IOException, URISyntaxException {
// 1
su = new String(Base64.encodeBase64(URLEncoder.encode(this.username, "UTF-8").getBytes()));
String preLoginUrl = "http://login.sina.com.cn/sso/prelogin.php?entry=account&callback=sinaSSOController.preloginCallBack&su="
+ su + "&rsakt=mod&client=ssologin.js(v1.4.15)&_=" + new Date().getTime();
get.setURI(new URI(preLoginUrl));
get.addHeader("Host", "login.sina.com.cn");
get.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0");
get.addHeader("Accept", "*/*");
get.addHeader("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");
get.addHeader("Accept-Encoding", "gzip, deflate");
get.addHeader("Referer", "http://login.sina.com.cn/");
get.addHeader("Connection", "keep-alive");
HttpResponse resp = client.execute(get);
//保存请求返回cookie,登录后请求其他页面需要带cookie
saveCookie(resp.getAllHeaders(), this.headerCookie);
HttpEntity entity = resp.getEntity();
String cont = EntityUtils.toString(entity);
// System.out.println(cont);
cont = cont.replace("sinaSSOController.preloginCallBack(", "");
cont = cont.replace(")", "");
JSONObject json = JSONObject.parseObject(cont);
this.retcode = json.getIntValue("retcode");
this.servertime = json.getLongValue("servertime");
this.pcid = json.getString("pcid");
this.nonce = json.getString("nonce");
this.pubkey = json.getString("pubkey");
this.rsakv = json.getString("rsakv");
this.is_openlock = json.getIntValue("is_openlock");
this.showpin = json.getIntValue("showpin");
this.exectime = json.getIntValue("exectime");
}
private void saveCookie(Header[] headers, StringBuffer headerCookie) {
for(Header h : headers) {
if (h.getName().equals("Set-Cookie")) {
headerCookie.append(h.getValue()).append(";");
}
}
}
/**
* 登录新浪通行证 账号登录,加密密码,post请求
*
* @return
*
* @throws IOException
* @throws ClientProtocolException
* @throws ScriptException
* @throws NoSuchMethodException
* @throws URISyntaxException
*/
private AccessToken accountLogin() throws NoSuchMethodException, ScriptException, ClientProtocolException, IOException, URISyntaxException {
// 2
String loginUrl = "http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.15)&_="
+ new Date().getTime();
post.setURI(new URI(loginUrl));
post.addHeader("Host", "login.sina.com.cn");
post.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0");
post.addHeader("Accept", "*/*");
post.addHeader("Content-Type", "application/x-www-form-urlencoded");
post.addHeader("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");
post.addHeader("Accept-Encoding", "gzip, deflate");
post.addHeader("Referer", "http://login.sina.com.cn/");
post.addHeader("Connection", "keep-alive");
// 使用js加密密码,RSA,调用js内方法
ScriptEngineManager sem = new ScriptEngineManager();
ScriptEngine se = sem.getEngineByName("javascript");
se.eval(sina_js);
if (se instanceof Invocable) {
Invocable iv = (Invocable) se;
sp = (String) iv.invokeFunction("getpass", this.password, this.servertime, this.nonce,
this.pubkey);
}
List
parms.add(new BasicNameValuePair("entry", "account"));
parms.add(new BasicNameValuePair("gateway", "1"));
parms.add(new BasicNameValuePair("from", "YmyqnnSQ"));
parms.add(new BasicNameValuePair("savestate", "0"));
parms.add(new BasicNameValuePair("useticket", "0"));
parms.add(new BasicNameValuePair("pagerefer", ""));
parms.add(new BasicNameValuePair("vsnf", "1"));
parms.add(new BasicNameValuePair("su", su));
parms.add(new BasicNameValuePair("service", "sso"));
parms.add(new BasicNameValuePair("servertime", servertime + ""));
parms.add(new BasicNameValuePair("nonce", nonce));
parms.add(new BasicNameValuePair("pwencode", "rsa2"));
parms.add(new BasicNameValuePair("rsakv", rsakv));
parms.add(new BasicNameValuePair("sp", sp));
parms.add(new BasicNameValuePair("sr", "1366*768"));
parms.add(new BasicNameValuePair("encoding", "UTF-8"));
parms.add(new BasicNameValuePair("cdult", "3"));
parms.add(new BasicNameValuePair("domain", "sina.com.cn"));
parms.add(new BasicNameValuePair("prelt", "51"));
parms.add(new BasicNameValuePair("returntype", "TEXT"));
post.setEntity(new UrlEncodedFormEntity(parms));
HttpResponse resp = client.execute(post);
//保存请求返回cookie,登录后请求其他页面需要带cookie
saveCookie(resp.getAllHeaders(), this.headerCookie);
//请求返回链接
JSONObject jsonObj = JSONObject.parseObject(EntityUtils.toString(resp.getEntity()));
this.retcode = jsonObj.getIntValue("retcode");
this.uid = jsonObj.getString("uid");
this.nick = jsonObj.getString("nick");
this.domainurls = jsonObj.getJSONArray("crossDomainUrlList");
List
int i = 0;
for (Object url : domainurls) {
System.out.println("url:"+url.toString());
get.setURI(new URI(url.toString()+"&callback=sinaSSOController.doCrossDomainCallBack&scriptId=ssoscript"+i+"&client=ssologin.js(v1.4.15)&_="+new Date().getTime()));
resp = client.execute(get);
retMsg.add(EntityUtils.toString(resp.getEntity()));
i++;
}
Oauth oauth = new Oauth();
try {
String url = oauth.authorize("code");
get.setURI(new URI(url));
List
for (URI uri : allRedirectLocations) {
String retUrl = uri.toString();
int begin = retUrl.indexOf("code=");
if (begin != -1) {
int end = retUrl.indexOf("&", begin);
if (end == -1)
end = retUrl.length();
String code = retUrl.substring(begin + 5, end);
if (code != null) {
try{
AccessToken token = oauth.getAccessTokenByCode(code);
return token;
}catch(Exception e){
e.printStackTrace();
}
}
break;
}
}
} catch (WeiboException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public List
List
CloseableHttpResponse response = null;
try{
HttpClientContext context = HttpClientContext.create();
HttpGet httpGet = new HttpGet(link);
//添加请求cookie到请求头
httpGet.addHeader("Cookie", this.headerCookie.toString());
response = (CloseableHttpResponse) client.execute(httpGet, context);
// 获取所有的重定向位置
redirectLocations = context.getRedirectLocations();
} finally{
if(response!=null){
response.close();
}
}
return redirectLocations;
}
/**
* 发微博
* @param token 认证Token
* @param content 微博内容
* @return
* @throws Exception
*/
public static boolean sinaSendWeibo(String token,String content) throws Exception {
boolean flag = false ;
Timeline tm = new Timeline(token);
try
{
Status status = tm.updateStatus(content);
flag = true ;
}
catch (WeiboException e)
{
flag = false ;
System.out.println(e.getErrorCode());
}
return flag;
}
/**
* 登录
*/
@SuppressWarnings("static-access")
public void login(String context) {
try {
this.preLogin();
AccessToken at = this.accountLogin();
this.sinaSendWeibo(at.getAccessToken(),context);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (ScriptException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public HttpClient getClient() {
return client;
}
public void setClient(HttpClient client) {
this.client = client;
}
public HttpPost getPost() {
return post;
}
public void setPost(HttpPost post) {
this.post = post;
}
public HttpGet getGet() {
return get;
}
public void setGet(HttpGet get) {
this.get = get;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getSu() {
return su;
}
public void setSu(String su) {
this.su = su;
}
public String getSp() {
return sp;
}
public void setSp(String sp) {
this.sp = sp;
}
public String getNonce() {
return nonce;
}
public void setNonce(String nonce) {
this.nonce = nonce;
}
public String getRsakv() {
return rsakv;
}
public void setRsakv(String rsakv) {
this.rsakv = rsakv;
}
public String getPubkey() {
return pubkey;
}
public void setPubkey(String pubkey) {
this.pubkey = pubkey;
}
public int getRetcode() {
return retcode;
}
public void setRetcode(int retcode) {
this.retcode = retcode;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getNick() {
return nick;
}
public void setNick(String nick) {
this.nick = nick;
}
public static String getSina_js() {
return sina_js;
}
public BasicCookieStore getCookieStore() {
return cookieStore;
}
public void setCookieStore(BasicCookieStore cookieStore) {
this.cookieStore = cookieStore;
}
public static void main(String[] args) throws HttpException, IOException {
new SinaWeiboLogger("微博账号", "密码").login("xxxxx");
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~