模拟登陆豆瓣

<font color="Gray">模拟登陆豆瓣</font>

---------
>工具:java httpclient F12
>在阅读前请先学习http相关内容,主要是首部,报文内容,cookie


1. 调试获得登陆网页时须要的cookie
>使用ie打开登陆豆瓣的[网址]( https://www.douban.com/accounts/login)
用开发人员工具(F12)查看网络,从新刷新网页,获得请求数据以下


2. 登陆账号密码 查看F12中数据,并找请求的过程

请求中所需的cookie

请求正文

这是就是登陆是所需提交的东西,其中账号密码是本身的,captcha是验证码,须要下载以及填入,也可也用opencv解决,对其他不知道的东西就要用搜索,好比发送的cookie中包含一个bid,可是bid是什么不知道,就要搜索bid的值

搜索获得第一次请求.douban.com这个域名时服务器返回的cookie中会包含bid
能够使用和httpclient 模拟访问一次douban.com,获得返回头部中Set-Cookie中的bid值

对于验证码,我访问时请求获得的验证码时sound,找到这条请求

请求的方法时get,须要包含id参数,搜索id,获得是请求时一次response的正文内容

模拟此次请求,获得 value
以上获得登陆所需的cookie以及表单,能够实现登陆

>以上是获得的过程,代码是与过程相反的,要先访问网址获得bid 和 验证码的网址 获得以后在登陆。如下代码是测试用,没有优化,没有注释,仅供参考,httpclient会自动保存cookie,因此验证以后用同一client访问会是登陆状态(保存了cookie),代码中有不少冗余代码,自行处理。仅供参考,最后打印出的是登陆后的用户主页。


```java
CloseableHttpClient client = HttpClients.custom()
.setRedirectStrategy(new LaxRedirectStrategy())
.build();
HttpGet get = new HttpGet("https://accounts.douban.com/login?source=book");
get.setHeader("Host","accounts.douban.com");
get.setHeader("Connection","Keep-Alive");
get.setHeader("Referer","https://book.douban.com");
CloseableHttpResponse response = client.execute(get);
HttpEntity entity = response.getEntity();
entity = new BufferedHttpEntity(entity);
String value = EntityUtils.toString(entity);
value = value.split("input type=\"hidden\" name=\"captcha-id\" value=\"")[1].split("\"/>")[0];
System.out.println(value);

Header[] header = response.getHeaders("Set-Cookie");

System.out.println(header[1].getValue().split(";")[0]);

String cookie = header[1].getValue().split(";")[0];
get = new HttpGet("https://www.douban.com/misc/captcha?id="+value+"&size=s");
get.setHeader("Cookie",cookie);

response = client.execute(get);
entity = response.getEntity();

String path ="D:\\yam.png";
File file = new File(path);
if(file.exists())
file.delete();
InputStream in = entity.getContent();
try {
FileOutputStream fout = new FileOutputStream(path);
int len = -1;
byte[] tmp = new byte[1024];
while ((len = in.read(tmp))!=-1)
fout.write(tmp);
fout.close();
}finally{
in.close();
}
get.releaseConnection();

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String yzm = reader.readLine();

HttpPost post = new HttpPost("https://accounts.douban.com/login");
post.setHeader("Cookie", cookie + ";ps=y");
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("source","book"));
nvps.add(new BasicNameValuePair("form_email","账号"));
nvps.add(new BasicNameValuePair("form_password","密码"));
nvps.add(new BasicNameValuePair("captcha-solution",yzm));
nvps.add(new BasicNameValuePair("captcha-id",value));
nvps.add(new BasicNameValuePair("login","登陆"));
UrlEncodedFormEntity encodedFormEntity = new UrlEncodedFormEntity(nvps);

post.setEntity(encodedFormEntity);
BufferedReader bufferedInputStream = new BufferedReader(new InputStreamReader(encodedFormEntity.getContent()));
String line = "";
while ((line = bufferedInputStream.readLine())!=null)
System.out.println(line);
response = client.execute(post);
entity = response.getEntity();
Header[]  headers = response.getHeaders("Set-Cookie");
for (Header header1 : headers) {
System.out.println(header1.getValue());
}

get = new HttpGet("https://www.douban.com/people/m9zjl/");

System.out.println(EntityUtils.toString(entity));
```




相关文章
相关标签/搜索