- 准备工具:Chrome , IDEA , JDK1.8
- 步骤:
要实现同时发送用户名,密码,验证码发送到服务器,不可能一上来就发送,因为验证码一开始要从服务器获取,所以有以下思路:
- 模拟打开主页
- 模拟验证码刷新
- 登陆
模拟打开主页后,验证码是在服务器端生成图片再返回给客户端,so,怎么才能得到它呢,将服务器返回的数据写到本地文件中,再手动打开该图片文件即可看到验证码
登陆:Post发送表单相关数据给服务器
通过Chrome抓包可以看到需要发送的数据有哪些:
图中的FormData就是表单中相关的数据
__VIEWSTATE:是.net语言生成的,听说每次登陆都不一样,那要怎么解决?一开始就进入了教务系统的主页,就可以从返回的html源码中通过正则表达式截取
/**
* 通过正则得到viewstate
*
* @param result html源码
*/
private static String getViewState(String result) {
Pattern p = Pattern.compile("name=\"__VIEWSTATE\" value=\"(.*?)\"");
Matcher m = p.matcher(result);
while (m.find()) {
result = m.group();
}
String viewState = result.substring(26, result.length() - 1);
System.out.println(viewState);
return viewState;
}
txtUserName 是用户名
TextBox2 是密码
txtSecretCode 是验证码
其他字段我也不知道有什么用了,反正它们每次都不会变
通过上面的步骤已经把需要的数据都得到了,学号跟密码就不用再描述了
下面直接通过post方式登陆,将以上的数据存到一个NameValuePair[]中.
再用httpClient进行Post方式网络访问即可.
登陆成功后,判断返回的html源码,如果有欢迎两个字,就是登陆成功了,再选择相应功能,继续Get方式网络访问
本demo中实现的有查看成绩,查看课表,
查看等级考试成绩
其他功能均可以通过抓包查看请求的url实现.
源码如下:
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.springframework.http.HttpStatus;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 步骤1:进入default2.aspx(Get)
* 步骤2:得到__VIEWSTATE,模拟刷新验证码,将验证码保存到电脑
* 步骤3:登陆(Post)
* Created by CHG on 2016-11-02 14:05.
*/
public class JavaPost {
private static Scanner sc = new Scanner(System.in);
private static String url_checkCode = "http://218.75.197.124:83/CheckCode.aspx?";
private static String userName = "13408200240";
private static String password = "this is a secret";
private static String checkCode = "";
private static String viewState = "";
private static HttpClient client = new HttpClient();
private static HttpUtils httpUtils = new HttpUtils();
private static String urlReferer = "http://218.75.197.124:83/xs_main.aspx?xh=13408200240";
public static void main(String[] args) {
String result = getdefault2Page();
viewState = getViewState(result);
downloadCheckCode();
System.out.print("e盘根目录查看验证码并输入控制台:");
checkCode = sc.next();
System.out.println("您输入的验证码为:" + checkCode);
getMainPage();
getxs_main_get();
mainMenu();
}
/**
* 功能主菜单
*/
private static void mainMenu() {
System.out.println("---------------------------");
System.out.print("查看成绩请输入1");
System.out.println();
System.out.print("查看课表请输入2");
System.out.println();
System.out.print("查看等级考试成绩请输入3");
int i = sc.nextInt();
switch (i) {
case 1: //查看课表
getStudentCJ();
mainMenu();
break;
case 2: //查看课表
getStudentKB();
mainMenu();
break;
case 3: //等级考试成绩查询
getMainTestCJ();
mainMenu();
break;
default:
break;
}
}
/**
* 等级考试成绩查询
*/
private static void getMainTestCJ() {
String url ="http://218.75.197.124:83/xsdjkscx.aspx?xh=13408200240";
String result = httpUtils.getDataGet(client, url, urlReferer, false);
saveAsHTML(result,"等级考试成绩");
}
/**
* 查看课表
*/
private static void getStudentKB() {
//http://218.75.197.124:83/xskbcx.aspx?xh=13408200240
String url = "http://218.75.197.124:83/xskbcx.aspx?xh=13408200240";
String result = httpUtils.getDataGet(client, url, urlReferer, true);
saveAsHTML(result,"课表");
}
/**
* 查看成绩
*/
private static void getStudentCJ() {
String url = "http://218.75.197.124:83/xscjcx.aspx?xh=13408200240";
String result = httpUtils.getDataGet(client, url, urlReferer, true);
saveAsHTML(result,"成绩");
}
/**
* 再次302跳转到主页
*/
private static void getxs_main_get() {
String url = "http://218.75.197.124:83/xs_main.aspx?xh=13408200240";
String result = httpUtils.getDataGet(client, url, urlReferer, false);
if (result.contains("欢迎"))
System.out.println("登陆成功");
}
/**
* 封装好所有参数,post
* 进入主页
*/
private static void getMainPage() {
String url = "http://218.75.197.124:83/default2.aspx";
NameValuePair[] nameValues = new NameValuePair[]{
new NameValuePair("__VIEWSTATE", viewState),
new NameValuePair("txtUserName", userName),
new NameValuePair("TextBox2", password),
new NameValuePair("txtSecretCode", checkCode),
new NameValuePair("RadioButtonList1", "%D1%A7%C9%FA"),
new NameValuePair("Button1", ""),
new NameValuePair("lbLanguage", ""),
new NameValuePair("hidPdrs", ""),
new NameValuePair("hidsc", ""),
};
httpUtils.getDataPost(client, url, nameValues);
}
/**
* 通过正则得到viewstate
*
* @param result
*/
private static String getViewState(String result) {
Pattern p = Pattern.compile("name=\"__VIEWSTATE\" value=\"(.*?)\"");
Matcher m = p.matcher(result);
while (m.find()) {
result = m.group();
}
String viewState = result.substring(26, result.length() - 1);
System.out.println(viewState);
return viewState;
}
/**
* 下载验证码and保存
*/
private static void downloadCheckCode() {
new Thread() {
@Override
public void run() {
super.run();
String url = "http://218.75.197.124:83/CheckCode.aspx?";
InputStream is = null;
OutputStream os = null;
File file = new File("e:\\checkcode.png");
try {
os = new FileOutputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
GetMethod getMethod = new GetMethod(url);
try {
int status = client.executeMethod(getMethod);
// 请求成功状态-200
if (status == HttpStatus.OK.value()) {
try {
is = getMethod.getResponseBodyAsStream();
byte[] b = new byte[1024];
int length;
while ((length = is.read(b)) != -1) {
os.write(b, 0, length);
}
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println("请求返回状态:" + status);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}.start();
}
/**
* 步骤1:先进入default2.aspx页面
*
* @return
*/
public static String getdefault2Page() {
String url_default = "http://218.75.197.124:83/default2.aspx";
String result = httpUtils.getDataGet(client, url_default, null, false);
return result;
}
/**
* 将返回的html源码保存到本地电脑 格式为xxx.html
* @param result
*/
public static void saveAsHTML(String result,String fileName){
String path = "e:\\"+fileName+".html";
File htmlFile = new File(path);
try
{
if (!htmlFile.exists())
{
htmlFile.createNewFile();
}
OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(htmlFile),"gbk");
BufferedWriter writer=new BufferedWriter(write);
writer.write(result);
writer.close();
System.out.println(fileName+"保存成功,路径:"+path);
} catch (Exception e)
{
e.printStackTrace();
}
}
}
网络访问封装为一个工具包,源码如下:
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.springframework.http.HttpStatus;
import java.io.IOException;
/**
* Created by CHG on 2016-11-02 21:54.
*/
public class HttpUtils {
public String getDataPost(HttpClient httpClient,String url, NameValuePair[] nameValuePairs) {
// 响应内容
String result = "";
// 定义http客户端对象--httpClient
// HttpClient httpClient = new HttpClient();
// 定义并实例化客户端链接对象-postMethod
PostMethod postMethod = new PostMethod(url);
// postMethod.setRequestHeader("Referer", urlReferer);
try {
// 设置http的头
postMethod.setRequestHeader("ContentType",
"application/x-www-form-urlencoded;charset=UTF-8");
// 将表单的值放入postMethod中
postMethod.setRequestBody(nameValuePairs);
// 定义访问地址的链接状态
int statusCode = 0;
try {
// 客户端请求url数据
statusCode = httpClient.executeMethod(postMethod);
} catch (Exception e) {
e.printStackTrace();
}
// 请求成功状态-200
if (statusCode == HttpStatus.OK.value()) {
try {
result = postMethod.getResponseBodyAsString();
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println("请求返回状态:" + statusCode);
result = postMethod.getResponseBodyAsString();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 释放链接
postMethod.releaseConnection();
httpClient.getHttpConnectionManager().closeIdleConnections(0);
}
System.out.println(result);
return result;
}
/**
* get请求获取数据
* @param httpClient client
* @param url 访问的连接地址
* @param urlReferer 防止访问后服务器返回302
* @param isPrint 是否将得到的html打印
* @return
*/
public String getDataGet(HttpClient httpClient,String url, String urlReferer ,boolean isPrint) {
// 响应内容
String result = "";
// 定义并实例化客户端链接对象-getMethod
GetMethod getMethod = new GetMethod(url);
getMethod.setRequestHeader("Referer", urlReferer);//302跳转
try {
// 设置http的头
getMethod.setRequestHeader("ContentType",
"application/x-www-form-urlencoded;charset=UTF-8");
// 定义访问地址的链接状态
int statusCode = 0;
try {
// 客户端请求url数据
statusCode = httpClient.executeMethod(getMethod);
} catch (Exception e) {
e.printStackTrace();
}
// 请求成功状态-200
if (statusCode == HttpStatus.OK.value()) {
try {
result = getMethod.getResponseBodyAsString();
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println("请求返回状态:" + statusCode);
result = getMethod.getResponseBodyAsString();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 释放链接
getMethod.releaseConnection();
httpClient.getHttpConnectionManager().closeIdleConnections(0);
}
if (isPrint)
System.out.println(result);
return result;
}
}
demo中用到的jar包如下:
commons-codec-1.9.jar
commons-httpclient-3.1.jar
要实现同时发送用户名,密码,验证码发送到服务器,不可能一上来就发送,因为验证码一开始要从服务器获取,所以有以下思路:
- 模拟打开主页
- 模拟验证码刷新
- 登陆
模拟打开主页后,验证码是在服务器端生成图片再返回给客户端,so,怎么才能得到它呢,将服务器返回的数据写到本地文件中,再手动打开该图片文件即可看到验证码
__VIEWSTATE:是.net语言生成的,听说每次登陆都不一样,那要怎么解决?一开始就进入了教务系统的主页,就可以从返回的html源码中通过正则表达式截取
/** * 通过正则得到viewstate * * @param result html源码 */ private static String getViewState(String result) { Pattern p = Pattern.compile("name=\"__VIEWSTATE\" value=\"(.*?)\""); Matcher m = p.matcher(result); while (m.find()) { result = m.group(); } String viewState = result.substring(26, result.length() - 1); System.out.println(viewState); return viewState; }
commons-codec-1.9.jartxtUserName 是用户名
TextBox2 是密码
txtSecretCode 是验证码
其他字段我也不知道有什么用了,反正它们每次都不会变通过上面的步骤已经把需要的数据都得到了,学号跟密码就不用再描述了下面直接通过post方式登陆,将以上的数据存到一个NameValuePair[]中.
再用httpClient进行Post方式网络访问即可.
登陆成功后,判断返回的html源码,如果有欢迎两个字,就是登陆成功了,再选择相应功能,继续Get方式网络访问
本demo中实现的有查看成绩,查看课表,
查看等级考试成绩
其他功能均可以通过抓包查看请求的url实现.源码如下:
网络访问封装为一个工具包,源码如下:import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.methods.GetMethod; import org.springframework.http.HttpStatus; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * 步骤1:进入default2.aspx(Get) * 步骤2:得到__VIEWSTATE,模拟刷新验证码,将验证码保存到电脑 * 步骤3:登陆(Post) * Created by CHG on 2016-11-02 14:05. */ public class JavaPost { private static Scanner sc = new Scanner(System.in); private static String url_checkCode = "http://218.75.197.124:83/CheckCode.aspx?"; private static String userName = "13408200240"; private static String password = "this is a secret"; private static String checkCode = ""; private static String viewState = ""; private static HttpClient client = new HttpClient(); private static HttpUtils httpUtils = new HttpUtils(); private static String urlReferer = "http://218.75.197.124:83/xs_main.aspx?xh=13408200240"; public static void main(String[] args) { String result = getdefault2Page(); viewState = getViewState(result); downloadCheckCode(); System.out.print("e盘根目录查看验证码并输入控制台:"); checkCode = sc.next(); System.out.println("您输入的验证码为:" + checkCode); getMainPage(); getxs_main_get(); mainMenu(); } /** * 功能主菜单 */ private static void mainMenu() { System.out.println("---------------------------"); System.out.print("查看成绩请输入1"); System.out.println(); System.out.print("查看课表请输入2"); System.out.println(); System.out.print("查看等级考试成绩请输入3"); int i = sc.nextInt(); switch (i) { case 1: //查看课表 getStudentCJ(); mainMenu(); break; case 2: //查看课表 getStudentKB(); mainMenu(); break; case 3: //等级考试成绩查询 getMainTestCJ(); mainMenu(); break; default: break; } } /** * 等级考试成绩查询 */ private static void getMainTestCJ() { String url ="http://218.75.197.124:83/xsdjkscx.aspx?xh=13408200240"; String result = httpUtils.getDataGet(client, url, urlReferer, false); saveAsHTML(result,"等级考试成绩"); } /** * 查看课表 */ private static void getStudentKB() { //http://218.75.197.124:83/xskbcx.aspx?xh=13408200240 String url = "http://218.75.197.124:83/xskbcx.aspx?xh=13408200240"; String result = httpUtils.getDataGet(client, url, urlReferer, true); saveAsHTML(result,"课表"); } /** * 查看成绩 */ private static void getStudentCJ() { String url = "http://218.75.197.124:83/xscjcx.aspx?xh=13408200240"; String result = httpUtils.getDataGet(client, url, urlReferer, true); saveAsHTML(result,"成绩"); } /** * 再次302跳转到主页 */ private static void getxs_main_get() { String url = "http://218.75.197.124:83/xs_main.aspx?xh=13408200240"; String result = httpUtils.getDataGet(client, url, urlReferer, false); if (result.contains("欢迎")) System.out.println("登陆成功"); } /** * 封装好所有参数,post * 进入主页 */ private static void getMainPage() { String url = "http://218.75.197.124:83/default2.aspx"; NameValuePair[] nameValues = new NameValuePair[]{ new NameValuePair("__VIEWSTATE", viewState), new NameValuePair("txtUserName", userName), new NameValuePair("TextBox2", password), new NameValuePair("txtSecretCode", checkCode), new NameValuePair("RadioButtonList1", "%D1%A7%C9%FA"), new NameValuePair("Button1", ""), new NameValuePair("lbLanguage", ""), new NameValuePair("hidPdrs", ""), new NameValuePair("hidsc", ""), }; httpUtils.getDataPost(client, url, nameValues); } /** * 通过正则得到viewstate * * @param result */ private static String getViewState(String result) { Pattern p = Pattern.compile("name=\"__VIEWSTATE\" value=\"(.*?)\""); Matcher m = p.matcher(result); while (m.find()) { result = m.group(); } String viewState = result.substring(26, result.length() - 1); System.out.println(viewState); return viewState; } /** * 下载验证码and保存 */ private static void downloadCheckCode() { new Thread() { @Override public void run() { super.run(); String url = "http://218.75.197.124:83/CheckCode.aspx?"; InputStream is = null; OutputStream os = null; File file = new File("e:\\checkcode.png"); try { os = new FileOutputStream(file); } catch (FileNotFoundException e) { e.printStackTrace(); } GetMethod getMethod = new GetMethod(url); try { int status = client.executeMethod(getMethod); // 请求成功状态-200 if (status == HttpStatus.OK.value()) { try { is = getMethod.getResponseBodyAsStream(); byte[] b = new byte[1024]; int length; while ((length = is.read(b)) != -1) { os.write(b, 0, length); } } catch (IOException e) { e.printStackTrace(); } } else { System.out.println("请求返回状态:" + status); } } catch (IOException e) { e.printStackTrace(); } finally { try { is.close(); os.close(); } catch (IOException e) { e.printStackTrace(); } } } }.start(); } /** * 步骤1:先进入default2.aspx页面 * * @return */ public static String getdefault2Page() { String url_default = "http://218.75.197.124:83/default2.aspx"; String result = httpUtils.getDataGet(client, url_default, null, false); return result; } /** * 将返回的html源码保存到本地电脑 格式为xxx.html * @param result */ public static void saveAsHTML(String result,String fileName){ String path = "e:\\"+fileName+".html"; File htmlFile = new File(path); try { if (!htmlFile.exists()) { htmlFile.createNewFile(); } OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(htmlFile),"gbk"); BufferedWriter writer=new BufferedWriter(write); writer.write(result); writer.close(); System.out.println(fileName+"保存成功,路径:"+path); } catch (Exception e) { e.printStackTrace(); } } }
demo中用到的jar包如下:import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.PostMethod; import org.springframework.http.HttpStatus; import java.io.IOException; /** * Created by CHG on 2016-11-02 21:54. */ public class HttpUtils { public String getDataPost(HttpClient httpClient,String url, NameValuePair[] nameValuePairs) { // 响应内容 String result = ""; // 定义http客户端对象--httpClient // HttpClient httpClient = new HttpClient(); // 定义并实例化客户端链接对象-postMethod PostMethod postMethod = new PostMethod(url); // postMethod.setRequestHeader("Referer", urlReferer); try { // 设置http的头 postMethod.setRequestHeader("ContentType", "application/x-www-form-urlencoded;charset=UTF-8"); // 将表单的值放入postMethod中 postMethod.setRequestBody(nameValuePairs); // 定义访问地址的链接状态 int statusCode = 0; try { // 客户端请求url数据 statusCode = httpClient.executeMethod(postMethod); } catch (Exception e) { e.printStackTrace(); } // 请求成功状态-200 if (statusCode == HttpStatus.OK.value()) { try { result = postMethod.getResponseBodyAsString(); } catch (IOException e) { e.printStackTrace(); } } else { System.out.println("请求返回状态:" + statusCode); result = postMethod.getResponseBodyAsString(); } } catch (Exception e) { e.printStackTrace(); } finally { // 释放链接 postMethod.releaseConnection(); httpClient.getHttpConnectionManager().closeIdleConnections(0); } System.out.println(result); return result; } /** * get请求获取数据 * @param httpClient client * @param url 访问的连接地址 * @param urlReferer 防止访问后服务器返回302 * @param isPrint 是否将得到的html打印 * @return */ public String getDataGet(HttpClient httpClient,String url, String urlReferer ,boolean isPrint) { // 响应内容 String result = ""; // 定义并实例化客户端链接对象-getMethod GetMethod getMethod = new GetMethod(url); getMethod.setRequestHeader("Referer", urlReferer);//302跳转 try { // 设置http的头 getMethod.setRequestHeader("ContentType", "application/x-www-form-urlencoded;charset=UTF-8"); // 定义访问地址的链接状态 int statusCode = 0; try { // 客户端请求url数据 statusCode = httpClient.executeMethod(getMethod); } catch (Exception e) { e.printStackTrace(); } // 请求成功状态-200 if (statusCode == HttpStatus.OK.value()) { try { result = getMethod.getResponseBodyAsString(); } catch (IOException e) { e.printStackTrace(); } } else { System.out.println("请求返回状态:" + statusCode); result = getMethod.getResponseBodyAsString(); } } catch (Exception e) { e.printStackTrace(); } finally { // 释放链接 getMethod.releaseConnection(); httpClient.getHttpConnectionManager().closeIdleConnections(0); } if (isPrint) System.out.println(result); return result; } }