【java】使用URL和CookieManager爬取頁面的驗證碼和cookie並保存

使用java的net包和io包下的幾個工具爬取頁面的驗證碼圖片並保存到本地。html

而後能夠把獲取的cookie保存下來,作進一步處理。好比經過識別驗證碼,進一步使用驗證碼和用戶名,密碼,保存下來的cookie提交表單驗證。使用java模擬登陸功能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 }

打印結果:web

BIGipServerweb_server=202025152.36895.0000
JSESSIONID=1D61F297617400C594B3F75E3C76D27Fcookie

相關文章
相關標籤/搜索