HttpURLConnection模拟登录学校的正方教务系统

  教务系统登录界面

  如图1-1

  

                1-1

  

  F12--》network查看登录教务系统需要参数:

  __VIEWSTAT

  txtUserName

  TextBox2

  txtSecretCode

  RadioButtonList1

  Button1

  lbLanguage

  hidPdrs

  hidsc

  如图1-2

  

                       1-2

  或者登录之前开启wireshark也可捕获到登录系统时post的数据,使用"http.request.method==POST"过滤条件进行筛选

  如图1-3

                                  1-3

  

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/*
 * Copyright (c) 2015 
 * technology co., LTD) 
 * All rights reserved.
 */
/**
 * Description        : 
 * <p/>
 * <br><br>Time        : 2015-11-10 上午11:13:13
 *
 * @author ZXL
 * @version 1.0
 * @since 1.0
 */
public class TeachingAffaiSystemLogin {

    //验证码url
    //static String surl = "http://jwc.gduf.edu.cn/CheckCode.aspx";
    private static String main_url = "http://jwc.gduf.edu.cn/default2.aspx";
    private static String forword_url = "http://jwc.gduf.edu.cn/xs_main.aspx?xh=";
    private static String responseCookie = "";
    private static String __VIEWSTATE = "";
    private static String txtUserName = "教务系统账号";
    private static String TextBox2 = "教务系统密码";
    private static String txtSecretCode = "";
    private static String RadioButtonList1 = "学生";
    private static String Button1 = "";
    private static String lbLanguage = "";
    private static String hidPdrs = "";
    private static String hidsc = "";
    private static URL url;
    private static HttpURLConnection httpURLConn;
    private static String imageName = "code";
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        doGet();
        doPost();
    }
    
    /**
     * GET参数 __VIEWSTATE
     * @param args
     */
    private static void doGet(){
        try {
            //保存验证码图片
            url = new URL(main_url);
            httpURLConn = (HttpURLConnection)url.openConnection();
            httpURLConn.setDoInput(true);
            httpURLConn.setDoOutput(true);
        
            
            //拿到服务器随机生成的__VIEWSTATE
            String line = null;
            BufferedReader br = new BufferedReader(new InputStreamReader(httpURLConn.getInputStream(),"GB2312"));
            
            String regEx = "<input.*type=\"hidden\".*name=\"__VIEWSTATE\".*value=\"(.*)\".*/>";
            while ((line = br.readLine())!=null){
                Pattern pat = Pattern.compile(regEx);
                Matcher mat = pat.matcher(line);
                boolean rs = mat.find();
                if (rs==true){
                    System.out.println(mat.groupCount());
                    for(int i=1;i<=mat.groupCount();i++){
                        System.out.println(mat.group(i));
                    }
                    __VIEWSTATE = mat.group(1);
                }
            }
            
            
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    /**
     * post参数登录到教务系统
     * @param args
     */
    private static void doPost(){
        //post数据登录
        try {
            url = new URL(main_url);
            
            httpURLConn = (HttpURLConnection)url.openConnection();
            httpURLConn.setDoInput(true);
            httpURLConn.setDoOutput(true);
            httpURLConn.setRequestProperty("contentType", "GB2312");  
            
            
            StringBuilder sb = new StringBuilder();
            sb.append("__VIEWSTATE=" +URLEncoder.encode(__VIEWSTATE, "GB2312"));
            sb.append("&txtUserName="+URLEncoder.encode(txtUserName, "GB2312"));
            sb.append("&TextBox2="+URLEncoder.encode(TextBox2, "GB2312"));
            sb.append("&txtSecretCode="+URLEncoder.encode(txtSecretCode, "GB2312"));
            sb.append("&RadioButtonList1="+URLEncoder.encode(RadioButtonList1, "GB2312"));
            sb.append("&Button1="+Button1);
            sb.append("&lbLanguage="+lbLanguage);
            sb.append("&hidPdrs="+hidPdrs);
            sb.append("&hidsc="+hidsc);
            httpURLConn.setRequestProperty("Content-Length",   
                        String.valueOf(sb.toString().length()));  
            httpURLConn.setRequestProperty("Referer", main_url);
            httpURLConn.setRequestMethod("POST");
            
            httpURLConn.setRequestProperty("Cookie", responseCookie);
            //不重定向
            httpURLConn.setInstanceFollowRedirects(false);
            
            OutputStream os = httpURLConn.getOutputStream();
            os.write(sb.toString().getBytes("GB2312"));
            os.close();
            
            //post之后返回的页面
            BufferedReader bf = new BufferedReader(new InputStreamReader(httpURLConn.getInputStream(),"GB2312"));
            String line1 = null;
            while ((line1=bf.readLine())!=null){
                System.out.println(line1);
            }
            
            System.out.println("responseCode="+httpURLConn.getResponseCode());
            System.out.println("重定向地址"+httpURLConn.getHeaderField("Location"));
            
            responseCookie = httpURLConn.getHeaderField("Set-Cookie");
            System.out.println(responseCookie);
            
            
            //登录主界面
            String line = null;
            url = new URL(forword_url+URLEncoder.encode(txtUserName,"GB2312"));
            httpURLConn = (HttpURLConnection)url.openConnection();
            httpURLConn.setRequestProperty("Referer", forword_url+URLEncoder.encode(txtUserName,"GB2312"));
            httpURLConn.setRequestProperty("Content-Type", "text/xml; charset=GB2312");
            httpURLConn.setRequestProperty("Cookie", responseCookie);
            httpURLConn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36");
            httpURLConn.setRequestMethod("GET");
            httpURLConn.setDoInput(true);
            httpURLConn.setDoOutput(true);
            
            BufferedReader br = new BufferedReader(new InputStreamReader(httpURLConn.getInputStream(),"GB2312"));
            while ((line = br.readLine())!=null){
                System.out.println(line);
            }
        } 
        catch (ProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (MalformedURLException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

  先get页面main_url,从返回页面获取到hidden的viewstate,连同其它参数一起post到main_url。

  

  因为post到main_url之后,服务器端做了一个重定向处理,返回码为302,重定向页面为forword_url+“学号”(也可以在HttpURLConnection的getHeaderField("Location")获得)。

  参考资料:http://www.blogfshare.com/php-curl-zhengfang.html

posted @ 2015-11-11 22:52  lindaZ  阅读(1976)  评论(0编辑  收藏  举报