心得经验总结:校园网自动登录app

简介: 心得经验总结:校园网自动登录app

  其实连接校园网不是个难题,就是传输一次post请求,之前也看到有大佬用python做到电脑上的自启动脚本。不过由于这次想做的是app,而app最好还是用java写,相比python会存在不少问题。

一、python脚本

  虽然要弄java,但还是借着之前看到的python脚本先理解一下思路。首先在校园网登录的界面用F12观察一下连接的时候post了哪些参数:

  可以看到主要有两个参数,DDDDD对应的是学号,形式是",0,学号和后缀",后缀如果是@cmcc就是中国移动,如果是@njxy就是中国电信,如果无后缀则是校园网;而upass即是密码。所以我们要手动实现一次登录,只需要post这两个参数就行,代码如下:

import requests

url=""

data={"DDDDD": ",0,学号和后缀","upass": "密码"}

response=requests.post(url,data)

  可以说是简单的不能再简单……不过我们的目标肯定不是一次登录,但是要是反复的post如果已经登录则也不太合理,参考大佬的脚本,发现有这样一个网址:"",ip后加上的是get""得到的v46ip的值,就能查看到是否连接(ok/fail),如果连接还能看到自己的学号和连接的后缀,所以我们设置每隔一段时间的循环,先用get检查是否连接,未连接则post信息。代码如下:

import requests

import requests

import re

import time

def getIp():

url = ""

res = requests.get(url = url)

res.encoding = 'gbk'

results = re.search("v46ip=\'(【^\'】)\'", res.text)

ip=results.group(1)

return ip

def checkIp(ip):

url = "".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=""

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、getpost

  网上有很多脚本,这里我用的是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,学号@cmccupass=密码"; // 修改学号密码

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(""));

ip = "" + ip;

status = http.re_search("\"result\":\"(【^\"】)\"", http.sendGet(ip));

if (status.equals("ok")) {

account = http.re_search("\"account\":\"(【^\"】*)\"", http.sendGet(ip));

} else {

http.sendPost("");

}

count++;

//代码效果参考:http://www.zidongmutanji.com/zsjx/14921.html

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("//代码效果参考:http://www.zidongmutanji.com/zsjx/267625.html

\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,学号@cmccupass=密码"; // 修改学号密码

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

  然而当我用j

相关文章
|
Linux
Linux Debian11安装QT6开发环境
Linux Debian11安装QT6开发环境。从Qt5.14开始,官方不提供离线安装包,只提供源码包和在线安装器。但是清华为我们提供了快速的在线安装方式。
2045 0
 Linux Debian11安装QT6开发环境
|
18天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
16731 61
|
7天前
|
域名解析 人工智能 Linux
|
1天前
|
人工智能 安全 网络安全
|
16天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
5天前
|
云安全 数据采集 人工智能
|
13天前
|
人工智能 开发工具 开发者
2026最新codex下载安装保姆级使用教程(下载+安装+登录+使用,含所有历史版本)
Codex 是 OpenAI 推出的开源 AI 编程智能体,跑在本地终端和桌面客户端里,能读懂整个代码仓库,一句自然语言就能让它改代码、执行命令、跑测试、修 Bug,支持 Windows、macOS、Linux 三大平台。本文把 Codex下载、安装、登录、汉化、基础使用一次讲全,并整理了国内用户最常踩的坑,跟着做 10 分钟就能跑起来。
2263 7
|
6天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1190 2
|
18天前
|
弹性计算 人工智能 API
Codex超详细入门指南(下载·安装·登录·上手,全版本覆盖,2026最新版)
建议收藏本教程,2026 全年各版本更新随时回来查阅。如果你在搭建过程中遇到任何问题,欢迎结合 阿里云工单系统 提交技术支持请求,或在 阿里云开发者社区 搜索 Codex 相关话题交流。 祝你编码愉快,效率翻倍!
3760 22