zoukankan      html  css  js  c++  java
  • (HTTP)POST and GET

    最近,像超级课程表一类的课程表“一件载入”功能很受欢迎,介绍如果通过模拟表单提交形式进入正方教务系统,然后通过HTTP的Post和Get方法去获取数据并解析数据(请注意,本文代码以JAVA APPLICATION代码为例):

    1、先定义所有需要进行POST的表单参数

     1 //一般为学号
     2 
     3 String _xh = "********";
     4 
     5 //登录密码
     6 
     7 String _pw = "********";
     8 
     9 //网页验证码
    10 
    11 String VIEWSTATE = "dDwtMTIwMTU3OTE3Nzs7PiY97LYM9CAjZFyA2K9ev6jmM0CY";
    12 
    13 //POST目的地,即服务器IP
    14 
    15 String host = "218.192.12.87";
    16 
    17 //POST的URL,作为网页redirect时使用
    18 
    19 String main_url = "http://" + host;
    20 
    21 String login_url = main_url + "/";

    2、登录前参数的设定,为获取重定向地址(PostMethod)

     1 post=new PostMethod(url);
     2 
     3 //设置发送的报文的Header(头文件)
     4 
     5 post.setRequestHeader("Accept","text/html,application/xhtml+xml,**");
     6 
     7 get.setRequestHeader("Referer",redirt2);
     8 
     9 get.setRequestHeader("Accept-Language","zh-CN");
    10 
    11 get.setRequestHeader("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0");
    12 
    13 get.setRequestHeader("Accept-Encoding","gzip,deflate");
    14 
    15 get.setRequestHeader("ContentType","application/x-www-form-urlencoded;charset=UTF-8"); 
    16 
    17 get.setRequestHeader("Host", "jwc.gduf.edu.cn");
    18 
    19 get.setRequestHeader("Connection", "Keep-Alive");
    20 
    21 get.setRequestHeader("Cookie", "Hm_lvt_8e7618a5b224acaa4f4f5c648598f4bd=1357386046,1357450972,1357574780,1357649227");
    22 
    23 //设置要进行查询提交的参数
    24 
    25 NameValuePair[] param2 = { new NameValuePair("xh",_xh),
    26 
    27 new NameValuePair("xm",+*******"),
    28 
    29 new NameValuePair("gnmkdm","N121601"),
    30 
    31 } ;
    32 
    33 // 将参数放入postMethod中
    34 
    35 get.setQueryString(param2);
    36 
    37 get.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"gb2312");

    3、设置重定向地址方法

     1 String locationHeader = null;
     2 
     3 //遇到报错302时
     4 
     5 if(statusCode == 302){
     6 
     7 locationHeader = post.getResponseHeader("location").getValue();
     8 
     9 }else if (locationHeader == null) {
    10 
    11 return main_url+"/";
    12 
    13 }
    14 
    15 return main_url+locationHeader;

    4、进行POST提交表单

     1 //进行第一次登陆参数配置
     2 
     3 firstPost(login_url);
     4 
     5 // 执行postMethod
     6 
     7 statusCode = httpclient.executeMethod(post);
     8 
     9 // HttpClient对于要求接受后继服务的请求,像POST和PUT等不能自动处理转发(错误代号为301或者302)
    10 
    11 if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode ==HttpStatus.SC_MOVED_TEMPORARILY) {
    12 
    13 // 从头中取出转向的地址
    14 
    15 locationHeader = post.getResponseHeader("location");
    16 
    17 if (locationHeader != null) {
    18 
    19 //正式从重定位地址进行登陆
    20 
    21 redirt1 = getRedirectLocation();
    22 
    23 firstPost(redirt1);
    24 
    25 statusCode = httpclient.executeMethod(post);
    26 
    27 if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode ==HttpStatus.SC_MOVED_TEMPORARILY) {
    28 
    29 locationHeader = post.getResponseHeader("location");
    30 
    31 if (locationHeader != null) {
    32 
    33 redirt2 = getRedirectLocation();
    34 
    35 }
    36 
    37 } else {
    38 
    39 System.err.println("Url指定文件地址错误!");
    40 
    41 }
    42 
    43 }

    5、登录后参数的设定,为获取数据(GetMethod)

     1 get=new GetMethod(url);
     2 
     3 get.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
     4 
     5 get.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
     6 
     7 //设置发送的报文的Header
     8 
     9 get.setRequestHeader("Accept","text/html,application/xhtml+xml,*/*");
    10 
    11 get.setRequestHeader("Referer",redirt2);
    12 
    13 get.setRequestHeader("Accept-Language","zh-CN");
    14 
    15 get.setRequestHeader("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0");
    16 
    17 get.setRequestHeader("Accept-Encoding","gzip,deflate");
    18 
    19 get.setRequestHeader("ContentType","application/x-www-form-urlencoded;charset=UTF-8"); 
    20 
    21 get.setRequestHeader("Host", "jwc.gduf.edu.cn");
    22 
    23 get.setRequestHeader("Connection", "Keep-Alive");
    24 
    25 get.setRequestHeader("Cookie", "Hm_lvt_8e7618a5b224acaa4f4f5c648598f4bd=1357386046,1357450972,1357574780,1357649227");
    26 
    27 //设置要进行查询提交的参数
    28 
    29 NameValuePair[] param2 = {
    30 
    31 new NameValuePair("xh",_xh),
    32 
    33 new NameValuePair("xm","********"),
    34 
    35 new NameValuePair("gnmkdm","N121601"),
    36 
    37 } ;
    38 
    39 // 将参数放入postMethod中
    40 
    41 get.setQueryString(param2);
    42 
    43 get.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"gb2312");

    6、嗅探指定页面的代码(GetMethod)

     1 firstget(redirt2.subSequence(0,48)+"tjkbcx.aspx");
     2 
     3 byte[] responseBody = null;
     4 
     5 int statusCode = httpclient.executeMethod(get);
     6 
     7 if (statusCode != HttpStatus.SC_OK) {
     8 
     9 System.err.println("Method failed: " + get.getStatusLine());
    10 
    11 }
    12 
    13 responseBody = get.getResponseBody();

    注:POST过程中所要提交表单的数据都可以通过HTTPWATCH等软件监控得到,网上都有这些教程。本人为菜鸟,如有错误请指出,如愿获取全部代码,可关注本人并评论。

  • 相关阅读:
    制作文件的备份
    文件的读写
    文件的打开与关闭
    文件操作介绍
    数据类型转换
    位运算
    进制
    函数使用注意事项
    匿名函数
     递归函数
  • 原文地址:https://www.cnblogs.com/vijay/p/3496316.html
Copyright © 2011-2022 走看看