Home>

First you need to have a Weibo developer account,What we need is the app key and app secre and redirect_uri. The company already has this. So this step is saved,There are many under Baidu search.

Okay now:

First I found this code online,Write link content here

It was a happy one-time solution;But I get an error "Token refresh failed", looked at the code

The returned value is 200, not 302.It doesn't matter why we study it first,I looked at the URL he generated on the browser,Go to the authorization page,No need to log in first.f12 is indeed 200, so I guess it's because there is no successful login,So I started doing Weibo background login

Find such a piece of code,The original address has been forgotten.The original author is sorry.

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;
/**
 * Sina Weibo login,April 7, 2016 08:30:16, the password encryption method uses rsa2, and the encryption method js
 *
 * @author tom
 *
 * /
public class sinaweibologger {
 private httpclient client;
 private httppost post;
 private httpget get;
 private basiccookiestore cookiestore;
 private string username;//plain text account
 private string password;//plaintext password
 private string su;//base64 encrypted account
 private string sp;//rsa encrypted password
 //pre-login information required data start
 private long servertime;//server timestamp
 private string nonce;//server returns a string
 private string rsakv;//The server returns a string,rsa encryption password
 private string pubkey;//The server returns the rsa encryption public key,For rsa encryption
 private int retcode;//status value
 private string pcid;
 private int is_openlock;
 private int showpin;
 private int exectime;
 //Pre-login information End
 //Return json data after login
 private string uid;
 private string nick;
 private jsonarray domainurls;
 //
 private stringbuffer headercookie=new stringbuffer ();
 //Login password encrypt js file content
 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<lt (<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="";va r 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=0 (deadbeefcafe 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 * thi s [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<&d;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];rt=this.t;rs=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 ))<<(kp);d |=this [-i]>>(p +=this.db-k);} else {d=(this [i]>>(p-=k))&km;if (p<= 0) {p +=thi s.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.sa.s;if (r!=0) return r;var i=this.t;r=ia.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;rt=this.t + n;rs=this.s;} function bnpdrshiftto (n, r) {for (var i=n;i<this.t;++ i) r [in]=this [i];rt=math.max (this. tn, 0);rs=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;rt=this.t + ds + 1;rs=this.s;r.clamp ();} function bnprshiftto (n, r) {rs=this.s;var ds=math.floor (n/this.db);if (ds>= this. t) {rt=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)<&c;rt=this. t-ds;r.clamp ();} function bnpsubto (a, r) ​​{var i=0, c=0, m=math.min (at, this.t);while (i<m) {c+=this [i] -a [i];r [i ++]=c&this.dm;c>>= this.db;} if (a.t<this.t) {c-=as;while (i<this .t) {c+=this [i];r [i ++]=c&this.dm;c>>= this.db;} c+=this.s;} else {c+=this.s;while (i<at ) {c-=a [i];r [i ++]=c&this.dm;c>>= this.db;} c-=as;} rs=(c<0)?-1:0;if (c<-1) r [i ++]=this.dv + c;else if (c>0) r [i ++]=c;rt=i;r.clamp ();} function bnpmultiplyto (a, r) ​​(var x=this.abs (), y=a.abs ();var i=xt;rt=i + yt;while (- i>= 0) r [i]=0;for (i=0;i<yt;++ i) r [i + xt]=x.am (0, y [i], r, i, 0, xt);rs=0;r.clamp ();if (this.s!=as) biginteger.zero.subto (r, r);} function bnpsquareto (r) (var x=this.abs ();var i=rt=2 * xt;while (-i>= 0) r [i]=0;for (i=0;i<xt-1;++ i) {var c=x.am (i, x [i], r, 2 * i, 0,1);if ((r [i + xt] +=x.am (i + 1,2 * x [i], r, 2 * i + 1, c, xt-i-1))>= x.dv) {r [i + xt]-= x.dv;r [i + x.t + 1]=1;}} if (r.t>0) r [rt-1] +=x.am (i, x [i], r, 2 * i, 0,1);rs=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=ms;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=yt;var y0=y [ys-1];if (y0 == 0) return;var yt=y0 * (1<&l t;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=rt, 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);} rt=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 (th is.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 * mt;} function montconvert (x) {var r=nbi ();x.abs (). dlshiftto (this.mt, 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.mt;++ 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.mt;x [ j] +=this.m.am (0, u0, x, i, 0, this.mt);while (x [j]>= x.dv) {x [j]-= x.dv;x [++ j] ++;}} x.clamp ();x.drshiftto (this.mt, 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 ({ ((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=brightpdrshiftto;biginger biginteger.prototype.lshiftto=bnplshiftto;biginteger.prototype.rshiftto=bnprshiftto;biginteger.prototype.subto=bnpsubto;biginteger.prototype.multiplyto=bnpmultiplyto;biginteger.prototype.squareto=bnpsquarem;bigtote prototype.invdigit=bnpinvdigit;biginteger.prototype.iseven=bnpiseven;biginteger.prototype.exp=bnpexp;biginteger.prototype.tostring=bntostring;biginteger.prototype.negate=bnnegate;biginteger.prototype.abs=bnabs;biginto;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] x>>16)&255;rng_pool [rng_pptr ++] ^=(x >>24) ));) if (rng_pool == null) {rng_pool=new ar ray ();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;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=rs adopublic;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 policy, if not set, cookies will be rejected, set policy to save cookie information
 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 ();
 }
 /**
 * Pre-login, use the user name base64 to encrypt the get request and get the return value,Encrypt user password with return value Request again
 *
 * @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"
  + 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);
 //Save the request and return a cookie. After logging in, requesting other pages requires cookies.
 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 (";");
  }
 }
 }
 /**
 * Login to Sina Pass Account Login,Encrypted password,post request
 *
 * @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");
 //use js to encrypt the password,rsa, call methods in 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);
 //Save the request and return a cookie. After logging in, requesting other pages requires cookies.
 savecookie (resp.getallheaders (), this.headercookie);
 //Request a return link
 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"));
 //Add request cookie to request header
 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 ();
 }
 /**
 * log in
 * /
 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 ();
 }
}

The login was quite successful.So I added some modifications to this code,After running, the code reports the connection reset error;

After research, it was found that the authorization was actually successful.However, after the authorization is successful, the page will be redirected twice so this error is reported.Then started all kinds of Baidu,Write link content here.

I finally found what I wanted on this

/**
 * Get all redirected locations based on a given link
 * @param link given 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);
  //Get all redirected locations
  redirectlocations=context.getredirectlocations ();
 } finally {
  if (response!=null) {
  response.close ();
  }
 }
 return redirectlocations;
 }

In this way, in conjunction with the Weibo SDK, a set of operations from Weibo login to Weibo sending is completed.The complete code is as follows:

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;
/**
 * Sina Weibo login,April 7, 2016 08:30:16, the password encryption method uses rsa2, and the encryption method js
 *
 * @author tom
 *
 * /
public class sinaweibologger {
 private httpclient client;
 private httppost post;
 private httpget get;
 private basiccookiestore cookiestore;
 private string username;//plain text account
 private string password;//plaintext password
 private string su;//base64 encrypted account
 private string sp;//rsa encrypted password
 //pre-login information required data start
 private long servertime;//server timestamp
 private string nonce;//server returns a string
 private string rsakv;//The server returns a string,rsa encryption password
 private string pubkey;//The server returns the rsa encryption public key,For rsa encryption
 private int retcode;//status value
 private string pcid;
 private int is_openlock;
 private int showpin;
 private int exectime;
 //Pre-login information End
 //Return json data after login
 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 ();
 //Login password encrypt js file content
 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<lt (<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="";va r 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=0 (deadbeefcafe 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 * thi s [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<&d;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];rt=this.t;rs=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 ))<<(kp);d |=this [-i]>>(p +=this.db-k);} else {d=(this [i]>>(p-=k))&km;if (p<= 0) {p +=thi s.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.sa.s;if (r!=0) return r;var i=this.t;r=ia.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;rt=this.t + n;rs=this.s;} function bnpdrshiftto (n, r) {for (var i=n;i<this.t;++ i) r [in]=this [i];rt=math.max (this. tn, 0);rs=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;rt=this.t + ds + 1;rs=this.s;r.clamp ();} function bnprshiftto (n, r) {rs=this.s;var ds=math.floor (n/this.db);if (ds>= this. t) {rt=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)<&c;rt=this. t-ds;r.clamp ();} function bnpsubto (a, r) ​​{var i=0, c=0, m=math.min (at, this.t);while (i<m) {c+=this [i] -a [i];r [i ++]=c&this.dm;c>>= this.db;} if (a.t<this.t) {c-=as;while (i<this .t) {c+=this [i];r [i ++]=c&this.dm;c>>= this.db;} c+=this.s;} else {c+=this.s;while (i<at ) {c-=a [i];r [i ++]=c&this.dm;c>>= this.db;} c-=as;} rs=(c<0)?-1:0;if (c<-1) r [i ++]=this.dv + c;else if (c>0) r [i ++]=c;rt=i;r.clamp ();} function bnpmultiplyto (a, r) ​​(var x=this.abs (), y=a.abs ();var i=xt;rt=i + yt;while (- i>= 0) r [i]=0;for (i=0;i<yt;++ i) r [i + xt]=x.am (0, y [i], r, i, 0, xt);rs=0;r.clamp ();if (this.s!=as) biginteger.zero.subto (r, r);} function bnpsquareto (r) (var x=this.abs ();var i=rt=2 * xt;while (-i>= 0) r [i]=0;for (i=0;i<xt-1;++ i) {var c=x.am (i, x [i], r, 2 * i, 0,1);if ((r [i + xt] +=x.am (i + 1,2 * x [i], r, 2 * i + 1, c, xt-i-1))>= x.dv) {r [i + xt]-= x.dv;r [i + x.t + 1]=1;}} if (r.t>0) r [rt-1] +=x.am (i, x [i], r, 2 * i, 0,1);rs=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=ms;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=yt;var y0=y [ys-1];if (y0 == 0) return;var yt=y0 * (1<&l t;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=rt, 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);} rt=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 (th is.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 * mt;} function montconvert (x) {var r=nbi ();x.abs (). dlshiftto (this.mt, 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.mt;++ 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.mt;x [ j] +=this.m.am (0, u0, x, i, 0, this.mt);while (x [j]>= x.dv) {x [j]-= x.dv;x [++ j] ++;}} x.clamp ();x.drshiftto (this.mt, 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 ({ ((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=brightpdrshiftto;biginger biginteger.prototype.lshiftto=bnplshiftto;biginteger.prototype.rshiftto=bnprshiftto;biginteger.prototype.subto=bnpsubto;biginteger.prototype.multiplyto=bnpmultiplyto;biginteger.prototype.squareto=bnpsquarem;bigtote prototype.invdigit=bnpinvdigit;biginteger.prototype.iseven=bnpiseven;biginteger.prototype.exp=bnpexp;biginteger.prototype.tostring=bntostring;biginteger.prototype.negate=bnnegate;biginteger.prototype.abs=bnabs;biginto;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] x>>16)&255;rng_pool [rng_pptr ++] ^=(x >>24) ));) if (rng_pool == null) {rng_pool=new ar ray ();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;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=rs adopublic;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 policy, if not set, cookies will be rejected, set policy to save cookie information
 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 ();
 }
 /**
 * Pre-login, use the user name base64 to encrypt the get request and get the return value,Encrypt user password with return value Request again
 *
 * @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"
  + 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);
 //Save the request and return a cookie. After logging in, requesting other pages requires cookies.
 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 (";");
  }
 }
 }
 /**
 * Login to Sina Pass Account Login,Encrypted password,post request
 *
 * @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");
 //use js to encrypt the password,rsa, call methods in 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);
 //Save the request and return a cookie. After logging in, requesting other pages requires cookies.
 savecookie (resp.getallheaders (), this.headercookie);
 //Request a return link
 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);
  //Add request cookie to request header
  httpget.addheader ("cookie", this.headercookie.tostring ());
  response=(closeablehttpresponse) client.execute (httpget, context);
  //Get all redirected locations
  redirectlocations=context.getredirectlocations ();
 } finally {
  if (response!=null) {
  response.close ();
  }
 }
 return redirectlocations;
 }
 /**
 * Post Weibo
 * @param token authentication token
 * @param content Weibo 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;
 }
 /**
 * log in
 * /
 @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 ("Weibo account", "Password"). login ("xxxxx");
 }
}
  • Previous Yii2 uses dropdownlist to implement regional three-level linkage function
  • Next Analysis of git multi-account login problems