模擬登陸豆瓣

<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));
```




相關文章
相關標籤/搜索