啦啦啦,废话不多说,要用的同学,自己去编译吧,2333333,上源码~
package Scrapy;
import Login.login;
public class Main{
public static void main(String[] args){
try {
login.login_website();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package Login;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import org.apache.http.HttpResponse;
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 Const.const_value;
import StudyTime.Study;
public class login {
// static HttpHost proxy = new HttpHost("127.0.0.1",8080);
static RequestConfig requestConfig = RequestConfig.custom()
// .setProxy(proxy)
.setConnectTimeout(50000000) // 设置连接超时时间
.setConnectionRequestTimeout(50000000) // 设置请求超时时间
.setSocketTimeout(5000000).setRedirectsEnabled(true)// 默认允许自动重定向
.build();
private static Scanner cin;
public static CloseableHttpClient login_website() throws Exception {
HttpClientBuilder builder = HttpClients.custom().disableAutomaticRetries() // 关闭自动处理重定向
.setRedirectStrategy(new LaxRedirectStrategy());// 利用LaxRedirectStrategy处理POST重定向问题
HttpPost httpPost = new HttpPost(const_value.RemoteHost);
CloseableHttpClient httpClient = builder.setDefaultRequestConfig(requestConfig).build();
httpPost.setConfig(requestConfig);
cin = new Scanner(System.in);
System.out.println("你他喵的快输入学号:!!!");
String studentnum=cin.nextLine();
List list = new ArrayList();
list.add(new BasicNameValuePair("__EVENTTARGET", ""));
list.add(new BasicNameValuePair("__EVENTARGUMENT", ""));
list.add(new BasicNameValuePair("__VIEWSTATE", "/wEPDwULLTE2NTQ5MDE2NTlkZK9ydOOMantPDBunWHmzPEKUS6l1"));
list.add(new BasicNameValuePair("tbName", studentnum));
list.add(new BasicNameValuePair("tbPwd", "111111"));
list.add(new BasicNameValuePair("btnLogin", "登 录"));
try{
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,"GBK");
httpPost.setEntity(entity);
HttpResponse httpResponse = httpClient.execute(httpPost);
if(httpResponse.getStatusLine().getStatusCode()==200){
String webCode= EntityUtils.toString(httpResponse.getEntity());
//System.err.println(webCode);
String classno=const_value.get_classno(webCode);
String stoken=const_value.get_stoken(webCode);
System.out.println("success get classno : "+classno);
System.out.println("success get stoken : "+stoken);
System.out.println("Material :");
String Material=const_value.get_Material(cin.nextInt());
System.out.println("Unit : ");
String Unit=const_value.get_Unit(cin.nextInt());
Study.study(httpClient, classno, stoken,Material, Unit);
}
}catch (Exception e) {
e.printStackTrace();
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return httpClient;
}
}
package Const;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class const_value {
public final static String RemoteHost="http://172.27.14.218/npels/login.aspx";
public final static String SaveTime="http://172.27.14.218/npels/Student/LogTime.aspx";
private final static String Material="College_English_NEW_Century_Video_ThirdEdition_";
private final static String Unit="Unit_0";
public static String get_Material(int semester){
return Material+semester;
}
public static String get_Unit(int _Unit){
return Unit+_Unit;
}
public static String get_classno(String webCode){
int idx=webCode.indexOf("var classno=\"")+13;
return webCode.substring(idx, idx+14);
}
public static String get_stoken(String webCode){
int idx=webCode.indexOf("var stoken=\"")+12;
return webCode.substring(idx, idx+38);
}
public static String get_date(){
Calendar calendar=Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(Calendar.HOUR, -2);
Date lastHour=calendar.getTime();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
String date=df.format(lastHour).toString();
date=date.replaceAll(" ", "%20");
return date;
}
public static String get_page(String classno,String Material,String Unit,String StudyTime){
String page="http://172.27.14.218/NPELS/Student/LogTime.aspx?logType=savestudytime&classno="+classno+"&material="+Material+"&unit="+Unit+"&studyType=1&starttime="+StudyTime+"&seconds=3&nocache=0.10499463896651684";
return page;
}
}
package StudyTime;
import java.util.List;
import java.util.ArrayList;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
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.message.BasicNameValuePair;
import Const.const_value;
public class Study {
static HttpHost proxy = new HttpHost("127.0.0.1",8080);
static RequestConfig requestConfig = RequestConfig.custom().setProxy(proxy).setConnectTimeout(5000) // 设置连接超时时间
.setConnectionRequestTimeout(5000) // 设置请求超时时间
.setSocketTimeout(5000).setRedirectsEnabled(true)// 默认允许自动重定向
.build();
public static void study(HttpClient httpClient,String classno,String stoken,String Material,String Unit) throws Exception, Throwable {
String StudyTime=const_value.get_date();
HttpPost httpPost=new HttpPost("http://172.27.14.218/npels/Student/LogTime.aspx");
//httpPost.setConfig(requestConfig);
List list = new ArrayList();
list.add(new BasicNameValuePair("logType", "savecomment"));
list.add(new BasicNameValuePair("unit",Unit));
list.add(new BasicNameValuePair("json", "undefined"));
HttpGet httpGet=new HttpGet(const_value.get_page(classno, Material, Unit, StudyTime));
httpGet.setHeader("Referer","http://172.27.14.218/NPELS/studentdefault.aspx");
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("Content-Type","application/x-www-form-urlencoded");
httpGet.setHeader("X-Requested-With","XMLHttpRequest");
httpGet.setHeader("Accept","*/*");
httpGet.setHeader("Host","172.27.14.218");
httpGet.setHeader("Connection","close");
httpGet.addHeader("Cookie", " StudyStart="+StudyTime.replaceAll(":", "%3A")+";Unit="+Unit+";st="+stoken);
httpGet.addHeader("\n\n\n\n\n\n", "\n\n\n\n\n");
//这个多个空行是为了抵消原有的cookies信息~
org.apache.http.Header[] header=httpGet.getAllHeaders();
for(int i=0;i\n\n");
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,"GBK");
httpPost.setEntity(entity);
System.err.println("HttpPost :"+httpPost+"\n\n");
HttpResponse httpResponse=httpClient.execute(httpPost);
if(httpResponse.getStatusLine().getStatusCode()==200){
System.err.println("Finish Post");
}
httpResponse=httpClient.execute(httpGet);
if(httpResponse.getStatusLine().getStatusCode()==200){
System.err.println("Successful post~");
}
}
}