HttpClient 3.1 模拟登陆正方教务系统 中文参数问题

crazypig 发布于 2013/11/01 17:41
阅读 4K+
收藏 2

httpclient模拟登陆学校的教务系统,当页面要请求中文参数时候,遇到这样的问题,页面请求不到,虽然看上去是返回了页面内容,但实际上没有得到想要的页面,用抓包工具看到URL中中文参数为xm=%25B3%25C2%25BD%25A8%25D0%25C0这种格式,返回的html中该字段也是这种格式,页面中其他中文没有出现这种情况,求解决办法,如何才能正确请求?

 

附代码如下:

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.scnu.edu.cn";
 private static String login_url = "/default2.aspx";
 private static String code_url = "/CheckCode.aspx";
 private static String query_url = "/kbcx_jxcd.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.scnu.edu.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.scnu.edu.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("textBox1",xh),
     new NameValuePair("textBox2",pwd),
     new NameValuePair("textBox3",checkcode),
     new NameValuePair("RadioButtonList1",URLEncoder.encode(type, "GBK")),
     new NameValuePair("Button1",""),
     new NameValuePair("lbLanguage","")
   });
  } 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 catchData(String xh,String xm) {
  
  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();
  }
  
  //GET 查询页面
  get = new GetMethod(query_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("Accept-Encoding","gzip, deflate");
         get.setRequestHeader("Referer","http://jwc.scnu.edu.cn/xs_main.aspx?xh="+xh);
         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);
        
         try {
   get.setQueryString(new NameValuePair[] {
     new NameValuePair("xh",xh),
     new NameValuePair("xm",URLEncoder.encode(xm, "GBK")),
     new NameValuePair("gnmkdm","N121611")
   });
  } catch (UnsupportedEncodingException e) {
   e.printStackTrace();
  }
        
         try {
   client.executeMethod(get);
   content = get.getResponseBodyAsString();
   get.releaseConnection();
  } catch (HttpException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
        
         //System.out.println(content);
         viewstate = this.getViewState(content); //作为下一次POST的参数之一
        
         PostMethod post = new PostMethod(query_url);
        
         String url = "http://jwc.scnu.edu.cn/kbcx_jxcd.aspx?xh=20112100053";
         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", url);
         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");
         post.setRequestHeader("Cookie", cookie);
        
         try {
   post.setRequestBody(new NameValuePair[] {
     new NameValuePair("__EVENTTARGET","ddlXq"),
     new NameValuePair("__EVENTARGUMENT",""),
     new NameValuePair("__VIEWSTATE",viewstate),
     new NameValuePair("xn","2013-2014"),
     new NameValuePair("xq","1"),
     new NameValuePair("ddlXq","0"),
     new NameValuePair("ddlJslb",URLEncoder.encode("多媒体教室", "GBK")),
     new NameValuePair("ddlJsmc","N1-101"),
     new NameValuePair("Button1",URLEncoder.encode("查 询", "GBK"))
   });
  } catch (IllegalArgumentException e) {
   e.printStackTrace();
  } catch (UnsupportedEncodingException e) {
   e.printStackTrace();
  }
        
         try {
   post.setQueryString(new NameValuePair[] {
     new NameValuePair("xh",xh),
     new NameValuePair("xm",URLEncoder.encode(xm, "GBK")),
     new NameValuePair("gnmkdm","N121611")
   });
  } catch (UnsupportedEncodingException e) {
   e.printStackTrace();
  }
        
         try {
   status = client.executeMethod(post);
   content = post.getResponseBodyAsString();
  } catch (HttpException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
        
 }

 private void loadPic(byte[] data) { //get.getResponseBody()获取验证码图片的字节信息,并写入output输出流中
  File file = new File("c:/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;
 }
 
}

 

加载中
0
AaronW
AaronW
用正则匹配到提交不对啊 
c
crazypig
我没用正则匹配呀,这个帖子已经很久了,不过还是谢谢你的关注
0
panzhang516
panzhang516

你好,我最近想做一个登录正方教务系统的安卓客户端,不知道从哪里入手,求助你,想拜你为师,我qq1525529578,,,,等加我吗或留下你的联系方式,,谢谢

AaronW
AaronW
我的登录已经可以了 哈哈 把你代码给我看看 可以不
0
dellch_zby
dellch_zby

你好,请问问题是怎么解决的?我也遇到这个问题,如果没有中文的URL就能获得,有中文的就不能进入。改了gb2312好像也没有作用

啦啦啦拉拉
啦啦啦拉拉
注意用http client提交参数会进行一次urlEncode,这时候用的还是UTF-8编码。我是用url直接写入字节流,这样才解决了编码问题。
0
wlchn
wlchn
我也遇到了这个问题一模一样。就是xm这里。。。请问能说一下具体的解决办法吗?
0
逍遥的小贝
逍遥的小贝

怎样mark啊,回家再试试这个方法,多谢楼主的分享

返回顶部
顶部