目录
- 1. 前言
 - 2. 分析
 - 3. 编码
 - 3.1 Python版
 - 3.1.1 编写Python脚本
 - 3.1.2 下载Python转EXE程序工具
 - 3.1.3 打包成EXE程序
 
- 3.2 Java版
 - 3.2.1 编写代码
 
- 4. 最后一步
 - 5. 总结
 
1. 前言
由于学校机房联网时,总是需要登录个人账号。为实现快速登录,我们就此问题给出了自己的解决方案。
2. 分析
环境介绍
 网站:http://10.10.10.5/0.htm
 注意:此网站为学校内部网站,其他网站需要根据实际情况作出修改。
  打开网站,并打开开发者工具。
   在登录页面中,我们去分析它发出的post请求,其中包含了我们需要的账号和密码信息。我们便可利用这些信息,通过程序来发送一个post请求。

3. 编码
3.1 Python版
此版本需要电脑上有Python环境。
3.1.1 编写Python脚本
右键创建一个FastLogin.py文件,并将以下内容写入。
import requests
import sys
name = ""     #此处填写用户名
password = "" #此处填写密码def login():url = "http://10.10.10.5/0.htm"headers = {"User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36","Cookie":"myusername="+name+"; username="+name+"; smartdot="+password,"Accept-Encoding":"gzip, deflate","Content-Type":"application/x-www-form-urlencoded"}data = {"DDDDD":name,"upass":password,"0MKKey":"%B5%C7%A1%A1%C2%BC","v6ip":""}res = requests.post(url=url,data=data,headers=headers)tip(res,"登录")def logout():url = "http://10.10.10.5/F.htm"headers = {"User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36","Cookie":"myusername="+name+"; username="+name+"; smartdot="+password,"Accept-Encoding":"gzip, deflate","Content-Type":"application/x-www-form-urlencoded"}res = requests.get(url=url,headers=headers)tip(res,"注销")def tip(res,operation):if res.status_code==200:print(operation+"成功")else:print(operation+"失败,错误信息")print(res.text)if __name__=="__main__":if len(sys.argv)==1 or sys.argv[1]=='login':login()elif sys.argv[1]=='logout':logout()else:print("操作不支持") 
将上面的用户名和密码改成自己的账号即可。到这里就可以实现自动登录了,但是这个脚本需要Python环境。为实现在没有Python环境的电脑上也能运行,我们将其打包成EXE程序。
3.1.2 下载Python转EXE程序工具
  在命令行执行以下命令,安装pyinstaller工具。
pip install pyinstaller
 
3.1.3 打包成EXE程序
执行以下命令完成打包操作。
pyinstaller -F -i favicon.ico FastLogin.py -n 快速登录
 
  其中-i表示添加的图标favicon.ico,-n表示将生成程序命名为快速登录,可根据情况添加。
   执行完成后,会在当前目录下生成一些文件和目录,我们在dist目录中取得所要的程序。
 注意:直接执行此命令,要求上面的脚本文件要以UTF-8格式存储。
3.2 Java版
  此版本使用到java.net.http.HttpClient需要Java11以上的环境。
3.2.1 编写代码
创建一个FastLogin.java的文件,并将以下内容写入。
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;public class FastLogin {private static String name="";     //此处填写用户名private static String password=""; //此处填写些密码private static void login() throws IOException, InterruptedException {HttpClient client = HttpClient.newHttpClient();HttpRequest request = HttpRequest.newBuilder().uri(URI.create("http://10.10.10.5/0.htm")).header("User-Agent","Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36").header("Cookie","myusername="+name+"; username="+name+"; smartdot="+password).header("Accept-Encoding","gzip, deflate").header("Content-Type","application/x-www-form-urlencoded").POST(HttpRequest.BodyPublishers.ofString("DDDDD="+name+"&upass="+password+"&0MKKey=%B5%C7%A1%A1%C2%BC&v6ip=")).build();HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString());checkStatus(response.statusCode());}private static void checkStatus(int statusCode) {if (statusCode==200){System.out.println("Success!!!");}else{System.out.println("Cao!");}}private static void logout() throws IOException, InterruptedException {HttpClient client = HttpClient.newHttpClient();HttpRequest request = HttpRequest.newBuilder().uri(URI.create("http://10.10.10.5/F.htm")).header("User-Agent","Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36").header("Cookie","myusername="+name+"; username="+name+"; smartdot="+password).header("Accept-Encoding","gzip, deflate").header("Content-Type","application/x-www-form-urlencoded").build();HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString());checkStatus(response.statusCode());}public static void main(String[] args) {try {if (args.length==0){login();}else if(args.length==1 && "logout".equalsIgnoreCase(args[0])){logout();}else{System.out.println("please input again.");}} catch (IOException | InterruptedException e) {System.out.println("Error happened!");e.printStackTrace();}}
}
 
  同样的修改用户名和密码,使用javac FastLogin编译即可。
4. 最后一步
  新建一个批处理文件启动.bat写入以下信息。完成后,双击即可来到学习通网站。
echo off
@Rem Python版打包成exe程序,仅需要程序名即可
FastLogin
@Rem Java版
@Rem java FastLogin
@Rem 打开学习通网站
explorer http://i.mooc.chaoxing.com/space/index.shtml
@Rem 需要的可以写入学校官网链接
 
5. 总结
  本文只介绍了通过发送post请求来实现网络登录的认证。但若想在浏览器页面实现自动登录,只靠发送post请求是不够的。因为,我们向服务器发送post请求,而服务器响应的信息,此程序并不能够像浏览器一样解析和展示,所以也没有实际意义。而且,我也不能通过外部手段像浏览器写入Cookie信息。所以,需要其他的办法。
   在另一篇文章中,介绍了通过Selenium来模拟用户登录,想要更进一步的可以通过此传送门查看。















