其實連接校園網不是個難題,就是傳輸一次post請求,之前也看到有大佬用python做到電腦上的自啟動腳本。不過由於這次想做的是app,而app最好還是用java寫,相比python會存在不少問題。
https://www.cnblogs.com/chi4ki/p/16068429.html
一、python腳本
雖然要弄java,但還是借着之前看到的python腳本先理解一下思路。首先在校園網登錄的界面用F12觀察一下連接的時候post了哪些參數:
可以看到主要有兩個參數,DDDDD對應的是學號,形式是",0,學號和后綴",后綴如果是@cmcc就是中國移動,如果是@njxy就是中國電信,如果無后綴則是校園網;而upass即是密碼。所以我們要手動實現一次登錄,只需要post這兩個參數就行,代碼如下:
import requests url="http://10.10.244.11:801/eportal/c=ACSetting&a=Login&wlanacip=10.255.252.150" data={"DDDDD": ",0,學號和后綴","upass": "密碼"} response=requests.post(url,data)
可以說是簡單的不能再簡單……不過我們的目標肯定不是一次登錄,但是要是反復的post如果已經登錄則也不太合理,參考大佬的腳本,發現有這樣一個網址:"http://p.njupt.edu.cn:801/eportal/?c=ACSetting&a=checkScanIP&wlanuserip=",ip后加上的是get"http://p.njupt.edu.cn/"得到的v46ip的值,就能查看到是否連接(ok/fail),如果連接還能看到自己的學號和連接的后綴,所以我們設置每隔一段時間的循環,先用get檢查是否連接,未連接則post信息。代碼如下:
import requests import requests import re import time def getIp(): url = "http://p.njupt.edu.cn/" res = requests.get(url = url) res.encoding = 'gbk' results = re.search("v46ip=\'([^\']*)\'", res.text) ip=results.group(1) return ip def checkIp(ip): url = "http://p.njupt.edu.cn:801/eportal/?c=ACSetting&a=checkScanIP&wlanuserip={}".format(ip) res = requests.get(url = url) status = re.search('\"result\":\"([^\"]*)\"', res.text).group(1) if (status == 'ok'): account = re.search('\"account\":\"([^\"]*)\"', res.text).group(1) return account else: return False def login(): url="http://10.10.244.11:801/eportal/?c=ACSetting&a=Login&wlanacip=10.255.252.150" data={"DDDDD": ",0,學號@cmcc","upass": "密碼"} #修改學號和密碼 response=requests.post(url,data) return True while True: try: ip = getIp() if checkIp(ip) == False: login() except: pass time.sleep(5)
二、java腳本
可以說思路和python一樣,但是我們需要找到這幾個函數在java中的實現方法:requests.get、requests.post、re.search
1、re.search
用Pattern類來實現
private String re_search(String pa, String li) { String line = li; String pattern = pa; Pattern r = Pattern.compile(pattern); Matcher m = r.matcher(line); if (m.find()) { return m.group(1); } else { return "null"; } }
2、get&post
網上有很多腳本,這里我用的是java自帶的HttpsURLConnection。
private final String USER_AGENT = "Mozilla/5.0"; private String sendGet(String Url) throws Exception { String url = Url; URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); con.setRequestMethod("GET"); con.setRequestProperty("User-Agent", USER_AGENT); int responseCode = con.getResponseCode(); System.out.println("\nSending 'GET' request to URL : " + url); System.out.println("Response Code : " + responseCode); BufferedReader in = new BufferedReader( new InputStreamReader(con.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); return response.toString(); } private void sendPost(String Url) throws Exception { String url = Url; URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); con.setRequestMethod("POST"); con.setRequestProperty("User-Agent", USER_AGENT); con.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); String urlParameters = "DDDDD=,0,學號@cmcc&upass=密碼"; // 修改學號密碼 con.setDoOutput(true); DataOutputStream wr = new DataOutputStream(con.getOutputStream()); wr.writeBytes(urlParameters); wr.flush(); wr.close(); int responseCode = con.getResponseCode(); System.out.println("\nSending 'POST' request to URL : " + url); System.out.println("Post parameters : " + urlParameters); System.out.println("Response Code : " + responseCode); BufferedReader in = new BufferedReader( new InputStreamReader(con.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); }
3、完整代碼
import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import javax.net.ssl.HttpsURLConnection; import java.util.regex.Matcher; import java.util.regex.Pattern; public class HttpURLConnectionExample { private final String USER_AGENT = "Mozilla/5.0"; public static void main(String[] args) throws Exception { HttpURLConnectionExample http = new HttpURLConnectionExample(); String ip, status, account = ""; int count = 0; while (true) { try { Thread.sleep(5 * 1000); ip = http.re_search("v46ip=\'([^\']*)\'", http.sendGet("http://p.njupt.edu.cn/")); ip = "http://p.njupt.edu.cn:801/eportal/?c=ACSetting&a=checkScanIP&wlanuserip=" + ip; status = http.re_search("\"result\":\"([^\"]*)\"", http.sendGet(ip)); if (status.equals("ok")) { account = http.re_search("\"account\":\"([^\"]*)\"", http.sendGet(ip)); } else { http.sendPost("http://10.10.244.11:801/eportal/?c=ACSetting&a=Login&wlanacip=10.255.252.150"); } count++; System.out.println(count); } catch (InterruptedException e) { e.printStackTrace(); } } } private String re_search(String pa, String li) { String line = li; String pattern = pa; Pattern r = Pattern.compile(pattern); Matcher m = r.matcher(line); if (m.find()) { return m.group(1); } else { return "null"; } } private String sendGet(String Url) throws Exception { String url = Url; URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); con.setRequestMethod("GET"); con.setRequestProperty("User-Agent", USER_AGENT); int responseCode = con.getResponseCode(); System.out.println("\nSending 'GET' request to URL : " + url); System.out.println("Response Code : " + responseCode); BufferedReader in = new BufferedReader( new InputStreamReader(con.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); return response.toString(); } private void sendPost(String Url) throws Exception { String url = Url; URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); con.setRequestMethod("POST"); con.setRequestProperty("User-Agent", USER_AGENT); con.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); String urlParameters = "DDDDD=,0,學號@cmcc&upass=密碼"; // 修改學號密碼 con.setDoOutput(true); DataOutputStream wr = new DataOutputStream(con.getOutputStream()); wr.writeBytes(urlParameters); wr.flush(); wr.close(); int responseCode = con.getResponseCode(); System.out.println("\nSending 'POST' request to URL : " + url); System.out.println("Post parameters : " + urlParameters); System.out.println("Response Code : " + responseCode); BufferedReader in = new BufferedReader( new InputStreamReader(con.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); } }
三、校園網自動登錄app
然而當我用java在電腦上寫好的時候才意識到get和post在windowst和android不太一樣……android相比windows還要解決以下幾個問題:
1、get&post
2、面向用戶時數據的讀取和存儲
3、下拉框(選擇移動電信還是校園網)
1、get&post
注意的是要首先加上發送網絡請求的權限,除此以外注意android studio要求網絡連接只能在子線程中進行。
網絡權限:在AndroidManifest.xml中添加
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
和android:usesCleartextTraffic="true"
位置如下:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.network"> <!--第一處--> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.Network" android:usesCleartextTraffic="true"> <!--第二處--> <activity android:name=".MainActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
get&post:
HttpURLConnection connection ; String line,pattern,param,results; int responseCode; //get的用法,用String獲取get的訪問值來滿足對ip和狀態的確定 public String sendGet(String ip){ try { connection = null; URL url = new URL(ip); connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(3000); connection.setReadTimeout(3000); connection.setRequestMethod("GET"); connection.setDoInput(true); connection.setDoOutput(false); connection.connect(); responseCode = connection.getResponseCode(); if (responseCode != HttpURLConnection.HTTP_OK) { throw new IOException("HTTP error code" + responseCode); } results = getStringByStream(connection.getInputStream()); return results; }catch (IOException e) { e.printStackTrace(); return "error"; } } //post 因為這里用不到返回值所以void就行 public void sendPost(String ip,String pa){ try { connection = null; URL url = new URL(ip); connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(3000); connection.setReadTimeout(3000); connection.setRequestMethod("POST"); connection.setDoInput(true); connection.setDoOutput(false); connection.connect(); DataOutputStream dos=new DataOutputStream(connection.getOutputStream()); param=pa; dos.writeBytes(param); responseCode = connection.getResponseCode(); if (responseCode != HttpURLConnection.HTTP_OK) { throw new IOException("HTTP error code" + responseCode); } }catch (IOException e) { e.printStackTrace(); } } //用於返回值的處理 private String getStringByStream(InputStream inputStream) { Reader reader; StringBuffer buffer; try { reader = new InputStreamReader(inputStream, "UTF-8"); char[] rawBuffer = new char[512]; buffer = new StringBuffer(); int length; while ((length = reader.read(rawBuffer)) != -1) { buffer.append(rawBuffer, 0, length); } return buffer.toString(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; }
開啟一個子線程並調用:
new Thread(()->{ //先做個連接,因為有時候這個ip還不是對應自己的 sendPost(ip3,content); //做個計數 int count=1; while(true) { try{ //每20秒進行一次的循環 Thread.sleep(20000); text = sendGet(ip1); ip = re_search("v46ip='([^']*)'", text); ip2 += ip; text = sendGet(ip2); result = re_search("\"result\":\"([^\"]*)\"", text); if (!result.equals("ok")) { //不是ok的時候發起連接 Log.d("tag",content+String.valueOf(count)); count++; } else { //已經連上時彈出消息框已經連接 runOnUiThread(() -> Toast.makeText(getApplicationContext(), "connected", Toast.LENGTH_SHORT).show()); Log.d("tag",String.valueOf(count)); count++; } }catch (Exception e) { e.printStackTrace(); } } }).start();
2、數據的永久性存儲
因為涉及需要存儲的信息很少,所以用SharedPreferences就行。
//獲取SharedPreferences對象 SharedPreferences.Editor editor = getSharedPreferences("lock", MODE_PRIVATE ).edit(); //添加數據 editor.putString("name", name); editor.putString("code", code); editor.apply(); //讀取數據 SharedPreferences read = getSharedPreferences("lock", MODE_PRIVATE ); read.getString("name", ""); read.getString("code", "");
3、下拉框
spinner的使用,由於只是選擇和讀取所以弄個監聽器就行。在values.xml里加入我們需要的選項,並在下拉框里加入android:entries="@array/choice"。
values.xml:
<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="choice"> <item>中國移動</item> <item>中國電信</item> <item>校園網</item> </string-array> </resources>
監聽函數:
//下拉框的監聽 private class ProvOnItemSelectedListener implements AdapterView.OnItemSelectedListener { @Override public void onItemSelected(AdapterView<?> adapter,View view,int position,long id) { String sInfo=adapter.getItemAtPosition(position).toString(); if(sInfo.equals("中國移動")) p1.setType("@cmcc"); if(sInfo.equals("中國電信")) p1.setType("@njxy"); if(sInfo.equals("校園網")) p1.setType("校園網"); } @Override public void onNothingSelected(AdapterView<?> arg0) { String sInfo="null"; } }
4、完整代碼
Person.java
package com.example.network_2; public class Person{ String id; String password; String type; public Person(){ } public void set(String i,String p,String t){ id=i; password=p; type=t; } public void setType(String t){ type=t; } public String getId(){ return id; } public String getPassword(){ return password; } public String getType(){ return type; } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <EditText android:id="@+id/edt1" android:layout_width="261dp" android:layout_height="61dp" android:autofillHints="username" android:ems="10" android:hint="請輸入學號" android:inputType="textPersonName" android:textSize="24sp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.44" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.134" /> <EditText android:id="@+id/edt2" android:layout_width="261dp" android:layout_height="61dp" android:autofillHints="password" android:ems="10" android:hint="請輸入密碼" android:inputType="textPassword" android:textSize="24sp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.433" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.225" /> <Button android:id="@+id/btn1" android:layout_width="238dp" android:layout_height="107dp" android:text="自動連接校園網" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.514" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.62" /> <Button android:id="@+id/btn2" android:layout_width="121dp" android:layout_height="66dp" android:text="修改" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.944" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.407" /> <Spinner android:id="@+id/spinner" android:layout_width="262dp" android:layout_height="48dp" android:entries="@array/choice" app:layout_constraintBottom_toTopOf="@+id/btn1" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.436" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/edt2" app:layout_constraintVertical_bias="0.0" /> </androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.network_2; import androidx.appcompat.app.AppCompatActivity; import android.content.SharedPreferences; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.Button; import android.widget.EditText; import android.widget.Spinner; import android.widget.Toast; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.URL; import java.util.regex.Matcher; import java.util.regex.Pattern; public class MainActivity extends AppCompatActivity { String ip,text,result,content,name,code; String ip1="http://p.njupt.edu.cn/"; String ip2="http://p.njupt.edu.cn:801/eportal/?c=ACSetting&a=checkScanIP&wlanuserip="; String ip3= "http://10.10.244.11:801/eportal/?c=ACSetting&a=Login&wlanacip=10.255.252.150"; Person person=new Person(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //給兩個按鈕設置監聽 Button btn1=findViewById(R.id.btn1); btn1.setOnClickListener(new BtnClickListener()); Button btn2=findViewById(R.id.btn2); btn2.setOnClickListener(new BtnClickListener()); //給下拉框設置監聽 Spinner spinner=findViewById(R.id.spinner); spinner.setOnItemSelectedListener(new ProvOnItemSelectedListener()); } //按鈕的監聽 class BtnClickListener implements View.OnClickListener { @Override public void onClick(View v){ Integer id=v.getId(); if(id.equals(R.id.btn2)) setUp(); if(id.equals(R.id.btn1)) connect(); } } //下拉框的監聽 private class ProvOnItemSelectedListener implements AdapterView.OnItemSelectedListener { @Override public void onItemSelected(AdapterView<?> adapter,View view,int position,long id) { String sInfo=adapter.getItemAtPosition(position).toString(); if(sInfo.equals("中國移動")) person.setType("@cmcc"); if(sInfo.equals("中國電信")) person.setType("@njxy"); if(sInfo.equals("校園網")) person.setType("校園網"); } @Override public void onNothingSelected(AdapterView<?> arg0) { String sInfo="null"; } } //連接的步驟 public void connect(){ EditText txtName=findViewById(R.id.edt1); EditText txtCode=findViewById(R.id.edt2); //讀取存儲的內容 SharedPreferences read = getSharedPreferences("lock", MODE_PRIVATE ); name=read.getString("name", ""); code=read.getString("code", ""); person.set(name, code,read.getString("type","")); //將兩行輸入框設置為我們現在用的學號和密碼 txtName.setText(name); txtCode.setText(code); //因為選擇校園網的時候后綴為空但這里不能直接設置為空,所以我們加個判斷來區分選擇校園網的時候 if(person.getType().equals("校園網")) { content = "DDDDD=,0," + person.getId() + "&upass=" + person.getPassword(); }else{ content = "DDDDD=,0," + person.getId() + person.getType() + "&upass=" + person.getPassword(); } //開啟子線程 new Thread(()->{ //先做個連接,因為有時候這個ip還不是對應自己的 sendPost(ip3,content); //做個計數 int count=1; while(true) { try{ //每20秒進行一次的循環 Thread.sleep(20000); text = sendGet(ip1); ip = re_search("v46ip='([^']*)'", text); ip2 += ip; text = sendGet(ip2); result = re_search("\"result\":\"([^\"]*)\"", text); if (!result.equals("ok")) { //不是ok的時候發起連接 sendPost(ip3,content); Log.d("tag",content+String.valueOf(count)); count++; } else { //已經連上時彈出消息框已經連接 runOnUiThread(() -> Toast.makeText(getApplicationContext(), "connected", Toast.LENGTH_SHORT).show()); Log.d("tag",String.valueOf(count)); count++; } }catch (Exception e) { e.printStackTrace(); } } }).start(); } //修改學號密碼 public void setUp(){ //讀取輸入框的值 EditText txtName=findViewById(R.id.edt1); EditText txtCode=findViewById(R.id.edt2); name = txtName.getText().toString().trim(); code = txtCode.getText().toString().trim(); //永久性存儲數據 SharedPreferences.Editor editor = getSharedPreferences("lock", MODE_PRIVATE ).edit(); editor.putString("name", name); editor.putString("code", code); editor.putString("type", person.getType()); editor.apply(); //提示消息設置成功 Toast.makeText(getApplicationContext(), "設置成功", Toast.LENGTH_SHORT).show(); } //以下是Get、Post以及search函數 HttpURLConnection connection ; String line,pattern,param,results; int responseCode; //get的用法,用String獲取get的訪問值來滿足對ip和狀態的確定 public String sendGet(String ip){ try { connection = null; URL url = new URL(ip); connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(3000); connection.setReadTimeout(3000); connection.setRequestMethod("GET"); connection.setDoInput(true); connection.setDoOutput(false); connection.connect(); responseCode = connection.getResponseCode(); if (responseCode != HttpURLConnection.HTTP_OK) { throw new IOException("HTTP error code" + responseCode); } results = getStringByStream(connection.getInputStream()); return results; }catch (IOException e) { e.printStackTrace(); return "error"; } } //post 因為這里用不到返回值所以void就行 public void sendPost(String ip,String pa){ try { connection = null; URL url = new URL(ip); connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(3000); connection.setReadTimeout(3000); connection.setRequestMethod("POST"); connection.setDoInput(true); connection.setDoOutput(false); connection.connect(); DataOutputStream dos=new DataOutputStream(connection.getOutputStream()); param=pa; dos.writeBytes(param); responseCode = connection.getResponseCode(); if (responseCode != HttpURLConnection.HTTP_OK) { throw new IOException("HTTP error code" + responseCode); } }catch (IOException e) { e.printStackTrace(); } } //用於返回值的處理 private String getStringByStream(InputStream inputStream) { Reader reader; StringBuffer buffer; try { reader = new InputStreamReader(inputStream, "UTF-8"); char[] rawBuffer = new char[512]; buffer = new StringBuffer(); int length; while ((length = reader.read(rawBuffer)) != -1) { buffer.append(rawBuffer, 0, length); } return buffer.toString(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } //模擬python中的re.search public String re_search(String pat, String li) { line = li; pattern = pat; Pattern r = Pattern.compile(pattern); Matcher m = r.matcher(line); if (m.find()) { return m.group(1); } else { return "null"; } } }
(比較好看的代碼顯示識別開啟線程的lambda表達式時會整個花掉,所以為了清楚一點就只能用這個了)
運行效果如下:
以上,不過也就只有在學校里能用,主要是寫一下思路。