English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java, um den Login von Sina Weibo zu simulieren und Daten abzurufen

Vorwort:

Brüder, hier ist es! Lately, someone asked me how to simulate the login of Sina Weibo to capture data. After hearing this, I silently took a puff of an old cigarette and whispered to myself, it's time for you, old man, to make an appearance. So today, I took some time to organize and discuss a few points.

Erstens:

Um sich bei Sina Weibo anzumelden, ist eine Voranmeldung erforderlich, d.h. die Base64-Kodierung des Kontos64Verschlüsselung, RSA-Verschlüsselung des Passworts sowie der Anfragehttp://login.sina.com.cn/sso/prelogin.phpUm einige Parameter zu erhalten, die für die Anmeldung erforderlich sind, wird eine Verbindung hergestellt und der zurückgegebene String wie folgt ist:

{"retcode":0, "servertime":1487292003,"pcid":"gz"-9e1f24c9acdefb111e1c8078558c7d9c0bf2", "nonce":"VHRDG"1", "pubkey":"EB"2A38568661887FA180BDDB5CABD5F21C7BFD59C090CB2D245A87AC253062882729293E5506350508E7F9AA3BB77F4333231490F915F6D63C55FE2F08A49B353F444AD3993CACC02DB784ABBB8E42A9B1BBFFFB38BE18D78E87A0E41B9B8F73A928EE0CCEE1F6739884B9777E4FE9E88A1BBE495927AC4A799B3181D6442443", "rsakv":"1330428213", "is_openlock":0, "lm":1,"smsurl":"https:\"}}/\/login.sina.com.cn\/sso\/msglogin?entry=weibo&mobile=18360903574&s=ea7a2e91c5f1d6da7f42aa87fe6963d0","showpin":0,"exectime":222}

Next comes the pre-login processing code:

/**
 * @author LongJin
 * @description Initial Login Information<br> Returning false indicates initial failure
 * @return
 */
 public boolean preLogin(){
 boolean flag = false;
 try {
 su = new String(Base64.encodeBase64(URLEncoder.encode(username, "UTF-8").getBytes()));
 String url = "http:";//login.sina.com.cn/sso/prelogin.php?entry=weibo&rsakt=mod&checkpin=1&" +
  "client=ssologin.js(v1.4.5)&_=" + getTimestamp();
 url += "&su=" + su;
 String content;
 content = HttpUtils.getRequest(client, url);
 System.out.println("content------------" + content);
 JSONObject json = JSONObject.fromObject(content);
 System.out.println(json);
 servertime = json.getLong("servertime");
 nonce = json.getString("nonce");
 rsakv = json.getString("rsakv");
 pubkey = json.getString("pubkey");
 flag = encodePwd();
 } catch (UnsupportedEncodingException e) {
 System.out.println("UnsupportedEncoding-Exception wird ausgelöst");
 } catch (ClientProtocolException e) {
 System.out.println("抛出ClientProtocol异常");
 } catch (IOException e) {
 System.out.println("抛出IO异常");
 }
 return flag;
 }

Zweitens:

Nachdem die Parameter für die Anmeldung erhalten wurden, wird eine POST-Anfrage verwendethttp://login.sina.com.cn/sso/login.phpDie oben genannten Daten, die nach der Voranmeldung verarbeitet werden, werden als Parameter in die Anfrage eingefügt und das folgende Ergebnis erhalten:

