标签:
运用httpclient3+jsoup获取正方教务系统课表
import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.Scanner; import org.apache.commons.httpclient.Cookie; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.PostMethod; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; public class Login { private static String host_url = "jwc.sju.js.cn"; private static String login_url = "/default2.aspx"; private static String code_url = "/CheckCode.aspx"; private static String kb_url = "/xskb.aspx"; private HttpClient client; private String viewstate = ""; private String cookie = ""; public Login() { init(); } private void init() { client = new HttpClient(); client.getParams().setHttpElementCharset("GBK"); client.getHostConfiguration().setHost(host_url, 80, "http"); GetMethod get = new GetMethod(login_url); get.addRequestHeader("Accept", "*/*"); get.addRequestHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Shuame)"); get.addRequestHeader("Accept-Encoding", "gzip, deflate"); get.addRequestHeader("Host", "jwc.sju.js.cn"); get.addRequestHeader("Connection", "Keep-Alive"); int status = 0; try { status = client.executeMethod(get); viewstate = getViewState(get.getResponseBodyAsString()); } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } Cookie cookies[] =client.getState().getCookies(); //得到cookies for(int i = 0; i < cookies.length; i++) { if(i == 0) { cookie = cookies[i].toString(); } else { cookie += cookies[i] + ";"; } } System.out.println(cookie); get.releaseConnection(); get = new GetMethod(code_url); try { status = client.executeMethod(get); loadPic(get.getResponseBody()); } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } get.releaseConnection(); } public void autoLogin(String xh,String pwd) { //xh:学号 pwd:密码 PostMethod post = new PostMethod(login_url); post.setRequestHeader("Accept","application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*"); post.setRequestHeader("Referer", "http://jwc.sju.js.cn/"); post.setRequestHeader("Accept-Language","zh-CN"); post.setRequestHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Shuame)"); post.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); post.setRequestHeader("Accept-Encoding", "gzip, deflate"); post.setRequestHeader("Connection", "Keep-Alive"); post.setRequestHeader("Cache-Control", "no-cache"); String checkcode = ""; Scanner sc = new Scanner(System.in); checkcode = sc.nextLine().trim(); System.out.println(checkcode); String type = "学生"; try { post.setRequestBody(new NameValuePair[]{ new NameValuePair("__VIEWSTATE",viewstate), new NameValuePair("txtUserName",xh), new NameValuePair("textBox2",pwd), new NameValuePair("txtSecretCode",checkcode), new NameValuePair("RadioButtonList1",URLEncoder.encode(type, "GBK")), new NameValuePair("Button1",""), new NameValuePair("lbLanguage",""), new NameValuePair("hidPdrs",""), new NameValuePair("hidsc","") }); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } int status = 0; String content = ""; try { status = client.executeMethod(post); content = post.getResponseBodyAsString(); post.releaseConnection(); } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } System.out.println(post); } public void catchKbData(String xh,String xq) { client = new HttpClient(); client.getParams().setHttpElementCharset("GBK"); client.getHostConfiguration().setHost(host_url, 80, "http"); int status = 0; String content = ""; //GET xs_main页面 String xs_mainUrl = "/xs_main.aspx?xh=" + xh; GetMethod get = new GetMethod(xs_mainUrl); get.setRequestHeader("Accept", "application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*"); get.setRequestHeader("Referer", "http://jwc.scnu.edu.cn/"); get.setRequestHeader("Accept-Language", "zh-CN"); get.setRequestHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Shuame)"); get.setRequestHeader("Accept-Encoding", "gzip, deflate"); get.setRequestHeader("Connection", "Keep-Alive"); get.setRequestHeader("Cache-Control", "no-cache"); get.setRequestHeader("Cookie", cookie); try { status = client.executeMethod(get); content = get.getResponseBodyAsString(); get.releaseConnection(); } catch (HttpException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } System.out.println(content); //GET 查询页面 get = new GetMethod(kb_url); get.setRequestHeader("Accept", "application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*"); get.setRequestHeader("Connection", "Keep-Alive"); get.setRequestHeader("Accept-Language","zh-CN"); get.setRequestHeader("Host","jwc.sju.js.cn"); get.setRequestHeader("Accept-Encoding","gzip, deflate"); get.setRequestHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Shuame)"); get.setRequestHeader("Cookie", cookie); get.setQueryString(new NameValuePair[] { new NameValuePair("xh",xh), new NameValuePair("xhxx",xh+xq) }); try { client.executeMethod(get); content = get.getResponseBodyAsString(); get.releaseConnection(); } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } System.out.println(content); } private void loadPic(byte[] data) { //get.getResponseBody()获取验证码图片的字节信息,并写入output输出流中 File file = new File("D:/code.png");//验证码图片存放位置 FileOutputStream output; try { output = new FileOutputStream(file); output.write(data); output.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } private String getViewState(String html) { //获取VIEWSTATE字段 Document doc = Jsoup.parse(html); String viewstate = ""; viewstate = doc.select("input[name=__VIEWSTATE]").val(); System.out.println("_VIEWSTATE= " + viewstate); return viewstate; } public static void main(String[] args) { Login login = new Login(); //login.autoLogin("xxxxxxxx", "xxxxxx"); //login.catchKbData("xxxxxxxx", "2014-20151"); } }
poxm.xml
<dependency> <groupId>commons-httpclient</groupId> <artifactId>commons-httpclient</artifactId> <version>3.1</version> </dependency> <dependency> <groupId>org.jsoup</groupId> <artifactId>jsoup</artifactId> <version>1.8.1</version> </dependency>
标签:
原文地址:http://www.cnblogs.com/huojia213/p/4194790.html