跪求各位大神解答~~~
1 public void doLogin() throws Exception{ 2 //获取连接登录网页 3 HttpClient httpclient = new DefaultHttpClient(); 4 HttpGet httpget = new HttpGet("http://www.12333sh.gov.cn/sbsjb/wzb/Bmblist.jsp"); 5 HttpResponse response = httpclient.execute(httpget); 6 HttpEntity entity = response.getEntity(); 7 EntityUtils.toString(entity, "GBK"); 8 9 10 HttpPost httppost = new HttpPost("http://www.12333sh.gov.cn/sbsjb/wzb/129.jsp"); 11 List<NameValuePair> params = new ArrayList<NameValuePair>(); 12 params.add(new BasicNameValuePair("userid", userid)); 13 params.add(new BasicNameValuePair("userpw", userpw)); 14 params.add(new BasicNameValuePair("userjym", userjym)); 15 httppost.setEntity(new UrlEncodedFormEntity(params)); 16 17 response = httpclient.execute(httppost); 18 entity = response.getEntity(); 19 String postEntity = EntityUtils.toString(entity, "GBK"); 20 System.out.println("postEntity \r\n"+postEntity); 21 int statusCode = response.getStatusLine().getStatusCode(); 22 System.out.println(response.getStatusLine()); 23 if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY){ 24 25 }else{ 26 System.out.println("登录失败!"); 27 return; 28 } 29 30 List<Cookie> cookies = ((AbstractHttpClient) httpclient).getCookieStore().getCookies(); 31 for (Cookie cookie : cookies){ 32 System.out.println("cookie begin***\r\n" + cookie + "\r\n cookie end"); 33 } 34 35 //获取社保缴纳信息 36 String sbsjUrl = "http://www.12333sh.gov.cn/sbsjb/wzb/sbsjbcx.jsp"; 37 httpget = new HttpGet(sbsjUrl); 38 System.out.println("httpget "+httpget.getMethod()); 39 response = httpclient.execute(httpget); // 必须是同一个HttpClient! 40 entity = response.getEntity(); 41 String sbsjHtml = EntityUtils.toString(entity, "GBK"); 42 System.out.println(sbsjHtml); 43 }
System.out.println("postEntity \r\n"+postEntity);
输出的文件还是原来的登录界面,而不是登录后的提示登录失败界面?
登录界面有个验证码,现在的验证码我是随便写了个固定的值会登录失败的,是因为这个原因吗?
System.out.println(response.getStatusLine());
输出值为:HTTP/1.1 200 OK
要换到别的页面你需要在服务端forward或redirect到其他的地址上;或者返回的页面中包含一个重定向的页面地址
现在问题是我的代码可能是没有登录成功,如果登录成功我打开第二次访问的页面是不是就可以抓到网页上的数据?
我在浏览器打开界面登录之后,在开一个窗口直接把第二次要访问的地址直接贴上去是可以访问的,那这块的代码是不是也是应该可以抓到数据?