【java】使用URL和CookieManager爬取页面的验证码和cookie并保存
2024-10-20 00:40:45
使用java的net包和io包下的几个工具爬取页面的验证码图片并保存到本地。
然后可以把获取的cookie保存下来,做进一步处理。比如通过识别验证码,进一步使用验证码和用户名,密码,保存下来的cookie提交表单验证。使用java模拟登录功能
package com.carl.carlapp.test; import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.CookieStore;
import java.net.HttpCookie;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;
import java.util.List; /**
* @author 作者 Carl Zhang. E-mail: carlzhangweiwen@sina.com
* @version 创建时间:2016年3月2日 下午10:39:52
* 类说明
*/
public class CookieTest {
public static void main(String args[]) throws Exception {
// String urlString = "http://58.215.195.18:10010/login_person.jsp";
String urlString = "http://58.215.195.18:10010/jcaptcha?date="+ new Date().getTime(); CookieManager manager = new CookieManager();
CookieHandler.setDefault(manager);
URL url = new URL(urlString);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection(); //将得到的验证码保存下来
saveFile(httpConn, "E:\\tset33.jpg"); // Object content = httpConn.getContent();
// String contentType = httpConn.getContentType();
// System.out.println(contentType);//MIME type:text/html //因为http已经做了请求,所以会得到cookie
CookieStore cookieJar = manager.getCookieStore();
List<HttpCookie> cookies = cookieJar.getCookies();
for (HttpCookie cookie : cookies) {
System.out.println(cookie);
}
}
public static void saveFile(URLConnection conn,String fullPath){
saveFile(conn, fullPath, 8);
}
/**
* 讲文件保存下来
* @param conn URLConnection连接
* @param fullPath 文件路径及文件名
* @param length 每次读文件字节数
*/
public static void saveFile(URLConnection conn, String fullPath, int length){
try {
if(conn == null){
throw new Exception("Can't get URLConnection.");
}
InputStream is = conn.getInputStream();
FileOutputStream fos = new FileOutputStream(fullPath);
byte[] b = new byte[length];
int len = 0;
while((len = is.read(b)) != -1){
fos.write(b,0,len);
}
fos.flush();
fos.close();
is.close();
} catch (Exception e) {
e.printStackTrace();
}
} }
打印结果:
BIGipServerweb_server=202025152.36895.0000
JSESSIONID=1D61F297617400C594B3F75E3C76D27F
最新文章
- Openwrt 编译报错:rootfs image is too big解决方法
- 2016年10月30日--JavaScript语法
- 【hdu4366】dfs序线段树
- shell变量注意事项
- Hbase Shell常用命令
- LinQ系列文章
- Postfix之telnet测试
- Web Component 文章
- HDU 5328 Problem Killer(水题)
- THREE.js代码备份——canvas - lines - colors(希尔伯特曲线3D、用HSL设置线颜色)
- BNUOJ-26586 Simon the Spider 最小生成树+枚举
- 关于数据库一致改关闭下redo日志文件丢失的处理办法的总结
- 项目中那些事|ListView中嵌套ListView问题
- CentOS yum 安装 PHP 5.6.24
- python 第一课 helloworld
- 从MySQL Bug#67718浅谈B+树索引的分裂优化(转)
- echart力导向图
- css 设置背景图片铺满固定不动
- bzoj3065
- python IDE安装-mac