<html>
<head>
<meta http-equiv="Content"-Type" content="text"/html; charset=GBK" />
<title>新浪通行证</title>
<script charset="utf-8" src="http://i.sso.sina.com.cn/js/ssologin.js"></script>
</head>
<body>
正在登录 ...
<script>
try{sinaSSOController.setCrossDomainUrlList({"retcode":0,"arrURL":["http:\/\/passport.97973.com\/sso\/crossdomain?action=login&savestate=1518828005"http:\/\/passport.weibo.cn\/sso\/crossdomain?action=login&savestate=1"]});}
 catch(e){
 var msg = e.message;
 var img = new Image();
 var type = 1;
 img.src = 'http://login.sina.com.cn/sso/debuglog?msg=' + msg +'&type=' + type;
 }try{sinaSSOController.crossDomainAction('login',function(){location.replace('http://passport.weibo.com/wbsso/login?ssosavestate=1518828005&url=http%3A%2F%2Fweibo.com%2Fajaxlogin.php%3Fframelogin%3D1%26callback%3Dparent.sinaSSOController.feedBackUrlCallBack&ticket=ST-NTUwODg3MjkxMQ==-1487292005-gz-FF56C545999F864FC6C7AB86FCA9FA4A-1&retcode=0');});}
 catch(e){
 var msg = e.message;
 var img = new Image();
 var type = 2;
 img.src = 'http://login.sina.com.cn/sso/debuglog?msg=' + msg +'&type=' + type;
 }
</script>
</body>
</html>

然后用正则截取其中我们想要的部分:location.replace('')中间部分,正则表达式为:

String regex = "location.replace\\('([\\s\\S*?);)'\\);";

将正则得到的结果进行处理,如果成功则使用get请求得到的链接,登录部分的代码如下:

/**
 * @author LongJin
 * @description 登录
 * @return true: 登录成功
 */
 public boolean login() {
 if(preLogin()) {}}
 String url = "http:";//login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.15);
 List<NameValuePair> parms = new ArrayList<NameValuePair>();
 parms.add(new BasicNameValuePair("entry", "weibo"));
 parms.add(new BasicNameValuePair("geteway", ""));1"));
 parms.add(new BasicNameValuePair("from", ""));
 parms.add(new BasicNameValuePair("savestate", ""));7"));
 parms.add(new BasicNameValuePair("useticket", ""));1"));
 parms.add(new BasicNameValuePair("pagerefer", "http:"));//login.sina.com.cn/sso/logout.php?entry=miniblog&r=http%3A%2F%2Fweibo.com%2Flogout.php%3Fbackurl%3D%2F"));
 parms.add(new BasicNameValuePair("vsnf", ""));1"));
 parms.add(new BasicNameValuePair("su", su));
 parms.add(new BasicNameValuePair("service", "miniblog"));
 parms.add(new BasicNameValuePair("servertime", servertime)); + ""));
 parms.add(new BasicNameValuePair("nonce", nonce));
 parms.add(new BasicNameValuePair("pwencode", "rsa"));2"));
 parms.add(new BasicNameValuePair("rsakv", rsakv));
 parms.add(new BasicNameValuePair("sp", sp));
 parms.add(new BasicNameValuePair("encoding", "UTF-8"));
 parms.add(new BasicNameValuePair("prelt", "182"));
 parms.add(new BasicNameValuePair("url", "http://weibo.com/ajaxlogin.php?framelogin=1&callback=parent.sinaSSOController.feedBackUrlCallBack"));
 parms.add(new BasicNameValuePair("domain", "sina.com.cn"));
 parms.add(new BasicNameValuePair("returntype", "META"));
 try {
 String content = HttpUtils.postRequest(client, url, parms);
 System.out.println("content----------" + content);
 String regex = "location.replace\\('([\\s\\S*?);)'\\);";//\\(' '\\)Besondere Zeichen umwandeln und den Inhalt im Pfeil (''') matchen//location.replace([\\s\\S]*?);
 Pattern p = Pattern.compile(regex);
 Matcher m = p.matcher(content);
 if(m.find()) {
  System.out.println("ss = ");+m.group());
  location = m.group(1);
  if(location.contains("reason=")) {//Wenn du bis hierher gekommen bist, gratuliere, du hast einen Fehler gemeldet
  errInfo = location.substring(location.indexOf("reason=") + 7);
  errInfo = URLDecoder.decode(errInfo, "GBK");
  } else {
  System.out.println("location = ");+location);
  String result = HttpUtils.getRequest(client, location);//.substring(2, location.length()-2)
  int beginIndex = result.indexOf("(");
  int endIndex = result.lastIndexOf(")");
  result = result.substring(beginIndex+1, endIndex);//截取括号里面的json字符串
  //content = URLDecoder.decode(content, "UTF-8");
  JSONObject jsonObject = JSONObject.fromObject(result);//转换为json
  //获取uniqueid+userdomain用于访问时带的参数
  uniqueid = jsonObject.getJSONObject("userinfo").getString("uniqueid");
  userdomain = jsonObject.getJSONObject("userinfo").getString("userdomain");
  System.out.println("result--------------" + result);
  return true;
  }
 }
 } catch (ClientProtocolException e) {
 System.out.println("抛出ClientProtocol异常");
 } catch (IOException e) {
 System.out.println("抛出IO异常");
 }
 }
 return false;
 }

