Skip to content

Instantly share code, notes, and snippets.

@YieldNull
Last active April 14, 2017 07:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YieldNull/68a9f10bdec9ec7e3a26eb775f619f1e to your computer and use it in GitHub Desktop.
Save YieldNull/68a9f10bdec9ec7e3a26eb775f619f1e to your computer and use it in GitHub Desktop.
登陆武汉大学教务系统
package whu;
import java.awt.*;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class WHU {
private static final String URL_CAPTCHA = "http://210.42.121.132/servlet/GenImg";
private static final String URL_LOGIN = "http://210.42.121.132/servlet/Login";
private static final String URL_INDEX = "http://210.42.121.132/stu/stu_index.jsp";
private static final File captchaFile = new File("captcha");
private static class Session {
private final String USER_AGENT = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)" +
" Ubuntu Chromium/51.0.2704.79 Chrome/51.0.2704.79 Safari/537.36";
private String cookie;
public void captcha(String url) throws IOException {
URLConnection conn = openConnection(url);
String cookieStr = conn.getHeaderField("Set-Cookie");
cookie = cookieStr.substring(0, cookieStr.indexOf(";"));
InputStream stream = conn.getInputStream();
Files.copy(stream, captchaFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
stream.close();
}
public String get(String url) throws IOException {
return readText(openConnection(url));
}
public String post(String url, Map<String, String> params) throws IOException {
StringBuilder builder = new StringBuilder();
for (Map.Entry<String, String> entry : params.entrySet()) {
builder.append(String.format("&%s=%s", entry.getKey(), entry.getValue()));
}
HttpURLConnection conn = openConnection(url);
conn.setRequestMethod("POST");
conn.setDoOutput(true);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream()));
writer.write(builder.substring(1));
writer.close();
return readText(conn);
}
private HttpURLConnection openConnection(String url) throws IOException {
HttpURLConnection conn = (HttpURLConnection) (new URL(url)).openConnection();
conn.setRequestProperty("User-Agent", USER_AGENT);
conn.setRequestProperty("Cookie", cookie);
return conn;
}
private String readText(URLConnection conn) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "gb2312"));
String line;
StringBuilder builder = new StringBuilder();
while ((line = reader.readLine()) != null) {
builder.append(line);
builder.append("\n");
}
reader.close();
return builder.toString();
}
}
public static void main(String[] args) throws IOException {
Session session = new Session();
String response;
do {
session.captcha(URL_CAPTCHA);
Desktop.getDesktop().browse(captchaFile.toURI());
System.out.print("captcha >> ");
Scanner scanner = new Scanner(System.in);
String captcha = scanner.nextLine();
Map<String, String> params = new HashMap<>();
params.put("id", "studentId"); // 学号
params.put("pwd", "password"); // MD5 加密后的密码
params.put("xdvfb", captcha);
response = session.post(URL_LOGIN, params);
} while (response.contains("验证码错误"));
System.out.println(session.get(URL_INDEX));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment