httpclient:实现有验证码的模拟登陆
//1、这种方式是先把验证码的图片下载到本地、并且根据网页解析获得token值
//2、手动在控制台输入验证码
//3、因为验证码图片已经下载下来,后面就可以使用图像文字识别
package DoubanSimulate; import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List; import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document; public class DoubanSimulate {
private static HttpClient httpClient = new DefaultHttpClient(); // 登陆入口
public static void loginDouban() {
String login_src = "https://accounts.douban.com/login";
// 输入用户名及密码
String form_email = "2692613726@qq.com";
String form_password = "0315209X";
String captcha_solution = "";
// 获取验证码
String captcha_id = getImgID();
String login = "登录";
// 输入验证码
System.out.println("请输入验证码:");
BufferedReader buff = new BufferedReader(new InputStreamReader(
System.in));
try {
captcha_solution = buff.readLine();
} catch (IOException e) {
e.printStackTrace();
} // 构造参数,即模拟需要输入的参数
List<NameValuePair> list = new ArrayList<NameValuePair>();
list.add(new BasicNameValuePair("form_email", form_email));
list.add(new BasicNameValuePair("form_password", form_password));
list.add(new BasicNameValuePair("captcha-solution", captcha_solution));
list.add(new BasicNameValuePair("captcha-id", captcha_id));
list.add(new BasicNameValuePair("login", login));
HttpPost httpPost = new HttpPost(login_src);
try {
// 向后台请求数据,登陆网站
httpPost.setEntity(new UrlEncodedFormEntity(list));
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity, "utf-8");
} catch (Exception e) {
// TODO: handle exception
}
} // 获取验证码图片“token”
private static String getImgID() {
String src = "https://www.douban.com/accounts/login";
HttpGet httpGet = new HttpGet(src);
String url = "";
String token="";
try {
HttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
String content = EntityUtils.toString(entity, "utf-8");
Document doc = Jsoup.parse(content);
url = doc.select("div[class=item item-captcha]").select("div")
.select("img[id=captcha_image]").attr("src");
int start = url.indexOf("=");
int end = url.indexOf("&");
token = url.substring(start+1, end);
// 下载验证码并存储到本地
downImg(url);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//https://www.douban.com/misc/captcha?id=f7enPStQocewglTaSeFdgfTu:en&size=s
return token;
} private static void downImg(String src) throws IOException {
File fileDir = new File("D:\\IdentifyingCode");
if (!fileDir.exists()) {
fileDir.mkdirs();
}
// 图片下载保存地址
File file = new File("D:\\IdentifyingCode\\yzm.png");
if (file.exists()) {
file.delete();
}
InputStream input = null;
FileOutputStream out = null;
HttpGet httpGet = new HttpGet(src);
try {
HttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
input = entity.getContent();
int i = -1;
byte[] byt = new byte[1024];
out = new FileOutputStream(file);
while ((i = input.read(byt)) != -1) {
out.write(byt);
}
System.out.println("图片下载成功!");
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
out.close(); } //登陆后,便可以输入一个或者多个url,进行请求
public static String gethtml(String redirectLocation) {
HttpGet httpget = new HttpGet(redirectLocation);
// Create a response handler
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = "";
try {
responseBody = httpClient.execute(httpget, responseHandler);
} catch (Exception e) {
e.printStackTrace();
responseBody = null;
} finally {
httpget.abort();
// httpClient.getConnectionManager().shutdown();
}
return responseBody;
}
//一个实例,只请求了一个url
public static void main(String[] args) throws ClientProtocolException, IOException {
loginDouban();
String redir="https://www.douban.com/people/conanemily/contacts";
// System.out.println(gethtml(redir));
String cc=gethtml(redir);
System.out.println(cc); } }


httpclient:实现有验证码的模拟登陆的更多相关文章
- 爬虫之 cookie , 验证码,模拟登陆,线程
需求文档的定制 糗事百科的段子内容和作者(xpath的管道符)名称进行爬取,然后存储到mysql中or文本 http://sc.chinaz.com/jianli/free.html爬取简历模板 HT ...
- Python爬虫实战点触验证码, 模拟登陆bilibili
爬虫思路如下: 利用自动化爬虫工具 Selenium 模拟点击输入等操作来进行登录 分析页面,获取点触验证码的点触图片,通过将图片发送给超级鹰打码平台识别后获取坐标信息 根据超级鹰返回的数据,模拟坐标 ...
- python+requests模拟登陆 学校选课系统
最近学校让我们选课,每天都有不同的课需要选....然后突发奇想试试用python爬学校选课系统的课程信息 先把自己的浏览器缓存清空,然后在登陆界面按f12 如图: 可以看到登陆时候是需要验证码的,验证 ...
- HttpClient+Jsoup模拟登陆贺州学院教务系统,获取学生个人信息
前言 注:可能学校的教务系统已经做了升级,当前的程序不知道还能不能成功获取信息,加上已经毕业,我的账户已经被注销,试不了,在这里做下思路跟过程的记录. 在我的毕业设计中”基于SSM框架贺州学院校园二手 ...
- 使用webdriver+urllib爬取网页数据(模拟登陆,过验证码)
urilib是python的标准库,当我们使用Python爬取网页数据时,往往用的是urllib模块,通过调用urllib模块的urlopen(url)方法返回网页对象,并使用read()方法获得ur ...
- 第三百四十三节,Python分布式爬虫打造搜索引擎Scrapy精讲—scrapy模拟登陆和知乎倒立文字验证码识别
第三百四十三节,Python分布式爬虫打造搜索引擎Scrapy精讲—scrapy模拟登陆和知乎倒立文字验证码识别 第一步.首先下载,大神者也的倒立文字验证码识别程序 下载地址:https://gith ...
- c# 使用 HttpWebRequest模拟登陆(附带验证码)
在C#中,可以使用HttpWebRequest进行相关的模拟登陆,登陆后进行相关的操作,比如抓取数据,页面分析,制作相关登陆助手等等. 先说下流程 1.使用httpwebrequest先进入你要登录的 ...
- 用session模拟登陆,手动输入验证码
# 本练习是模拟登陆及验证码处理(把验证码下载到本地后手动输入) # 1 通过分析页面获得form表单的登陆接口为 action="https://www.douban.com/accoun ...
- 验证码破解 | Selenium模拟登陆微博
模拟登陆微博相对来说,并不难.验证码是常规的5个随机数字字母的组合,识别起来也比较容易.主要是用到许多Selenium中的知识,如定位标签.输入信息.点击等.如对Selenium的使用并不熟悉,请先移 ...
随机推荐
- C# winform ListView 的右键菜单的下级菜单的选项视情况禁用方法
ListView 和右键菜单例如以下图: 要实现功能是: 1.用户状态为[活动]时,改动用户状态为[活动]禁用,反之则反. 2.而且仅仅实用户状态为[非活动]时,[删除学员用户]才是可用状态. 功能非 ...
- Ubuntu Server 12.04 乱码
sudo vim /etc/default/locale 将 下面的内容修改 LANG="zh_CN.UTF-8" LANGUAGE="zh_CN:zh" 修改 ...
- java开发之随笔记录
1.java 保留两位小数 DecimalFormat df = new DecimalFormat("#.##"); System.out.println(df.format(1 ...
- ios开发动物园管理 继承多态的实现
// // main.m // 继承 // // #import <Foundation/Foundation.h> #import "Animal.h" #impor ...
- 数据库中表的复杂查询&分页
一.数据库中表的复杂查询 1)连接查询 1.0连接的基本的语法格式: from TABLE1 join_type TABLE2 [on (join_condition)][where (query_c ...
- 不为客户连接创建子进程的并发回射服务器( select实现 )
前言 在此前,我已经介绍了一种并发回射服务器实现( 点此进入 ).它通过调用fork函数为每个客户请求创建一个子进程.同时,我还为此服务器添加了自动消除僵尸子进程的机制.现在请想想,在客户量非常大的情 ...
- POJ 2263 Heavy Cargo(ZOJ 1952)
最短路变形或最大生成树变形. 问 目标两地之间能通过的小重量. 用最短路把初始赋为INF.其它为0.然后找 dis[v]=min(dis[u], d); 生成树就是把最大生成树找出来.直到出发和终点能 ...
- 关于myeclipse缓存问题
昨天在部署项目的时候发现,刚检出的项目jdk竟然不是1.6版本的了,而是新的(在此之前每次检出项目都会重新设置一下jdk),如图所示: 当时还在想,真好,以后就不用配置了,但是随之而来的是一个重大问题 ...
- java读取properties文件中参数值
在类文件中加入代码: //config.properties.sysInfo //sysInfo.properties在文件夹的路径为/src/config/properties/sysInfo.pr ...
- linux 脚本统计代码行数
由于实际需求,需要统计开源产品的代码行数,so,以下命令统计*.c的行数. .h,.java .同理 find . -name *.c|xargs wc -l