使用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

最新文章

  1. Openwrt 编译报错:rootfs image is too big解决方法
  2. 2016年10月30日--JavaScript语法
  3. 【hdu4366】dfs序线段树
  4. shell变量注意事项
  5. Hbase Shell常用命令
  6. LinQ系列文章
  7. Postfix之telnet测试
  8. Web Component 文章
  9. HDU 5328 Problem Killer(水题)
  10. THREE.js代码备份——canvas - lines - colors(希尔伯特曲线3D、用HSL设置线颜色)
  11. BNUOJ-26586 Simon the Spider 最小生成树+枚举
  12. 关于数据库一致改关闭下redo日志文件丢失的处理办法的总结
  13. 项目中那些事|ListView中嵌套ListView问题
  14. CentOS yum 安装 PHP 5.6.24
  15. python 第一课 helloworld
  16. 从MySQL Bug#67718浅谈B+树索引的分裂优化(转)
  17. echart力导向图
  18. css 设置背景图片铺满固定不动
  19. bzoj3065
  20. python IDE安装-mac

热门文章

  1. oracle环境变量
  2. hdu1085
  3. Free Style Structure text
  4. 清北刷题冲刺 10-28 p.m
  5. 一个APK反编译利器Apktool
  6. Hadoop 3.0完全分布式集群搭建方法(CentOS 7+Hadoop 3.2.0)
  7. Mysql实例参数优化15个主要参数讲解(原创)
  8. 基于ECharts的股票行情分时、K线、MACD、DIF、DEA图表 (绝无仅有)
  9. lynis检测
  10. ldap第一天 编译安装LDAP + ldapadmin