噜噜噜,附上代码~~~
不过说句老实话,ACM是真的比写这个有意思多了,吸吸吸~~
package jwgl;
import java.io.IOException;
import website.login;
public class Main {
public static void main(String[] args){
try {
login.login_jwgl();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package website;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
//import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.client.LaxRedirectStrategy;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import constval.pageval;
public class login {
// static HttpHost proxy = new HttpHost("127.0.0.1",8080);
static RequestConfig requestConfig = RequestConfig.custom()
// .setProxy(proxy)
.setConnectTimeout(10000) // 设置连接超时时间
.setConnectionRequestTimeout(100000) // 设置请求超时时间
.setSocketTimeout(100000).setRedirectsEnabled(true)// 默认允许自动重定向
.build();
public static void login_jwgl() throws ClientProtocolException, IOException {
HttpClientBuilder builder = HttpClients.custom().disableAutomaticRetries() // 关闭自动处理重定向
.setRedirectStrategy(new LaxRedirectStrategy());// 利用LaxRedirectStrategy处理POST重定向问题
CloseableHttpClient httpClient = builder.setDefaultRequestConfig(requestConfig).build();
String state=pageval.get_page();
HttpPost httpPost = new HttpPost(pageval.login_page+state+"/default2.aspx");
System.err.println("RemoteHost :"+pageval.login_page+state+"/default2.aspx");
Scanner scanner=new Scanner(System.in);
System.out.println("请输入学号 :");
String Sno=scanner.nextLine();
System.out.println("请输入密码 :");
String password=scanner.nextLine();
List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
list.add(new BasicNameValuePair("__VIEWSTATE", "dDwtMTg3MTM5OTI5MTs7PqzAMxEcbTO1Y3iF3fZH/MWfvpUe"));
list.add(new BasicNameValuePair("TextBox1", Sno));
list.add(new BasicNameValuePair("TextBox2", password));
list.add(new BasicNameValuePair("RadioButtonList1", "%D1%A7%C9%FA"));
list.add(new BasicNameValuePair("Button1", ""));
list.add(new BasicNameValuePair("lbLanguage", ""));
try{
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,"gb2312");
httpPost.setEntity(entity);
HttpResponse httpResponse=httpClient.execute(httpPost);
if(httpResponse.getStatusLine().getStatusCode()==200){
String webCode= EntityUtils.toString(httpResponse.getEntity());
String Sname=pageval.get_Sname(webCode, Sno);
String stoken=pageval.get_stoken(webCode);
System.out.println("Welcome "+Sname+" ~~~");
pageval.printClasses(httpClient,state,Sno,Sname,stoken);
System.err.println("----------------------------------------------------->");
pageval.printStudySchedule(httpClient, state, Sno, Sname, stoken);
httpPost.abort();
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(httpClient!=null){
try {
httpClient.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
scanner.close();
}
}
}
package constval;
import java.util.List;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import util.DP;
public class pageval {
private static RequestConfig requestConfig=RequestConfig.custom()
.setRedirectsEnabled(false)
.build();
public static String login_page="http://202.197.120.57";
public static String get_page() throws ClientProtocolException, IOException {
HttpClient httpClient=HttpClients.createDefault();
HttpGet httpGet=new HttpGet(login_page);
httpGet.setConfig(requestConfig);
HttpResponse httpResponse=httpClient.execute(httpGet);
String host="";
if(httpResponse.getStatusLine().getStatusCode()==302){
Header[] header=httpResponse.getAllHeaders();
for(int i=0;i<header.length;i++){
String hString=header[i].toString();
int idx=hString.indexOf("Location");
if(idx!=-1){
host=header[i].toString().substring(hString.indexOf("(")-1,hString.indexOf(")")+1);
break;
}
}
}
httpGet.abort();
return host;
}
public static String get_Sname(String webCode,String Sno){
String from="<span id=\"xhxm\">"+Sno;
String to="同学</span></em>";
int idx1=webCode.indexOf(from)+30;
int idx2=webCode.indexOf(to);
return webCode.substring(idx1,idx2);
}
public static String get_stoken(String webCode){
String from="&gnmkdm=";
int idx=webCode.indexOf(from);
return webCode.substring(idx+8,idx+15);
}
public static String get_viewstate1(String webCode){
int idx1=webCode.indexOf("value=\"")+7;
int idx2=webCode.indexOf("<div class=\"toolbox\" id=\"divCxtj\">")-11;
return webCode.substring(idx1, idx2);
}
public static String get_viewstate2(String webCode){
int idx1=webCode.indexOf("__VIEWSTATE\" value=\"")+20;
int idx2=webCode.indexOf("<script language=\"javascript\" type=\"text/javascript\">")-8;
return webCode.substring(idx1, idx2);
}
private static HttpGet Set_httpGetOfclass(String state,String Sno,String Sname,String stoken) throws UnsupportedEncodingException{
String page=login_page+state+"/xscj.aspx?xh="+Sno+"&xm="+Sname+"&gnmkdm="+stoken;
HttpGet httpGet=new HttpGet(page);
httpGet.setHeader("Accept", "text/html, application/xhtml+xml, image/jxr, */*");
httpGet.setHeader("Referer",login_page+state+"/xs_main.aspx?xh="+Sno);
httpGet.setHeader("Accept-Language","zh-Hans-CN,zh-Hans;q=0.5");
httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36 Edge/15.15063");
httpGet.setHeader("Host", "202.197.120.57");
httpGet.setHeader("Connection","close");
return httpGet;
}
private static HttpPost Set_httpPostOfclass(String state,String Sno,String Sname,String stoken,String viewstate) throws UnsupportedEncodingException{
String page=login_page+state+"/xscj.aspx?xh="+Sno+"&xm="+Sname+"&gnmkdm="+stoken;
HttpPost httpPost=new HttpPost(page);
httpPost.setHeader("Host", "202.197.120.57");
httpPost.setHeader("Cache-Control", "max-age=0");
httpPost.setHeader("Origin", "http://202.197.120.57");
httpPost.setHeader("Upgrade-Insecure-Requests", "1");
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36 Edge/15.15063");
httpPost.setHeader("Accept", "text/html, application/xhtml+xml, image/jxr, */*");
httpPost.setHeader("Referer",login_page+state+"/xscj.aspx?xh="+Sno+"&xm="+Sname+"&gnmkdm="+stoken);
httpPost.setHeader("Accept-Language","zh-Hans-CN,zh-Hans;q=0.5");
httpPost.setHeader("Connection","close");
List<BasicNameValuePair> list=new ArrayList<BasicNameValuePair>();
list.add(new BasicNameValuePair("__VIEWSTATE", viewstate));
list.add(new BasicNameValuePair("ddlXN",""));
list.add(new BasicNameValuePair("ddlXQ", ""));
list.add(new BasicNameValuePair("txtQSCJ", "0"));
list.add(new BasicNameValuePair("txtZZCJ", "100"));
list.add(new BasicNameValuePair("Button2", "%D4%DA%D0%A3%D1%A7%CF%B0%B3%C9%BC%A8%B2%E9%D1%AF"));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,"GBK");
httpPost.setEntity(entity);
return httpPost;
}
public static void printClasses(HttpClient httpClient,String state,String Sno,String Sname,String stoken) throws ClientProtocolException, IOException{
HttpGet httpGet=Set_httpGetOfclass(state, Sno, Sname, stoken);
HttpResponse httpResponse=httpClient.execute(httpGet);
if(httpResponse.getStatusLine().getStatusCode() == 200){
String srtResult = EntityUtils.toString(httpResponse.getEntity());//获得返回的结果
String viewstate=get_viewstate1(srtResult);
HttpPost httpPost=Set_httpPostOfclass(state, Sno, Sname, stoken, viewstate);
HttpResponse httpResponse2=httpClient.execute(httpPost);
if(httpResponse2.getStatusLine().getStatusCode()==200){
String rString=EntityUtils.toString(httpResponse2.getEntity());
String Grade=rString.substring(rString.indexOf("<tr class=\"datelisthead\">"),rString.indexOf("\r\n\t\t\t\t\t\t\t<br>"));
boolean isprint[]=new boolean[10];
isprint[0]=isprint[1]=isprint[2]=isprint[3]=isprint[4]=true;
DP.PrintResult(Grade.replaceAll(" ", "null"),10,isprint);
httpPost.abort();
}
httpGet.abort();
}
}
private static HttpGet Set_httpGetOfSchedule(String state,String Sno,String Sname,String stoken) throws UnsupportedEncodingException{
String page=login_page+state+"/pyjh.aspx?xh="+Sno+"&xm="+Sname+"&gnmkdm="+stoken;
HttpGet httpGet=new HttpGet(page);
httpGet.setHeader("Accept", "text/html, application/xhtml+xml, image/jxr, */*");
httpGet.setHeader("Referer",login_page+state+"/xs_main.aspx?xh="+Sno);
httpGet.setHeader("Accept-Language","zh-Hans-CN,zh-Hans;q=0.5");
httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36 Edge/15.15063");
httpGet.setHeader("Host", "202.197.120.57");
httpGet.setHeader("Connection","close");
return httpGet;
}
private static HttpPost Set_httpPostOfSchedule(String state,String Sno,String Sname,String stoken,String viewstate,String event) throws UnsupportedEncodingException{
String page=login_page+state+"/pyjh.aspx?xh="+Sno+"&xm="+Sname+"&gnmkdm="+stoken;
HttpPost httpPost=new HttpPost(page);
httpPost.setHeader("Host", "202.197.120.57");
httpPost.setHeader("Cache-Control", "max-age=0");
httpPost.setHeader("Origin", "http://202.197.120.57");
httpPost.setHeader("Upgrade-Insecure-Requests", "1");
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36 Edge/15.15063");
httpPost.setHeader("Accept", "text/html, application/xhtml+xml, image/jxr, */*");
httpPost.setHeader("Referer",login_page+state+"/pyjh.aspx?xh="+Sno+"&xm="+Sname+"&gnmkdm="+stoken);
httpPost.setHeader("Accept-Language","zh-Hans-CN,zh-Hans;q=0.5");
httpPost.setHeader("Connection","close");
List<BasicNameValuePair> list=new ArrayList<BasicNameValuePair>();
list.add(new BasicNameValuePair("__EVENTTARGET", event));
list.add(new BasicNameValuePair("__EVENTARGUMENT", ""));
list.add(new BasicNameValuePair("__VIEWSTATE", viewstate));
list.add(new BasicNameValuePair("xq", "È«²¿"));
list.add(new BasicNameValuePair("kcxz", "È«²¿"));
list.add(new BasicNameValuePair("Button1", "¿ÉÌæ»»¿Î³Ì"));
list.add(new BasicNameValuePair("dpDBGrid:txtChoosePage", "1"));
list.add(new BasicNameValuePair("dpDBGrid:txtPageSize", "111"));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,"gb2312");
httpPost.setEntity(entity);
return httpPost;
}
public static void printStudySchedule(HttpClient httpClient,String state,String Sno,String Sname,String stoken) throws ClientProtocolException, IOException{
HttpGet httpGet=Set_httpGetOfSchedule(state, Sno, Sname, stoken);
HttpResponse httpResponse=httpClient.execute(httpGet);
if(httpResponse.getStatusLine().getStatusCode() == 200){
String srtResult = EntityUtils.toString(httpResponse.getEntity());//获得返回的结果
String viewstate=get_viewstate2(srtResult);
HttpPost httpPost=Set_httpPostOfSchedule(state, Sno, Sname, stoken, viewstate,"xq");
HttpResponse httpResponse2=httpClient.execute(httpPost);
//模拟两次事件触发~~~~
if(httpResponse2.getStatusLine().getStatusCode()==200){
HttpPost httpPost2=Set_httpPostOfSchedule(state, Sno, Sname, stoken, viewstate, "dpDBGrid%3AtxtPageSize");
httpResponse2=httpClient.execute(httpPost2);
if(httpResponse2.getStatusLine().getStatusCode()==200){
String event=EntityUtils.toString(httpResponse2.getEntity());
String result=event.substring(event.indexOf("</tr><tr>"),event.lastIndexOf("\r\n\t\t\t\t\t\t\t\t\t\t<div id="));
boolean isprint[]=new boolean[16];
isprint[0]=isprint[1]=isprint[2]=isprint[5]=true;
System.err.println("所有课程信息:----->");
DP.PrintResult(result.replaceAll(" ", "null"),16,isprint);
}
httpPost2.abort();
}
httpPost.abort();
}
httpGet.abort();
}
}
package util;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class DP {
public static void PrintResult(String Code,int length,boolean isprint[]){
String regex="<td>(.*?)</td>";
Pattern pattern=Pattern.compile(regex);
Matcher matcher=pattern.matcher(Code);
while(matcher.find()){
System.err.print(String.format("%s", matcher.group(1))+" ");
for(int i=1;i<length;i++){
matcher.find();
if(isprint[i]){
System.err.print(String.format("%s", matcher.group(1))+" ");
}
}
System.err.println();
}
}
}