用HttpURLConnection模拟登录某个网站,以post的方式发送请求,然后得到response的状态码是200而不是302,页面没有重定向,返回的输入流是原来的登录页面。以下是一些关键代码,麻烦给位帮忙看看,我是新手 Map<String, String> post = new HashMap<String, String>();
//post.put("__VIEWSTATE", "dDwtMTIwMTU3OTE3Nzs7Pkpv5PWSSJQutGzQO79G0NxH9kuS");//这个值从foxbug上复制下来的
post.put("TextBox1", "20093069***");
post.put("TextBox2", "*****");
post.put("RadioButtonList1", "学生");
post.put("Button","");//
post.put("lbLanguage", "");//这两个的值在httpfox显示都是空的
String urlStr = "http://202.116.160.170/default2.aspx";//default2.aspx是post的方法(action="default2.aspx")
//设置请求头,建立连接
URL url = new URL(urlStr);
HttpURLConnection uc = (HttpURLConnection)url.openConnection();
uc.setRequestMethod("POST");
uc.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
uc.setRequestProperty("Host", "202.116.160.170");
uc.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 6.1; rv:11.0) Gecko/20100101 Firefox/11.0");
uc.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
uc.setRequestProperty("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
uc.setRequestProperty("Accept-Encoding", "gzip, deflate");
uc.setRequestProperty("Connection", "keep-alive");
uc.setRequestProperty("Referer", "http://202.116.160.170/");
uc.setDoOutput(true);
//uc.setFollowRedirects(true);
uc.setDoInput(true);
//将用户名、密码等以"username=XXX&password=***"的形式输出
PrintWriter pw = new PrintWriter(uc.getOutputStream());
boolean first = true;
for(Map.Entry<String, String> pair : nameValuePairs.entrySet()){
if(first){
first = false;
}
else{
pw.print('&');
}
pw.print(pair.getKey());
pw.print('=');
pw.print(URLEncoder.encode(pair.getValue(), "gb2312"));//content-type里的charset=gb2312,但是把得到输入流输出到控制台后,中文乱码,试过用'UTF-8"以及"GBK",结果一样。
}
//post.put("__VIEWSTATE", "dDwtMTIwMTU3OTE3Nzs7Pkpv5PWSSJQutGzQO79G0NxH9kuS");//这个值从foxbug上复制下来的
post.put("TextBox1", "20093069***");
post.put("TextBox2", "*****");
post.put("RadioButtonList1", "学生");
post.put("Button","");//
post.put("lbLanguage", "");//这两个的值在httpfox显示都是空的
String urlStr = "http://202.116.160.170/default2.aspx";//default2.aspx是post的方法(action="default2.aspx")
//设置请求头,建立连接
URL url = new URL(urlStr);
HttpURLConnection uc = (HttpURLConnection)url.openConnection();
uc.setRequestMethod("POST");
uc.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
uc.setRequestProperty("Host", "202.116.160.170");
uc.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 6.1; rv:11.0) Gecko/20100101 Firefox/11.0");
uc.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
uc.setRequestProperty("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
uc.setRequestProperty("Accept-Encoding", "gzip, deflate");
uc.setRequestProperty("Connection", "keep-alive");
uc.setRequestProperty("Referer", "http://202.116.160.170/");
uc.setDoOutput(true);
//uc.setFollowRedirects(true);
uc.setDoInput(true);
//将用户名、密码等以"username=XXX&password=***"的形式输出
PrintWriter pw = new PrintWriter(uc.getOutputStream());
boolean first = true;
for(Map.Entry<String, String> pair : nameValuePairs.entrySet()){
if(first){
first = false;
}
else{
pw.print('&');
}
pw.print(pair.getKey());
pw.print('=');
pw.print(URLEncoder.encode(pair.getValue(), "gb2312"));//content-type里的charset=gb2312,但是把得到输入流输出到控制台后,中文乱码,试过用'UTF-8"以及"GBK",结果一样。
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货