补充一下密码加密部分的代码:

 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=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]&11)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();}+n<s.length){ret+=s.substring(i,i+n)+'\\n';i+byte=n;}return ret+s.substring(i,s.length);}2Hex(b){if(b<0x10)return '0'+b.toString(16);else return b.toString(16);}1pad2(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.dmp1116);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()+73);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;}";
/**
 * 密码进行RSA加密<br>
 * 返回false说明加密失败
 * @return
 */
 private boolean encodePwd() {
 ScriptEngineManager sem = new ScriptEngineManager();
 ScriptEngine se = sem.getEngineByName("javascript");
 try {
 // Verwendung der js-Verschlüsselung des Passworts, RSA, Aufruf der internen js-Methode. Ich verwende hier ein String, kann aber auch direkt in eine Datei eingefügt und gelesen werden, wie in den folgenden Kommentaren beschrieben.
 se.eval(sina_js);
 //Aufruf der internen js-Funktion zum Verschlüsseln
 if (se instanceof Invocable) {
 Invocable iv = (Invocable) se;
 sp = (String) iv.invokeFunction("getpass", this.password, this.servertime, this.nonce,
  this.pubkey);
 }
 /* FileReader fr = new FileReader("E:\\encoder.js");
 se.eval(fr);
 Invocable invocableEngine = (Invocable) se;
 String callbackvalue = (String) invocableEngine.invokeFunction("encodePwd", pubkey, servertime, nonce, password);
 sp = callbackvalue;*/
 return true;
 } catch (ScriptException e) {
 // TODO Auto-Erzeugter catch-Block
 //e.printStackTrace();
 } catch (NoSuchMethodException e) {
 // TODO Auto-Erzeugter catch-Block
 //e.printStackTrace();
 }
 errInfo = "Das Passwortverschlüsseln ist fehlgeschlagen!";
 return false;
 }
/**
* @author LongJin
* @description Rückerstattung der Fehlerinformation
* @return
*/
public String getErrInfo() {
return errInfo;
}

Der Anmeldeabschnitt ist im Grunde abgeschlossen.

Zuletzt führen wir den Test der Anmelde- und Datenabholung durch:

public static void main(String[] args) throws ClientProtocolException, IOException {
 SinaWeibo weibo = new SinaWeibo("**", ", "}}***");//账号密码在此就不透露了
 if(weibo.login()) {
 System.out.println("登录成功!");
 InputStream con= HttpUtils.getRequests(client, "http://weibo.com/u/"+uniqueid+userdomain);//请求个人主页获取输入流
 String cont = readStreamByEncoding(con, "UTF-8");//将返回的输入流转换为字符串
 String sb = HttpUtils.getText(cont);//通过jsoup获取text内容部分
 //readStreamOutFileByEncoding(sb);也可以将获取的内容写入文件中
 } else {
 System.out.println("登录失败!");
 } 
 }

得到的结果为:

text--------------我选择了"易建联"这个选项。 #本土MVP# 本赛季常规赛最有价值球员(MVP)评选小组由中国篮协新闻委员会成员单位代表、俱乐部推荐的地方媒体代表组成,新浪拥有一票,我们将把粉丝们的意见发送给篮协。本赛季本土MVP是谁? ????

至此整个登录过程就完成了,希望有不足之处多多提出。

以上就是本文的全部内容,希望本文的内容对大家的学习或工作能带来一定的帮助,同时也希望多多支持呐喊教程!

声明:本文内容来源于互联网,版权归原作者所有。内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#oldtoolbag.com(在发送邮件时,请将#替换为@)进行举报,并提供相关证据。一经查实,本站将立即删除涉嫌侵权内容。

Empfohlen für Sie