城堡
前言
很早之前用別的語言寫過一次微博登陸,現在改成java版,主要是為了驗證jsoup的強大,一款完全可勝任http請求和html解析的工具。抓包,抽取js的方法這里就不在贅述。
工具
- jsoup-1.11.2(使用最新)
- 打碼賬號及官方提供的java類(市面上很多,這里用若快)
開始
- 首先是分析登錄過程,常登陸的沒有驗證碼,新的會有驗證碼,保險起見這里都采用有驗證碼的方式登錄。
- Base64編碼用戶名后,get請求獲取servertime,nonce,pubkey,pcid,及返回的cookie
//Base64編碼用戶名 su = new BASE64Encoder().encode(usename.getBytes()); String url = "http://login.sina.com.cn/sso/prelogin.php?entry=weibo&callback=sinaSSOController.preloginCallBack&su=" + su + "&rsakt=mod&checkpin=1&client=ssologin.js(v1.4.18)&_=" + getTimestamp(); Connection.Response execute = Jsoup.connect(url).ignoreContentType(true).execute(); //獲取返回數據 String body = execute.body(); //獲取返回cookie Map<String, String> photocookie = execute.cookies(); JSONObject jsonObject = JSONObject.parseObject(StringUtils.substringBetween(body, "(", ")")); servertime = jsonObject.getString("servertime"); nonce = jsonObject.getString("nonce"); pubkey = jsonObject.getString("pubkey"); pcid = jsonObject.getString("pcid");
- 帶著返回的cookie及pcid,get請求獲取驗證碼
url = "http://login.sina.com.cn/cgi/pin.php?r=54474015&s=0&p=" + pcid; byte[] bytes = Jsoup.connect(url).ignoreContentType(true).cookies(photocookie).execute().bodyAsBytes();
- 調用打碼平臺的類實現打碼,返回結果
public static String createByPost(String username, String password, String typeid, String timeout, String softid, String softkey,byte[] byteArr) { String result = ""; String param = String .format( "username=%s&password=%s&typeid=%s&timeout=%s&softid=%s&softkey=%s", username, password, typeid, timeout, softid, softkey); try { result = RuoKuai.httpPostImage("http://api.ruokuai.com/create.xml", param, byteArr); // jsoup去解析xml (略坑,加了好多參數進去) result = Jsoup.parse(result).select("body > root > result").text(); } catch(Exception e) { result = "未知問題"; } return result; }
- 調用js,對密碼動態加密,返回參數sp
private boolean encodePwd() { ScriptEngineManager sem = new ScriptEngineManager(); ScriptEngine se = sem.getEngineByName("javascript"); try { // FileReader fr = new FileReader(""); se.eval(LOGIN_JS); Invocable invocableEngine = (Invocable) se; sp = (String) invocableEngine.invokeFunction("getPW", password, servertime, nonce, pubkey); return true; } catch (ScriptException e) { } catch (NoSuchMethodException e) { } return false; }
- 帶著參數pcid,code,sp,su,servertime,nonce,sp及cookie,post請求返回cookies及下一個url,此時可從返回的body中判斷登錄成功與否
//retcode=101 賬號密碼錯誤 //retcode=80 請輸入正確的密碼 //retcode=4049 輸入驗證碼 //retcode=2070 驗證碼錯誤 url = "http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.18)"; String post = "entry=weibo&gateway=1&from=&savestate=7&useticket=1&pagerefer=http%3A%2F%2Fpassport.weibo" + ".com%2Fvisitor%2Fvisitor%3Fentry%3Dminiblog%26a%3Denter%26url%3Dhttp%253A%252F%252Fweibo.com%252F%26domain%3D.weibo" + ".com%26ua%3Dphp-sso_sdk_client-0.6.14%26_rand%3D1441434306.495&pcid=" + pcid + "&door=" + code + "&vsnf=1&su=" + su + "&service=miniblog&servertime=" + servertime + "&nonce=" + nonce + "&pwencode=rsa2&rsakv=1330428213&sp=" + sp + "&sr=1366*768&encoding=UTF-8&url=http%3A%2F%2Fweibo.com%2Fajaxlogin.php%3Fframelogin%3D1%26callback%3Dparent.sinaSSOController.feedBackUrlCallBack&returntype=META"; Connection.Response execute1 = Jsoup.connect(url).method(Connection.Method.POST).requestBody(post).cookies(photocookie).ignoreContentType(true).execute().charset("GBK"); String body1 = execute1.body(); Map<String, String> cookies = execute1.cookies(); System.out.println(body1); System.out.println(body1.indexOf("正在登錄") != -1 ? "登錄成功" : "登錄失敗"); url = StringUtils.substringBetween(body1, "location.replace('", "'");
- 帶著上次返回的cookie,get請求(禁止重定向)獲取最終的cookie
Connection.Response execute2 = Jsoup.connect(url).cookies(cookies).followRedirects(false).ignoreContentType(true).execute(); Map<String, String> cookies1 = execute2.cookies(); //從返回協議頭中獲取location,重定向地址,一般是固定 Map<String, String> headers = execute2.headers();
結束
獲取到cookie,基本之后的操作都可以,那就簡單的發條微博,發微博其實只帶post數據和cookie就能提交,協議頭完全可以不帶,偏偏jsoup默認帶個UA,結果要多帶個refrere頭才可以。
- 通過步驟7,獲取的重定向地址,get請求獲取refrere參數uniqueid
url = "http://weibo.com/ajaxlogin.php?framelogin=1&callback=parent.sinaSSOController.feedBackUrlCallBack&sudaref=login.sina.com.cn"; String body2 = Jsoup.connect(url).cookies(cookies1).ignoreContentType(true).execute().body(); String uid = StringUtils.substringBetween(body2, "uniqueid\":\"", "\",\"userid"); String referer = "https://weibo.com/u/" + uid + "/home";
- 帶著cookie,及協議頭referer,post請求,發一條微博吧
url = "https://weibo.com/aj/mblog/add?ajwvr=6&__rnd=" + getTimestamp(); post = "location=v6_content_home&appkey=&style_type=1&pic_id=&text=" + text + "&pdetail=&rank=0&rankid=&module=stissue&pub_source=main_&pub_type=dialog&_t=0"; Connection.Response execute3 = Jsoup.connect(url).method(Connection.Method.POST).requestBody(post).cookies(cookies1).referrer(referer).ignoreContentType(true).execute().charset("GBK"); System.out.println(execute3.body());
Success