使用Java模拟登录
使用Java模擬登錄
- 運(yùn)行環(huán)境
- 步驟
- 瀏覽器階段
- 思路
- 編碼
- 使用python模擬登錄
運(yùn)行環(huán)境
本次開發(fā)在Windos上進(jìn)行,運(yùn)行環(huán)境為:
java 11.0.2 2019-01-15 LTS Java(TM) SE Runtime Environment 18.9 (build 11.0.2+9-LTS) Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.2+9-LTS, mixed mode)步驟
瀏覽器階段
本次模擬網(wǎng)站:https://www.bbaaz.com
其登錄界面網(wǎng)址為:https://www.bbaaz.com/member.php?mod=logging&action=login
先使用瀏覽器打開,輸入用戶名、密碼,然后F12打開Dev Tools,選擇Network選項(xiàng)卡,注意勾選Preserve log,不然可能的情況是,你點(diǎn)了登錄,由于package太多的原因,或者跳轉(zhuǎn)新頁面,關(guān)鍵的包已經(jīng)舍棄掉了。
找到關(guān)鍵包,如下圖,可以看到很多信息。
從package中可以獲取到:
仔細(xì)觀察,發(fā)現(xiàn)了什么,首先,Request URL帶有了一個(gè)loginhash,再Form Data中帶有了formhash,這些應(yīng)該是防止非法登錄的,那么這些數(shù)據(jù)哪里來的呢?經(jīng)過注銷,重新打開登陸界面,分析登錄界面,可以得知這些信息,都是原登錄界面的隱藏信息。
思路
那么我們的思路就是,首先,打開登錄界面,從登錄界面獲取兩個(gè)Hash值,再打開一個(gè)能存放Cookie的Request,給登錄URL發(fā)送表單,記錄返回的Cookie,再打開別的頁面的時(shí)候,就可以通過Cookie進(jìn)行訪問。
編碼
注意,以下編碼為了便于閱讀,進(jìn)行了修改,編碼習(xí)慣并不好,請(qǐng)不要學(xué)習(xí)編碼習(xí)慣。
final String loginURL = "https://www.bbaaz.com/member.php?mod=logging&action=login"; String query = "&loginsubmit=yes&frommessage&loginhash=xxx&inajax=1"; String form = "formhash=xxx&referer=https://www.bbaaz.com/&loginfield=username&username=xxxx&password=xxxx&questionid=0&answer="; final String headerAgent = "User-Agent"; final String headerAgentArg = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3562.0 Safari/537.36"; byte [] buffer; byte [] all; int length; ArrayList<byte []> byteList; ArrayList<Integer> byteLength; int totalLength = 0; String [] content = null; String formhash, suffix; //兩個(gè)Hash值try {// 在剛開始就使用CookieManager的原因是,經(jīng)過測(cè)試,在打開登錄界面的時(shí)候,服務(wù)器就會(huì)向客戶端發(fā)送3個(gè)Cookie,這些Cookie同樣有驗(yàn)證作用,如果不帶上,那么登錄的時(shí)候,服務(wù)器會(huì)返回,含有非法字符,無法登錄CookieManager manager = new CookieManager();manager.setCookiePolicy(CookiePolicy.ACCEPT_ORIGINAL_SERVER);CookieHandler.setDefault(manager);HttpURLConnection httpURLConnection = (HttpURLConnection) (new URL(loginURL).openConnection());httpURLConnection.setRequestMethod("GET");httpURLConnection.setRequestProperty(headerAgent, headerAgentArg);httpURLConnection.connect();if(httpURLConnection.getResponseCode() == 200) {InputStream inputStream = httpURLConnection.getInputStream();buffer = new byte[1024];byteList = new ArrayList<>();byteLength = new ArrayList<>();while( (length = inputStream.read(buffer)) != -1 ) {byteList.add(buffer);byteLength.add(length);totalLength += length;buffer = new byte[1024];}httpURLConnection.disconnect();all = new byte[totalLength];totalLength = 0;while(byteList.size() != 0) {System.arraycopy(byteList.get(0), 0, all, totalLength, byteLength.get(0));totalLength += byteLength.get(0);byteList.remove(0);byteLength.remove(0);}// 通過第一次打開的頁面獲取Hash值content = new String(all, "UTF-8").split("<form method=\"post\"")[1].split("name=\"formhash\" value=\"");all = null;suffix = content[0].split("loginform_", 2)[1].split("\"", 2)[0];formhash = content[1].split("\" />", 2)[0];// 第二次獲取網(wǎng)頁,會(huì)自動(dòng)攜帶Cookie信息httpURLConnection = (HttpURLConnection) (new URL(loginURL + query.replace("xxx", suffix)).openConnection());httpURLConnection.setRequestMethod("POST");httpURLConnection.setRequestProperty(headerAgent, headerAgentArg);httpURLConnection.setDoOutput(true);httpURLConnection.connect();httpURLConnection.getOutputStream().write(form.replace("xxx", formhash).getBytes("UTF-8"));inputStream = httpURLConnection.getInputStream();buffer = new byte[1024];byteList = new ArrayList<>();byteLength = new ArrayList<>();totalLength = 0;while( (length = inputStream.read(buffer)) != -1 ) {byteList.add(buffer);byteLength.add(length);totalLength += length;buffer = new byte[1024];}httpURLConnection.disconnect();all = new byte[totalLength];totalLength = 0;while(byteList.size() != 0) {System.arraycopy(byteList.get(0), 0, all, totalLength, byteLength.get(0));totalLength += byteLength.get(0);byteList.remove(0);byteLength.remove(0);}new String(all, "UTF-8"); // 查看該頁面信息,登錄成功all = null;} } catch (Exception e) {e.printStackTrace(); }經(jīng)測(cè)試,使用該方法,成功模擬登錄。
使用python模擬登錄
https://blog.csdn.net/yancr/article/details/88748521
總結(jié)
以上是生活随笔為你收集整理的使用Java模拟登录的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 猿创征文 |【算法入门必刷】数据结构-栈
- 下一篇: Java-强制类型转换