您的位置:首页 > 编程语言 > Java开发

java无需获取cookie实现模拟登陆正方教务系统获取课表等数据

2018-01-13 21:44 1176 查看
之前获取正方教务系统课表,我们一般都是模拟登陆,然后拿到cookie,再进行访问。但是可能由于信息安全问题,一部分的正方系统已经没有生成cookie在本地,并且还会登陆网址之后,随机生成一些随机数在网址后面,并自己请求重定向网址,跳转到另一个网址。所以之前的获取cookie模拟登陆,已经没有无法满足需要了。

所以,我的思路就是,首先模拟登陆网址,然后在网址自己请求重定向的时候,拿到路径location里面的随机数,利用字符串截取出来,然后拼接到自己最后重定向的网站,使用账户密码模拟登陆进去,请求头不需要使用cookie,也不需要获取cookie。

下面的代码是截取随机数的代码:

//截取随机数代码
public static String  getNum(String reqUrl) {
      DefaultHttpClient httpclient = new DefaultHttpClient();
      String location = null;  //定义接收请求头的location,里面含有随机数
      String str = null;
      int responseCode = 0;
      try {
          final HttpGet request = new HttpGet(reqUrl);
          HttpParams params = new BasicHttpParams();
          params.setParameter("http.protocol.handle-redirects", false); // 默认不让重定向
          request.setParams(params);
          HttpResponse response = httpclient.execute(request);
          responseCode = response.getStatusLine().getStatusCode();
          Header[] headers = response.getAllHeaders();
         if(responseCode==302){
            Header locationHeader = response.getFirstHeader("Location");
           if (locationHeader != null) {
                  location = locationHeader.getValue();               
         
 str=location.substring(location.indexOf("/")+1, location.lastIndexOf("/"));              
            }
        }
      } catch (Exception e) {
          e.printStackTrace();
      }
      return str;
  }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息