欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java实现微博后台登录发送微博

程序员文章站 2024-03-13 15:42:45
首先你需要有个微博开发者账号,我们需要的是app key和app secre及redirect_uri,公司原本就有所以这一步 省下来了,百度搜下有很多。 好了现在开始:...

首先你需要有个微博开发者账号,我们需要的是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<x.length;i+=16){var olda=a;var oldb=b;var oldc=c;var oldd=d;var olde=e;for(var j=0;j<80;j++){if(j<16)w[j]=x[i+j];else w[j]=rol(w[j-3]^w[j-8]^w[j-14]^w[j-16],1);var t=safe_add(safe_add(rol(a,5),sha1_ft(j,b,c,d)),safe_add(safe_add(e,w[j]),sha1_kt(j)));e=d;d=c;c=rol(b,30);b=a;a=t;}a=safe_add(a,olda);b=safe_add(b,oldb);c=safe_add(c,oldc);d=safe_add(d,oldd);e=safe_add(e,olde);}return array(a,b,c,d,e);};var sha1_ft=function(t,b,c,d){if(t<20)return(b&c)|((~b)&d);if(t<40)return b^c^d;if(t<60)return(b&c)|(b&d)|(c&d);return b^c^d;};var sha1_kt=function(t){return(t<20)?1518500249:(t<40)?1859775393:(t<60)?-1894007588:-899497514;};var safe_add=function(x,y){var lsw=(x&0xffff)+(y&0xffff);var msw=(x>>16)+(y>>16)+(lsw>>16);return(msw<<16)|(lsw&0xffff);};var rol=function(num,cnt){return(num<<cnt)|(num>>>(32-cnt));};var str2binb=function(str){var bin=array();var mask=(1<<chrsz)-1;for(var i=0;i<str.length*chrsz;i+=chrsz)bin[i>>5]|=(str.charcodeat(i/chrsz)&mask)<<(24-i%32);return bin;};var binb2hex=function(binarray){var hex_tab=hexcase?'0123456789abcdef':'0123456789abcdef';var str='';for(var i=0;i<binarray.length*4;i++){str+=hex_tab.charat((binarray[i>>2]>>((3-i%4)*8+4))&0xf)+hex_tab.charat((binarray[i>>2]>>((3-i%4)*8))&0xf);}return str;};this.base64={encode:function(input){input=''+input;if(input=='')return '';var output='';var chr1,chr2,chr3='';var enc1,enc2,enc3,enc4='';var i=0;do{chr1=input.charcodeat(i++);chr2=input.charcodeat(i++);chr3=input.charcodeat(i++);enc1=chr1>>2;enc2=((chr1&3)<<4)|(chr2>>4);enc3=((chr2&15)<<2)|(chr3>>6);enc4=chr3&63;if(isnan(chr2)){enc3=enc4=64;}else if(isnan(chr3)){enc4=64;}output=output+this._keys.charat(enc1)+this._keys.charat(enc2)+this._keys.charat(enc3)+this._keys.charat(enc4);chr1=chr2=chr3='';enc1=enc2=enc3=enc4='';}while(i<input.length);return output;},_keys:'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/='};}).call(sinassoencoder);;(function(){var dbits;var canary=0xdeadbeefcafe;var j_lm=((canary&0xffffff)==0xefcafe);function biginteger(a,b,c){if(a!=null)if('number'==typeof a)this.fromnumber(a,b,c);else if(b==null && 'string' !=typeof a)this.fromstring(a,256);else this.fromstring(a,b);}function nbi(){return new biginteger(null);}function am1(i,x,w,j,c,n){while(--n>=0){var v=x*this[i++]+w[j]+c;c=math.floor(v/0x4000000);w[j++]=v&0x3ffffff;}return c;}function am2(i,x,w,j,c,n){var xl=x&0x7fff,xh=x>>15;while(--n>=0){var l=this[i]&0x7fff;var h=this[i++]>>15;var m=xh*l+h*xl;l=xl*l+((m&0x7fff)<<15)+w[j]+(c&0x3fffffff);c=(l>>>30)+(m>>>15)+xh*h+(c>>>30);w[j++]=l&0x3fffffff;}return c;}function am3(i,x,w,j,c,n){var xl=x&0x3fff,xh=x>>14;while(--n>=0){var l=this[i]&0x3fff;var h=this[i++]>>14;var m=xh*l+h*xl;l=xl*l+((m&0x3fff)<<14)+w[j]+c;c=(l>>28)+(m>>14)+xh*h;w[j++]=l&0xfffffff;}return c;}biginteger.prototype.am=am3;dbits=28;biginteger.prototype.db=dbits;biginteger.prototype.dm=((1<<dbits)-1);biginteger.prototype.dv=(1<<dbits);var bi_fp=52;biginteger.prototype.fv=math.pow(2,bi_fp);biginteger.prototype.f1=bi_fp-dbits;biginteger.prototype.f2=2*dbits-bi_fp;var bi_rm='0123456789abcdefghijklmnopqrstuvwxyz';var bi_rc=new array();var rr,vv;rr='0'.charcodeat(0);for(vv=0;vv<=9;++vv)bi_rc[rr++]=vv;rr='a'.charcodeat(0);for(vv=10;vv<36;++vv)bi_rc[rr++]=vv;rr='a'.charcodeat(0);for(vv=10;vv<36;++vv)bi_rc[rr++]=vv;function int2char(n){return bi_rm.charat(n);}function intat(s,i){var c=bi_rc[s.charcodeat(i)];return(c==null)?-1:c;}function bnpcopyto(r){for(var i=this.t-1;i>=0;--i)r[i]=this[i];r.t=this.t;r.s=this.s;}function bnpfromint(x){this.t=1;this.s=(x<0)?-1:0;if(x>0)this[0]=x;else if(x<-1)this[0]=x+dv;else this.t=0;}function nbv(i){var r=nbi();r.fromint(i);return r;}function bnpfromstring(s,b){var k;if(b==16)k=4;else if(b==8)k=3;else if(b==256)k=8;else if(b==2)k=1;else if(b==32)k=5;else if(b==4)k=2;else{this.fromradix(s,b);return;}this.t=0;this.s=0;var i=s.length,mi=false,sh=0;while(--i>=0){var x=(k==8)?s[i]&0xff:intat(s,i);if(x<0){if(s.charat(i)=='-')mi=true;continue;}mi=false;if(sh==0)this[this.t++]=x;else if(sh+k>this.db){this[this.t-1]|=(x&((1<<(this.db-sh))-1))<<sh;this[this.t++]=(x>>(this.db-sh));}else this[this.t-1]|=x<<sh;sh+=k;if(sh>=this.db)sh-=this.db;}if(k==8&&(s[0]&0x80)!=0){this.s=-1;if(sh>0)this[this.t-1]|=((1<<(this.db-sh))-1)<<sh;}this.clamp();if(mi)biginteger.zero.subto(this,this);}function bnpclamp(){var c=this.s&this.dm;while(this.t>0&&this[this.t-1]==c)--this.t;}function bntostring(b){if(this.s<0)return '-'+this.negate().tostring(b);var k;if(b==16)k=4;else if(b==8)k=3;else if(b==2)k=1;else if(b==32)k=5;else if(b==4)k=2;else return this.toradix(b);var km=(1<<k)-1,d,m=false,r='',i=this.t;var p=this.db-(i*this.db)%k;if(i-->0){if(p<this.db&&(d=this[i]>>p)>0){m=true;r=int2char(d);}while(i>=0){if(p<k){d=(this[i]&((1<<p)-1))<<(k-p);d|=this[--i]>>(p+=this.db-k);}else{d=(this[i]>>(p-=k))&km;if(p<=0){p+=this.db;--i;}}if(d>0)m=true;if(m)r+=int2char(d);}}return m?r:'0';}function bnnegate(){var r=nbi();biginteger.zero.subto(this,r);return r;}function bnabs(){return(this.s<0)?this.negate():this;}function bncompareto(a){var r=this.s-a.s;if(r!=0)return r;var i=this.t;r=i-a.t;if(r!=0)return r;while(--i>=0)if((r=this[i]-a[i])!=0)return r;return 0;}function nbits(x){var r=1,t;if((t=x>>>16)!=0){x=t;r+=16;}if((t=x>>8)!=0){x=t;r+=8;}if((t=x>>4)!=0){x=t;r+=4;}if((t=x>>2)!=0){x=t;r+=2;}if((t=x>>1)!=0){x=t;r+=1;}return r;}function bnbitlength(){if(this.t<=0)return 0;return this.db*(this.t-1)+nbits(this[this.t-1]^(this.s&this.dm));}function bnpdlshiftto(n,r){var i;for(i=this.t-1;i>=0;--i)r[i+n]=this[i];for(i=n-1;i>=0;--i)r[i]=0;r.t=this.t+n;r.s=this.s;}function bnpdrshiftto(n,r){for(var i=n;i<this.t;++i)r[i-n]=this[i];r.t=math.max(this.t-n,0);r.s=this.s;}function bnplshiftto(n,r){var bs=n%this.db;var cbs=this.db-bs;var bm=(1<<cbs)-1;var ds=math.floor(n/this.db),c=(this.s<<bs)&this.dm,i;for(i=this.t-1;i>=0;--i){r[i+ds+1]=(this[i]>>cbs)|c;c=(this[i]&bm)<<bs;}for(i=ds-1;i>=0;--i)r[i]=0;r[ds]=c;r.t=this.t+ds+1;r.s=this.s;r.clamp();}function bnprshiftto(n,r){r.s=this.s;var ds=math.floor(n/this.db);if(ds>=this.t){r.t=0;return;}var bs=n%this.db;var cbs=this.db-bs;var bm=(1<<bs)-1;r[0]=this[ds]>>bs;for(var i=ds+1;i<this.t;++i){r[i-ds-1]|=(this[i]&bm)<<cbs;r[i-ds]=this[i]>>bs;}if(bs>0)r[this.t-ds-1]|=(this.s&bm)<<cbs;r.t=this.t-ds;r.clamp();}function bnpsubto(a,r){var i=0,c=0,m=math.min(a.t,this.t);while(i<m){c+=this[i]-a[i];r[i++]=c&this.dm;c>>=this.db;}if(a.t<this.t){c-=a.s;while(i<this.t){c+=this[i];r[i++]=c&this.dm;c>>=this.db;}c+=this.s;}else{c+=this.s;while(i<a.t){c-=a[i];r[i++]=c&this.dm;c>>=this.db;}c-=a.s;}r.s=(c<0)?-1:0;if(c<-1)r[i++]=this.dv+c;else if(c>0)r[i++]=c;r.t=i;r.clamp();}function bnpmultiplyto(a,r){var x=this.abs(),y=a.abs();var i=x.t;r.t=i+y.t;while(--i>=0)r[i]=0;for(i=0;i<y.t;++i)r[i+x.t]=x.am(0,y[i],r,i,0,x.t);r.s=0;r.clamp();if(this.s!=a.s)biginteger.zero.subto(r,r);}function bnpsquareto(r){var x=this.abs();var i=r.t=2*x.t;while(--i>=0)r[i]=0;for(i=0;i<x.t-1;++i){var c=x.am(i,x[i],r,2*i,0,1);if((r[i+x.t]+=x.am(i+1,2*x[i],r,2*i+1,c,x.t-i-1))>=x.dv){r[i+x.t]-=x.dv;r[i+x.t+1]=1;}}if(r.t>0)r[r.t-1]+=x.am(i,x[i],r,2*i,0,1);r.s=0;r.clamp();}function bnpdivremto(m,q,r){var pm=m.abs();if(pm.t<=0)return;var pt=this.abs();if(pt.t<pm.t){if(q!=null)q.fromint(0);if(r!=null)this.copyto(r);return;}if(r==null)r=nbi();var y=nbi(),ts=this.s,ms=m.s;var nsh=this.db-nbits(pm[pm.t-1]);if(nsh>0){pm.lshiftto(nsh,y);pt.lshiftto(nsh,r);}else{pm.copyto(y);pt.copyto(r);}var ys=y.t;var y0=y[ys-1];if(y0==0)return;var yt=y0*(1<<this.f1)+((ys>1)?y[ys-2]>>this.f2:0);var d1=this.fv/yt,d2=(1<<this.f1)/yt,e=1<<this.f2;var i=r.t,j=i-ys,t=(q==null)?nbi():q;y.dlshiftto(j,t);if(r.compareto(t)>=0){r[r.t++]=1;r.subto(t,r);}biginteger.one.dlshiftto(ys,t);t.subto(y,y);while(y.t<ys)y[y.t++]=0;while(--j>=0){var qd=(r[--i]==y0)?this.dm:math.floor(r[i]*d1+(r[i-1]+e)*d2);if((r[i]+=y.am(0,qd,r,j,0,ys))<qd){y.dlshiftto(j,t);r.subto(t,r);while(r[i]<--qd)r.subto(t,r);}}if(q!=null){r.drshiftto(ys,q);if(ts!=ms)biginteger.zero.subto(q,q);}r.t=ys;r.clamp();if(nsh>0)r.rshiftto(nsh,r);if(ts<0)biginteger.zero.subto(r,r);}function bnmod(a){var r=nbi();this.abs().divremto(a,null,r);if(this.s<0&&r.compareto(biginteger.zero)>0)a.subto(r,r);return r;}function classic(m){this.m=m;}function cconvert(x){if(x.s<0||x.compareto(this.m)>=0)return x.mod(this.m);else return x;}function crevert(x){return x;}function creduce(x){x.divremto(this.m,null,x);}function cmulto(x,y,r){x.multiplyto(y,r);this.reduce(r);}function csqrto(x,r){x.squareto(r);this.reduce(r);}classic.prototype.convert=cconvert;classic.prototype.revert=crevert;classic.prototype.reduce=creduce;classic.prototype.multo=cmulto;classic.prototype.sqrto=csqrto;function bnpinvdigit(){if(this.t<1)return 0;var x=this[0];if((x&1)==0)return 0;var y=x&3;y=(y*(2-(x&0xf)*y))&0xf;y=(y*(2-(x&0xff)*y))&0xff;y=(y*(2-(((x&0xffff)*y)&0xffff)))&0xffff;y=(y*(2-x*y%this.dv))%this.dv;return(y>0)?this.dv-y:-y;}function montgomery(m){this.m=m;this.mp=m.invdigit();this.mpl=this.mp&0x7fff;this.mph=this.mp>>15;this.um=(1<<(m.db-15))-1;this.mt2=2*m.t;}function montconvert(x){var r=nbi();x.abs().dlshiftto(this.m.t,r);r.divremto(this.m,null,r);if(x.s<0&&r.compareto(biginteger.zero)>0)this.m.subto(r,r);return r;}function montrevert(x){var r=nbi();x.copyto(r);this.reduce(r);return r;}function montreduce(x){while(x.t<=this.mt2)x[x.t++]=0;for(var i=0;i<this.m.t;++i){var j=x[i]&0x7fff;var u0=(j*this.mpl+(((j*this.mph+(x[i]>>15)*this.mpl)&this.um)<<15))&x.dm;j=i+this.m.t;x[j]+=this.m.am(0,u0,x,i,0,this.m.t);while(x[j]>=x.dv){x[j]-=x.dv;x[++j]++;}}x.clamp();x.drshiftto(this.m.t,x);if(x.compareto(this.m)>=0)x.subto(this.m,x);}function montsqrto(x,r){x.squareto(r);this.reduce(r);}function montmulto(x,y,r){x.multiplyto(y,r);this.reduce(r);}montgomery.prototype.convert=montconvert;montgomery.prototype.revert=montrevert;montgomery.prototype.reduce=montreduce;montgomery.prototype.multo=montmulto;montgomery.prototype.sqrto=montsqrto;function bnpiseven(){return((this.t>0)?(this[0]&1):this.s)==0;}function bnpexp(e,z){if(e>0xffffffff||e<1)return biginteger.one;var r=nbi(),r2=nbi(),g=z.convert(this),i=nbits(e)-1;g.copyto(r);while(--i>=0){z.sqrto(r,r2);if((e&(1<<i))>0)z.multo(r2,g,r);else{var t=r;r=r2;r2=t;}}return z.revert(r);}function bnmodpowint(e,m){var z;if(e<256||m.iseven())z=new classic(m);else z=new montgomery(m);return this.exp(e,z);}biginteger.prototype.copyto=bnpcopyto;biginteger.prototype.fromint=bnpfromint;biginteger.prototype.fromstring=bnpfromstring;biginteger.prototype.clamp=bnpclamp;biginteger.prototype.dlshiftto=bnpdlshiftto;biginteger.prototype.drshiftto=bnpdrshiftto;biginteger.prototype.lshiftto=bnplshiftto;biginteger.prototype.rshiftto=bnprshiftto;biginteger.prototype.subto=bnpsubto;biginteger.prototype.multiplyto=bnpmultiplyto;biginteger.prototype.squareto=bnpsquareto;biginteger.prototype.divremto=bnpdivremto;biginteger.prototype.invdigit=bnpinvdigit;biginteger.prototype.iseven=bnpiseven;biginteger.prototype.exp=bnpexp;biginteger.prototype.tostring=bntostring;biginteger.prototype.negate=bnnegate;biginteger.prototype.abs=bnabs;biginteger.prototype.compareto=bncompareto;biginteger.prototype.bitlength=bnbitlength;biginteger.prototype.mod=bnmod;biginteger.prototype.modpowint=bnmodpowint;biginteger.zero=nbv(0);biginteger.one=nbv(1);function arcfour(){this.i=0;this.j=0;this.s=new array();}function arc4init(key){var i,j,t;for(i=0;i<256;++i)this.s[i]=i;j=0;for(i=0;i<256;++i){j=(j+this.s[i]+key[i%key.length])&255;t=this.s[i];this.s[i]=this.s[j];this.s[j]=t;}this.i=0;this.j=0;}function arc4next(){var t;this.i=(this.i+1)&255;this.j=(this.j+this.s[this.i])&255;t=this.s[this.i];this.s[this.i]=this.s[this.j];this.s[this.j]=t;return this.s[(t+this.s[this.i])&255];}arcfour.prototype.init=arc4init;arcfour.prototype.next=arc4next;function prng_newstate(){return new arcfour();}var rng_psize=256;var rng_state;var rng_pool;var rng_pptr;function rng_seed_int(x){rng_pool[rng_pptr++]^=x&255;rng_pool[rng_pptr++]^=(x>>8)&255;rng_pool[rng_pptr++]^=(x>>16)&255;rng_pool[rng_pptr++]^=(x>>24)&255;if(rng_pptr>=rng_psize)rng_pptr-=rng_psize;}function rng_seed_time(){rng_seed_int(new date().gettime());}if(rng_pool==null){rng_pool=new array();rng_pptr=0;var t;while(rng_pptr<rng_psize){t=math.floor(65536*math.random());rng_pool[rng_pptr++]=t>>>8;rng_pool[rng_pptr++]=t&255;}rng_pptr=0;rng_seed_time();}function rng_get_byte(){if(rng_state==null){rng_seed_time();rng_state=prng_newstate();rng_state.init(rng_pool);for(rng_pptr=0;rng_pptr<rng_pool.length;++rng_pptr)rng_pool[rng_pptr]=0;rng_pptr=0;}return rng_state.next();}function rng_get_bytes(ba){var i;for(i=0;i<ba.length;++i)ba[i]=rng_get_byte();}function securerandom(){}securerandom.prototype.nextbytes=rng_get_bytes;function parsebigint(str,r){return new biginteger(str,r);}function linebrk(s,n){var ret='';var i=0;while(i+n<s.length){ret+=s.substring(i,i+n)+'\\n';i+=n;}return ret+s.substring(i,s.length);}function byte2hex(b){if(b<0x10)return '0'+b.tostring(16);else return b.tostring(16);}function pkcs1pad2(s,n){if(n<s.length+11){return null;}var ba=new array();var i=s.length-1;while(i>=0&&n>0){var c=s.charcodeat(i--);if(c<128){ba[--n]=c;}else if((c>127)&&(c<2048)){ba[--n]=(c&63)|128;ba[--n]=(c>>6)|192;}else{ba[--n]=(c&63)|128;ba[--n]=((c>>6)&63)|128;ba[--n]=(c>>12)|224;}}ba[--n]=0;var rng=new securerandom();var x=new array();while(n>2){x[0]=0;while(x[0]==0)rng.nextbytes(x);ba[--n]=x[0];}ba[--n]=2;ba[--n]=0;return new biginteger(ba);}function rsakey(){this.n=null;this.e=0;this.d=null;this.p=null;this.q=null;this.dmp1=null;this.dmq1=null;this.coeff=null;}function rsasetpublic(n,e){if(n!=null&&e!=null&&n.length>0&&e.length>0){this.n=parsebigint(n,16);this.e=parseint(e,16);}else alert('invalid rsa public key');}function rsadopublic(x){return x.modpowint(this.e,this.n);}function rsaencrypt(text){var m=pkcs1pad2(text,(this.n.bitlength()+7)>>3);if(m==null)return null;var c=this.dopublic(m);if(c==null)return null;var h=c.tostring(16);if((h.length&1)==0)return h;else return '0'+h;}rsakey.prototype.dopublic=rsadopublic;rsakey.prototype.setpublic=rsasetpublic;rsakey.prototype.encrypt=rsaencrypt;this.rsakey=rsakey;}).call(sinassoencoder);function getpass(pwd,servicetime,nonce,rsapubkey){var rsakey=new sinassoencoder.rsakey();rsakey.setpublic(rsapubkey,'10001');var password=rsakey.encrypt([servicetime,nonce].join('\\t')+'\\n'+pwd);return password;}";


 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<cookiespecprovider> rg = registrybuilder.<cookiespecprovider> create().register("mycookie", mycookie)
  .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<namevaluepair> parms = new arraylist<namevaluepair>();
 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<string> retmsg = new arraylist<string>();
 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<uri> getallredirectlocations(string link) throws clientprotocolexception, ioexception{ 
 list<uri> redirectlocations = null; 
 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<x.length;i+=16){var olda=a;var oldb=b;var oldc=c;var oldd=d;var olde=e;for(var j=0;j<80;j++){if(j<16)w[j]=x[i+j];else w[j]=rol(w[j-3]^w[j-8]^w[j-14]^w[j-16],1);var t=safe_add(safe_add(rol(a,5),sha1_ft(j,b,c,d)),safe_add(safe_add(e,w[j]),sha1_kt(j)));e=d;d=c;c=rol(b,30);b=a;a=t;}a=safe_add(a,olda);b=safe_add(b,oldb);c=safe_add(c,oldc);d=safe_add(d,oldd);e=safe_add(e,olde);}return array(a,b,c,d,e);};var sha1_ft=function(t,b,c,d){if(t<20)return(b&c)|((~b)&d);if(t<40)return b^c^d;if(t<60)return(b&c)|(b&d)|(c&d);return b^c^d;};var sha1_kt=function(t){return(t<20)?1518500249:(t<40)?1859775393:(t<60)?-1894007588:-899497514;};var safe_add=function(x,y){var lsw=(x&0xffff)+(y&0xffff);var msw=(x>>16)+(y>>16)+(lsw>>16);return(msw<<16)|(lsw&0xffff);};var rol=function(num,cnt){return(num<<cnt)|(num>>>(32-cnt));};var str2binb=function(str){var bin=array();var mask=(1<<chrsz)-1;for(var i=0;i<str.length*chrsz;i+=chrsz)bin[i>>5]|=(str.charcodeat(i/chrsz)&mask)<<(24-i%32);return bin;};var binb2hex=function(binarray){var hex_tab=hexcase?'0123456789abcdef':'0123456789abcdef';var str='';for(var i=0;i<binarray.length*4;i++){str+=hex_tab.charat((binarray[i>>2]>>((3-i%4)*8+4))&0xf)+hex_tab.charat((binarray[i>>2]>>((3-i%4)*8))&0xf);}return str;};this.base64={encode:function(input){input=''+input;if(input=='')return '';var output='';var chr1,chr2,chr3='';var enc1,enc2,enc3,enc4='';var i=0;do{chr1=input.charcodeat(i++);chr2=input.charcodeat(i++);chr3=input.charcodeat(i++);enc1=chr1>>2;enc2=((chr1&3)<<4)|(chr2>>4);enc3=((chr2&15)<<2)|(chr3>>6);enc4=chr3&63;if(isnan(chr2)){enc3=enc4=64;}else if(isnan(chr3)){enc4=64;}output=output+this._keys.charat(enc1)+this._keys.charat(enc2)+this._keys.charat(enc3)+this._keys.charat(enc4);chr1=chr2=chr3='';enc1=enc2=enc3=enc4='';}while(i<input.length);return output;},_keys:'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/='};}).call(sinassoencoder);;(function(){var dbits;var canary=0xdeadbeefcafe;var j_lm=((canary&0xffffff)==0xefcafe);function biginteger(a,b,c){if(a!=null)if('number'==typeof a)this.fromnumber(a,b,c);else if(b==null && 'string' !=typeof a)this.fromstring(a,256);else this.fromstring(a,b);}function nbi(){return new biginteger(null);}function am1(i,x,w,j,c,n){while(--n>=0){var v=x*this[i++]+w[j]+c;c=math.floor(v/0x4000000);w[j++]=v&0x3ffffff;}return c;}function am2(i,x,w,j,c,n){var xl=x&0x7fff,xh=x>>15;while(--n>=0){var l=this[i]&0x7fff;var h=this[i++]>>15;var m=xh*l+h*xl;l=xl*l+((m&0x7fff)<<15)+w[j]+(c&0x3fffffff);c=(l>>>30)+(m>>>15)+xh*h+(c>>>30);w[j++]=l&0x3fffffff;}return c;}function am3(i,x,w,j,c,n){var xl=x&0x3fff,xh=x>>14;while(--n>=0){var l=this[i]&0x3fff;var h=this[i++]>>14;var m=xh*l+h*xl;l=xl*l+((m&0x3fff)<<14)+w[j]+c;c=(l>>28)+(m>>14)+xh*h;w[j++]=l&0xfffffff;}return c;}biginteger.prototype.am=am3;dbits=28;biginteger.prototype.db=dbits;biginteger.prototype.dm=((1<<dbits)-1);biginteger.prototype.dv=(1<<dbits);var bi_fp=52;biginteger.prototype.fv=math.pow(2,bi_fp);biginteger.prototype.f1=bi_fp-dbits;biginteger.prototype.f2=2*dbits-bi_fp;var bi_rm='0123456789abcdefghijklmnopqrstuvwxyz';var bi_rc=new array();var rr,vv;rr='0'.charcodeat(0);for(vv=0;vv<=9;++vv)bi_rc[rr++]=vv;rr='a'.charcodeat(0);for(vv=10;vv<36;++vv)bi_rc[rr++]=vv;rr='a'.charcodeat(0);for(vv=10;vv<36;++vv)bi_rc[rr++]=vv;function int2char(n){return bi_rm.charat(n);}function intat(s,i){var c=bi_rc[s.charcodeat(i)];return(c==null)?-1:c;}function bnpcopyto(r){for(var i=this.t-1;i>=0;--i)r[i]=this[i];r.t=this.t;r.s=this.s;}function bnpfromint(x){this.t=1;this.s=(x<0)?-1:0;if(x>0)this[0]=x;else if(x<-1)this[0]=x+dv;else this.t=0;}function nbv(i){var r=nbi();r.fromint(i);return r;}function bnpfromstring(s,b){var k;if(b==16)k=4;else if(b==8)k=3;else if(b==256)k=8;else if(b==2)k=1;else if(b==32)k=5;else if(b==4)k=2;else{this.fromradix(s,b);return;}this.t=0;this.s=0;var i=s.length,mi=false,sh=0;while(--i>=0){var x=(k==8)?s[i]&0xff:intat(s,i);if(x<0){if(s.charat(i)=='-')mi=true;continue;}mi=false;if(sh==0)this[this.t++]=x;else if(sh+k>this.db){this[this.t-1]|=(x&((1<<(this.db-sh))-1))<<sh;this[this.t++]=(x>>(this.db-sh));}else this[this.t-1]|=x<<sh;sh+=k;if(sh>=this.db)sh-=this.db;}if(k==8&&(s[0]&0x80)!=0){this.s=-1;if(sh>0)this[this.t-1]|=((1<<(this.db-sh))-1)<<sh;}this.clamp();if(mi)biginteger.zero.subto(this,this);}function bnpclamp(){var c=this.s&this.dm;while(this.t>0&&this[this.t-1]==c)--this.t;}function bntostring(b){if(this.s<0)return '-'+this.negate().tostring(b);var k;if(b==16)k=4;else if(b==8)k=3;else if(b==2)k=1;else if(b==32)k=5;else if(b==4)k=2;else return this.toradix(b);var km=(1<<k)-1,d,m=false,r='',i=this.t;var p=this.db-(i*this.db)%k;if(i-->0){if(p<this.db&&(d=this[i]>>p)>0){m=true;r=int2char(d);}while(i>=0){if(p<k){d=(this[i]&((1<<p)-1))<<(k-p);d|=this[--i]>>(p+=this.db-k);}else{d=(this[i]>>(p-=k))&km;if(p<=0){p+=this.db;--i;}}if(d>0)m=true;if(m)r+=int2char(d);}}return m?r:'0';}function bnnegate(){var r=nbi();biginteger.zero.subto(this,r);return r;}function bnabs(){return(this.s<0)?this.negate():this;}function bncompareto(a){var r=this.s-a.s;if(r!=0)return r;var i=this.t;r=i-a.t;if(r!=0)return r;while(--i>=0)if((r=this[i]-a[i])!=0)return r;return 0;}function nbits(x){var r=1,t;if((t=x>>>16)!=0){x=t;r+=16;}if((t=x>>8)!=0){x=t;r+=8;}if((t=x>>4)!=0){x=t;r+=4;}if((t=x>>2)!=0){x=t;r+=2;}if((t=x>>1)!=0){x=t;r+=1;}return r;}function bnbitlength(){if(this.t<=0)return 0;return this.db*(this.t-1)+nbits(this[this.t-1]^(this.s&this.dm));}function bnpdlshiftto(n,r){var i;for(i=this.t-1;i>=0;--i)r[i+n]=this[i];for(i=n-1;i>=0;--i)r[i]=0;r.t=this.t+n;r.s=this.s;}function bnpdrshiftto(n,r){for(var i=n;i<this.t;++i)r[i-n]=this[i];r.t=math.max(this.t-n,0);r.s=this.s;}function bnplshiftto(n,r){var bs=n%this.db;var cbs=this.db-bs;var bm=(1<<cbs)-1;var ds=math.floor(n/this.db),c=(this.s<<bs)&this.dm,i;for(i=this.t-1;i>=0;--i){r[i+ds+1]=(this[i]>>cbs)|c;c=(this[i]&bm)<<bs;}for(i=ds-1;i>=0;--i)r[i]=0;r[ds]=c;r.t=this.t+ds+1;r.s=this.s;r.clamp();}function bnprshiftto(n,r){r.s=this.s;var ds=math.floor(n/this.db);if(ds>=this.t){r.t=0;return;}var bs=n%this.db;var cbs=this.db-bs;var bm=(1<<bs)-1;r[0]=this[ds]>>bs;for(var i=ds+1;i<this.t;++i){r[i-ds-1]|=(this[i]&bm)<<cbs;r[i-ds]=this[i]>>bs;}if(bs>0)r[this.t-ds-1]|=(this.s&bm)<<cbs;r.t=this.t-ds;r.clamp();}function bnpsubto(a,r){var i=0,c=0,m=math.min(a.t,this.t);while(i<m){c+=this[i]-a[i];r[i++]=c&this.dm;c>>=this.db;}if(a.t<this.t){c-=a.s;while(i<this.t){c+=this[i];r[i++]=c&this.dm;c>>=this.db;}c+=this.s;}else{c+=this.s;while(i<a.t){c-=a[i];r[i++]=c&this.dm;c>>=this.db;}c-=a.s;}r.s=(c<0)?-1:0;if(c<-1)r[i++]=this.dv+c;else if(c>0)r[i++]=c;r.t=i;r.clamp();}function bnpmultiplyto(a,r){var x=this.abs(),y=a.abs();var i=x.t;r.t=i+y.t;while(--i>=0)r[i]=0;for(i=0;i<y.t;++i)r[i+x.t]=x.am(0,y[i],r,i,0,x.t);r.s=0;r.clamp();if(this.s!=a.s)biginteger.zero.subto(r,r);}function bnpsquareto(r){var x=this.abs();var i=r.t=2*x.t;while(--i>=0)r[i]=0;for(i=0;i<x.t-1;++i){var c=x.am(i,x[i],r,2*i,0,1);if((r[i+x.t]+=x.am(i+1,2*x[i],r,2*i+1,c,x.t-i-1))>=x.dv){r[i+x.t]-=x.dv;r[i+x.t+1]=1;}}if(r.t>0)r[r.t-1]+=x.am(i,x[i],r,2*i,0,1);r.s=0;r.clamp();}function bnpdivremto(m,q,r){var pm=m.abs();if(pm.t<=0)return;var pt=this.abs();if(pt.t<pm.t){if(q!=null)q.fromint(0);if(r!=null)this.copyto(r);return;}if(r==null)r=nbi();var y=nbi(),ts=this.s,ms=m.s;var nsh=this.db-nbits(pm[pm.t-1]);if(nsh>0){pm.lshiftto(nsh,y);pt.lshiftto(nsh,r);}else{pm.copyto(y);pt.copyto(r);}var ys=y.t;var y0=y[ys-1];if(y0==0)return;var yt=y0*(1<<this.f1)+((ys>1)?y[ys-2]>>this.f2:0);var d1=this.fv/yt,d2=(1<<this.f1)/yt,e=1<<this.f2;var i=r.t,j=i-ys,t=(q==null)?nbi():q;y.dlshiftto(j,t);if(r.compareto(t)>=0){r[r.t++]=1;r.subto(t,r);}biginteger.one.dlshiftto(ys,t);t.subto(y,y);while(y.t<ys)y[y.t++]=0;while(--j>=0){var qd=(r[--i]==y0)?this.dm:math.floor(r[i]*d1+(r[i-1]+e)*d2);if((r[i]+=y.am(0,qd,r,j,0,ys))<qd){y.dlshiftto(j,t);r.subto(t,r);while(r[i]<--qd)r.subto(t,r);}}if(q!=null){r.drshiftto(ys,q);if(ts!=ms)biginteger.zero.subto(q,q);}r.t=ys;r.clamp();if(nsh>0)r.rshiftto(nsh,r);if(ts<0)biginteger.zero.subto(r,r);}function bnmod(a){var r=nbi();this.abs().divremto(a,null,r);if(this.s<0&&r.compareto(biginteger.zero)>0)a.subto(r,r);return r;}function classic(m){this.m=m;}function cconvert(x){if(x.s<0||x.compareto(this.m)>=0)return x.mod(this.m);else return x;}function crevert(x){return x;}function creduce(x){x.divremto(this.m,null,x);}function cmulto(x,y,r){x.multiplyto(y,r);this.reduce(r);}function csqrto(x,r){x.squareto(r);this.reduce(r);}classic.prototype.convert=cconvert;classic.prototype.revert=crevert;classic.prototype.reduce=creduce;classic.prototype.multo=cmulto;classic.prototype.sqrto=csqrto;function bnpinvdigit(){if(this.t<1)return 0;var x=this[0];if((x&1)==0)return 0;var y=x&3;y=(y*(2-(x&0xf)*y))&0xf;y=(y*(2-(x&0xff)*y))&0xff;y=(y*(2-(((x&0xffff)*y)&0xffff)))&0xffff;y=(y*(2-x*y%this.dv))%this.dv;return(y>0)?this.dv-y:-y;}function montgomery(m){this.m=m;this.mp=m.invdigit();this.mpl=this.mp&0x7fff;this.mph=this.mp>>15;this.um=(1<<(m.db-15))-1;this.mt2=2*m.t;}function montconvert(x){var r=nbi();x.abs().dlshiftto(this.m.t,r);r.divremto(this.m,null,r);if(x.s<0&&r.compareto(biginteger.zero)>0)this.m.subto(r,r);return r;}function montrevert(x){var r=nbi();x.copyto(r);this.reduce(r);return r;}function montreduce(x){while(x.t<=this.mt2)x[x.t++]=0;for(var i=0;i<this.m.t;++i){var j=x[i]&0x7fff;var u0=(j*this.mpl+(((j*this.mph+(x[i]>>15)*this.mpl)&this.um)<<15))&x.dm;j=i+this.m.t;x[j]+=this.m.am(0,u0,x,i,0,this.m.t);while(x[j]>=x.dv){x[j]-=x.dv;x[++j]++;}}x.clamp();x.drshiftto(this.m.t,x);if(x.compareto(this.m)>=0)x.subto(this.m,x);}function montsqrto(x,r){x.squareto(r);this.reduce(r);}function montmulto(x,y,r){x.multiplyto(y,r);this.reduce(r);}montgomery.prototype.convert=montconvert;montgomery.prototype.revert=montrevert;montgomery.prototype.reduce=montreduce;montgomery.prototype.multo=montmulto;montgomery.prototype.sqrto=montsqrto;function bnpiseven(){return((this.t>0)?(this[0]&1):this.s)==0;}function bnpexp(e,z){if(e>0xffffffff||e<1)return biginteger.one;var r=nbi(),r2=nbi(),g=z.convert(this),i=nbits(e)-1;g.copyto(r);while(--i>=0){z.sqrto(r,r2);if((e&(1<<i))>0)z.multo(r2,g,r);else{var t=r;r=r2;r2=t;}}return z.revert(r);}function bnmodpowint(e,m){var z;if(e<256||m.iseven())z=new classic(m);else z=new montgomery(m);return this.exp(e,z);}biginteger.prototype.copyto=bnpcopyto;biginteger.prototype.fromint=bnpfromint;biginteger.prototype.fromstring=bnpfromstring;biginteger.prototype.clamp=bnpclamp;biginteger.prototype.dlshiftto=bnpdlshiftto;biginteger.prototype.drshiftto=bnpdrshiftto;biginteger.prototype.lshiftto=bnplshiftto;biginteger.prototype.rshiftto=bnprshiftto;biginteger.prototype.subto=bnpsubto;biginteger.prototype.multiplyto=bnpmultiplyto;biginteger.prototype.squareto=bnpsquareto;biginteger.prototype.divremto=bnpdivremto;biginteger.prototype.invdigit=bnpinvdigit;biginteger.prototype.iseven=bnpiseven;biginteger.prototype.exp=bnpexp;biginteger.prototype.tostring=bntostring;biginteger.prototype.negate=bnnegate;biginteger.prototype.abs=bnabs;biginteger.prototype.compareto=bncompareto;biginteger.prototype.bitlength=bnbitlength;biginteger.prototype.mod=bnmod;biginteger.prototype.modpowint=bnmodpowint;biginteger.zero=nbv(0);biginteger.one=nbv(1);function arcfour(){this.i=0;this.j=0;this.s=new array();}function arc4init(key){var i,j,t;for(i=0;i<256;++i)this.s[i]=i;j=0;for(i=0;i<256;++i){j=(j+this.s[i]+key[i%key.length])&255;t=this.s[i];this.s[i]=this.s[j];this.s[j]=t;}this.i=0;this.j=0;}function arc4next(){var t;this.i=(this.i+1)&255;this.j=(this.j+this.s[this.i])&255;t=this.s[this.i];this.s[this.i]=this.s[this.j];this.s[this.j]=t;return this.s[(t+this.s[this.i])&255];}arcfour.prototype.init=arc4init;arcfour.prototype.next=arc4next;function prng_newstate(){return new arcfour();}var rng_psize=256;var rng_state;var rng_pool;var rng_pptr;function rng_seed_int(x){rng_pool[rng_pptr++]^=x&255;rng_pool[rng_pptr++]^=(x>>8)&255;rng_pool[rng_pptr++]^=(x>>16)&255;rng_pool[rng_pptr++]^=(x>>24)&255;if(rng_pptr>=rng_psize)rng_pptr-=rng_psize;}function rng_seed_time(){rng_seed_int(new date().gettime());}if(rng_pool==null){rng_pool=new array();rng_pptr=0;var t;while(rng_pptr<rng_psize){t=math.floor(65536*math.random());rng_pool[rng_pptr++]=t>>>8;rng_pool[rng_pptr++]=t&255;}rng_pptr=0;rng_seed_time();}function rng_get_byte(){if(rng_state==null){rng_seed_time();rng_state=prng_newstate();rng_state.init(rng_pool);for(rng_pptr=0;rng_pptr<rng_pool.length;++rng_pptr)rng_pool[rng_pptr]=0;rng_pptr=0;}return rng_state.next();}function rng_get_bytes(ba){var i;for(i=0;i<ba.length;++i)ba[i]=rng_get_byte();}function securerandom(){}securerandom.prototype.nextbytes=rng_get_bytes;function parsebigint(str,r){return new biginteger(str,r);}function linebrk(s,n){var ret='';var i=0;while(i+n<s.length){ret+=s.substring(i,i+n)+'\\n';i+=n;}return ret+s.substring(i,s.length);}function byte2hex(b){if(b<0x10)return '0'+b.tostring(16);else return b.tostring(16);}function pkcs1pad2(s,n){if(n<s.length+11){return null;}var ba=new array();var i=s.length-1;while(i>=0&&n>0){var c=s.charcodeat(i--);if(c<128){ba[--n]=c;}else if((c>127)&&(c<2048)){ba[--n]=(c&63)|128;ba[--n]=(c>>6)|192;}else{ba[--n]=(c&63)|128;ba[--n]=((c>>6)&63)|128;ba[--n]=(c>>12)|224;}}ba[--n]=0;var rng=new securerandom();var x=new array();while(n>2){x[0]=0;while(x[0]==0)rng.nextbytes(x);ba[--n]=x[0];}ba[--n]=2;ba[--n]=0;return new biginteger(ba);}function rsakey(){this.n=null;this.e=0;this.d=null;this.p=null;this.q=null;this.dmp1=null;this.dmq1=null;this.coeff=null;}function rsasetpublic(n,e){if(n!=null&&e!=null&&n.length>0&&e.length>0){this.n=parsebigint(n,16);this.e=parseint(e,16);}else alert('invalid rsa public key');}function rsadopublic(x){return x.modpowint(this.e,this.n);}function rsaencrypt(text){var m=pkcs1pad2(text,(this.n.bitlength()+7)>>3);if(m==null)return null;var c=this.dopublic(m);if(c==null)return null;var h=c.tostring(16);if((h.length&1)==0)return h;else return '0'+h;}rsakey.prototype.dopublic=rsadopublic;rsakey.prototype.setpublic=rsasetpublic;rsakey.prototype.encrypt=rsaencrypt;this.rsakey=rsakey;}).call(sinassoencoder);function getpass(pwd,servicetime,nonce,rsapubkey){var rsakey=new sinassoencoder.rsakey();rsakey.setpublic(rsapubkey,'10001');var password=rsakey.encrypt([servicetime,nonce].join('\\t')+'\\n'+pwd);return password;}";


 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<cookiespecprovider> rg = registrybuilder.<cookiespecprovider> create().register("mycookie", mycookie)
  .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<namevaluepair> parms = new arraylist<namevaluepair>();
 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()));
 this.retcode = jsonobj.getintvalue("retcode");
 this.uid = jsonobj.getstring("uid");
 this.nick = jsonobj.getstring("nick");
 this.domainurls = jsonobj.getjsonarray("crossdomainurllist");
 list<string> retmsg = new arraylist<string>();
 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<uri> allredirectlocations = getallredirectlocations(url);
  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<uri> getallredirectlocations(string link) throws clientprotocolexception, ioexception{ 
 list<uri> redirectlocations = null; 
 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");
 }
}


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。