HttpClient模擬瀏覽器登錄后發起請求
瀏覽器實現這個效果需要如下幾個步驟:
1請求一個需要登錄的頁面或資源
2服務器判斷當前的會話是否包含已登錄信息。如果沒有登錄重定向到登錄頁面
3手工在登錄頁面錄入正確的賬戶信息并提交
4服務器判斷登錄信息是否正確,如果正確則將登錄成功信息保存到session中
5登錄成功后服務器端給瀏覽器返回會話的SessionID信息保存到客戶端的Cookie中
6瀏覽器自動跳轉到之前的請求地址并攜帶之前的Cookie(包含登錄成功的SessionID)
7服務器端判斷session中是否有成功登錄信息,如果有則將請求的資源反饋給瀏覽器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
package com.artsoft.demo; import java.io.FileOutputStream; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.CookieStore; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.conn.PoolingClientConnectionManager; import org.apache.http.util.EntityUtils; /** * TODO(用一句話描述該文件的作用) * * @title: HttpClientDemo.java * @author zhangjinshan-ghq * @date 2014-6-11 14:59:04 */ public class HttpClientDemo { /** * The main method. * * @param args the arguments * @throws Exception the exception */ public static void main(String[] args) throws Exception { getResoucesByLoginCookies(); } /** * 根據登錄Cookie獲取資源 * 一切異常均未處理,需要酌情檢查異常 * * @throws Exception */ private static void getResoucesByLoginCookies() throws Exception { HttpClientDemo demo = new HttpClientDemo(); String username = "......" ; // 登錄用戶 String password = "......" ; // 登錄密碼 // 需要提交登錄的信息 String urlLogin = "http://hx.buscoming.cn/Api/Security/Logon?UserCode=" + username + "&Password=" + password; // 登錄成功后想要訪問的頁面 可以是下載資源 需要替換成自己的iteye Blog地址 String urlAfter = "http://hx.buscoming.cn/Api/Security/GetLoginAccount" ; DefaultHttpClient client = new DefaultHttpClient( new PoolingClientConnectionManager()); /** * 第一次請求登錄頁面 獲得cookie * 相當于在登錄頁面點擊登錄,此處在URL中 構造參數, * 如果參數列表相當多的話可以使用HttpClient的方式構造參數 * 此處不贅述 */ HttpPost post = new HttpPost(urlLogin); HttpResponse response = client.execute(post); HttpEntity entity = response.getEntity(); CookieStore cookieStore = client.getCookieStore(); client.setCookieStore(cookieStore); /** * 帶著登錄過的cookie請求下一個頁面,可以是需要登錄才能下載的url * 此處使用的是iteye的博客首頁,如果登錄成功,那么首頁會顯示【歡迎XXXX】 * */ HttpGet get = new HttpGet(urlAfter); response = client.execute(get); entity = response.getEntity(); /** * 將請求結果放到文件系統中保存為 myindex.html,便于使用瀏覽器在本地打開 查看結果 */ String pathName = "d:\\index.html" ; writeHTMLtoFile(entity, pathName); } /** * Write htmL to file. * 將請求結果以二進制形式放到文件系統中保存為.html文件,便于使用瀏覽器在本地打開 查看結果 * * @param entity the entity * @param pathName the path name * @throws Exception the exception */ public static void writeHTMLtoFile(HttpEntity entity, String pathName) throws Exception { byte [] bytes = new byte [( int ) entity.getContentLength()]; FileOutputStream fos = new FileOutputStream(pathName); bytes = EntityUtils.toByteArray(entity); fos.write(bytes); fos.flush(); fos.close(); } } |
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
原文鏈接:https://my.oschina.net/zhangzexing/blog/897459