【java】使用URL和CookieManager爬取页面的验证码和cookie并保存

使用java的net包和io包下的几个工具爬取页面的验证码图片并保存到本地。

然后可以把获取的cookie保存下来,做进一步处理。比如通过识别验证码,进一步使用验证码和用户名,密码,保存下来的cookie提交表单验证。使用java模拟登录功能

 

 1 package com.carl.carlapp.test;
 2 
 3 import java.io.FileOutputStream;
 4 import java.io.InputStream;
 5 import java.net.CookieHandler;
 6 import java.net.CookieManager;
 7 import java.net.CookieStore;
 8 import java.net.HttpCookie;
 9 import java.net.HttpURLConnection;
10 import java.net.URL;
11 import java.net.URLConnection;
12 import java.util.Date;
13 import java.util.List;
14 
15 /** 
16  * @author 作者 Carl Zhang. E-mail: carlzhangweiwen@sina.com
17  * @version 创建时间:2016年3月2日 下午10:39:52 
18  * 类说明 
19  */
20 public class CookieTest {
21      public static void main(String args[]) throws Exception {
22 //            String urlString = "http://58.215.195.18:10010/login_person.jsp";
23              String urlString = "http://58.215.195.18:10010/jcaptcha?date="+ new Date().getTime();         
24             
25             CookieManager manager = new CookieManager();
26             CookieHandler.setDefault(manager);
27             URL url = new URL(urlString);   
28             HttpURLConnection httpConn = (HttpURLConnection) url.openConnection(); 
29             
30             //将得到的验证码保存下来
31             saveFile(httpConn, "E:\\tset33.jpg");
32             
33 //            Object content = httpConn.getContent();
34 //            String contentType = httpConn.getContentType();
35 //            System.out.println(contentType);//MIME type:text/html
36             
37             //因为http已经做了请求,所以会得到cookie
38             CookieStore cookieJar = manager.getCookieStore();
39             List<HttpCookie> cookies = cookieJar.getCookies();
40             for (HttpCookie cookie : cookies) {
41               System.out.println(cookie);
42             }
43           }
44      public static void saveFile(URLConnection conn,String fullPath){
45          saveFile(conn, fullPath, 8);
46      }
47      /**
48       * 讲文件保存下来
49       * @param conn URLConnection连接
50       * @param fullPath 文件路径及文件名
51       * @param length 每次读文件字节数
52       */
53      public static void saveFile(URLConnection conn, String fullPath, int length){
54             try {
55                 if(conn == null){
56                     throw new Exception("Can't get URLConnection.");
57                 }
58                 InputStream is = conn.getInputStream();
59                 FileOutputStream fos = new FileOutputStream(fullPath);
60                 byte[] b = new byte[length];
61                 int len = 0;
62                 while((len = is.read(b)) != -1){
63                     fos.write(b,0,len); 
64                 }
65                 fos.flush();
66                 fos.close();
67                 is.close();
68             } catch (Exception e) {
69                 e.printStackTrace();
70             }
71         }
72 
73 }

打印结果:

BIGipServerweb_server=202025152.36895.0000
JSESSIONID=1D61F297617400C594B3F75E3C76D27F

 

posted @ 2016-03-02 23:54  jinhuazhe2013  阅读(1707)  评论(0编辑  收藏  举报