使用httpclient模拟登录(Cookies使用)

本文提供了一个使用 Java 的 HttpClient 库来实现网页抓取、文件下载及模拟登录操作的完整示例。示例包括获取 HTML 页面内容、下载文件到本地、通过 Post 方法登录网站并设置 Cookie 来访问受限页面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文地址:http://www.oschina.net/code/snippet_179804_46120


[1].[代码] [Java]代码 跳至[1]

?
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
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.CookieStore;
import org.apache.http.client.HttpClient;
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.cookie.Cookie;
import org.apache.http.impl.client.AbstractHttpClient;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
 
public class HttpClientLoginTest {
     @Test
     // 获取一个HTML页面的内容,一个简单的get应用
     public void grabPageHTML() throws Exception {
         HttpClient httpclient = new DefaultHttpClient();
         HttpGet httpget = new HttpGet( "http://www.baidu.com/" );
         HttpResponse response = httpclient.execute(httpget);
         HttpEntity entity = response.getEntity();
         String html = EntityUtils.toString(entity, "GBK" );
 
         // releaseConnection等同于reset,作用是重置request状态位,为下次使用做好准备。
         // 其实就是用一个HttpGet获取多个页面的情况下有效果;否则可以忽略此方法。
         httpget.releaseConnection();
 
         System.out.println(html);
     }
 
     // 下载一个文件到本地(本示范中为一个验证码图片)
     @Test
     public void downloadFile() throws Exception {
         String url = "http://www.lashou.com/account/captcha" ;
         File dir = new File( "D:\\TDDOWNLOAD" );
         if (!dir.exists()) {
             dir.mkdirs();
         }
         String destfilename = "D:\\TDDOWNLOAD\\yz.png" ;
         HttpClient httpclient = new DefaultHttpClient();
         HttpGet httpget = new HttpGet(url);
         File file = new File(destfilename);
         if (file.exists()) {
             file.delete();
         }
 
         HttpResponse response = httpclient.execute(httpget);
         HttpEntity entity = response.getEntity();
         InputStream in = entity.getContent();
         try {
             FileOutputStream fout = new FileOutputStream(file);
             int l = - 1 ;
             byte [] tmp = new byte [ 2048 ];
             while ((l = in.read(tmp)) != - 1 ) {
                 fout.write(tmp);
             }
             fout.close();
         } finally {
             // 在用InputStream处理HttpEntity时,切记要关闭低层流。
             in.close();
         }
 
         httpget.releaseConnection();
     }
 
     @Test
     // Post方法,模拟表单提交参数登录到网站。
     // 结合了上面两个方法:grabPageHTML/downloadFile,同时增加了Post的代码。
     public void login2Lashou() throws Exception {
         // 第一步:先下载验证码到本地
         String url = "http://www.lashou.com/account/captcha" ;
         String destfilename = "D:\\TDDOWNLOAD\\yz.png" ;
         HttpClient httpclient = new DefaultHttpClient();
         HttpGet httpget = new HttpGet(url);
         File file = new File(destfilename);
         if (file.exists()) {
             file.delete();
         }
 
         HttpResponse response = httpclient.execute(httpget);
         HttpEntity entity = response.getEntity();
         InputStream in = entity.getContent();
         try {
             FileOutputStream fout = new FileOutputStream(file);
             int l = - 1 ;
             byte [] tmp = new byte [ 2048 ];
             while ((l = in.read(tmp)) != - 1 ) {
                 fout.write(tmp);
             }
             fout.close();
         } finally {
             in.close();
         }
         httpget.releaseConnection();
 
         // 第二步:用Post方法带若干参数尝试登录,需要手工输入下载验证码中显示的字母、数字
         BufferedReader br = new BufferedReader( new InputStreamReader(System.in));
         System.out.println( "请输入下载下来的验证码中显示的数字..." );
         String yan = br.readLine();
 
         HttpPost httppost = new HttpPost( "http://www.lashou.com/account/login/" );
         List<NameValuePair> params = new ArrayList<NameValuePair>();
         params.add( new BasicNameValuePair( "user_id" , "testuser007" ));
         params.add( new BasicNameValuePair( "pwd" , "asdfg123" ));
         params.add( new BasicNameValuePair( "yan" , yan));
         params.add( new BasicNameValuePair( "save_user" , "on" ));
         params.add( new BasicNameValuePair( "save_pwd" , "on" ));
         params.add( new BasicNameValuePair( "sub" , "登录" ));
         httppost.setEntity( new UrlEncodedFormEntity(params));
 
         response = httpclient.execute(httppost);
         entity = response.getEntity();
         // 在这里可以用Jsoup之类的工具对返回结果进行分析,以判断登录是否成功
         String postResult = EntityUtils.toString(entity, "GBK" );
         // 我们这里只是简单的打印出当前Cookie值以判断登录是否成功。
         CookieStore cookieStore = ((AbstractHttpClient) httpclient).getCookieStore();
         List<Cookie> cookies = ((AbstractHttpClient) httpclient)
                 .getCookieStore().getCookies();
         for (Cookie cookie : cookies)
             System.out.println( "cookie begin***\n" + cookie + "\n cookie end" );
         httppost.releaseConnection();
 
         // 第三步:打开会员页面以判断登录成功(未登录用户是打不开会员页面的)
         String memberpage = "http://www.lashou.com/account/orders/" ;
         httpget = new HttpGet(memberpage);
         response = httpclient.execute(httpget); // 必须是同一个HttpClient!
         entity = response.getEntity();
         String html = EntityUtils.toString(entity, "GBK" );
         httpget.releaseConnection();
 
         System.out.println(html);
     }
 
     @Test
     public void testSystemIn() throws IOException {
         BufferedReader reader = new BufferedReader( new InputStreamReader(
                 System.in));
         String hello = reader.readLine();
         System.out.println(hello);
     }
 
     @Test
     // 设置已获取的cookie,查看需要登录后才能查看的页面
     public void testGetinfoByLoginCookie() throws Exception, IOException {
         DefaultHttpClient httpclient = new DefaultHttpClient();
         CookieStore cookieStore = new BasicCookieStore();
         BasicClientCookie cookie1 = new BasicClientCookie( "ThinkID" , "5s4tmqem08gh091v3egoa7sqf7" );
         cookie1.setDomain( ".lashou.com" );
         BasicClientCookie cookie2 = new BasicClientCookie( "city_b" , "2419" );
         cookie2.setDomain( "lashou.com" );
         BasicClientCookie cookie3 = new BasicClientCookie( "client_key" , "1425104707wd157b4b24ff70adcb875a" );
         cookie3.setDomain( "lashou.com" );
         BasicClientCookie cookie4 = new BasicClientCookie( "login_name2" , "testuser007" );
         cookie4.setDomain( "lashou.com" );
         BasicClientCookie cookie5 = new BasicClientCookie( "pwd2" , "4c88a4062736c26572d3ec382868fa2b" );
         cookie5.setDomain( "lashou.com" );
         cookieStore.addCookie(cookie1);
         cookieStore.addCookie(cookie2);
         cookieStore.addCookie(cookie3);
         cookieStore.addCookie(cookie4);
         cookieStore.addCookie(cookie5);
         List<Cookie> cookies = new ArrayList<Cookie>();
         httpclient.setCookieStore(cookieStore);
         
         List<Cookie> cookieList = httpclient.getCookieStore().getCookies();
         for ( int i= 0 ;i<cookieList.size();i++){
             System.out.println( "cookie" +i+ ":" +cookieList.get(i));
         }
         
         // 设置已登录的cookie
         String memberpage = "http://www.lashou.com/account/orders/" ;
         HttpGet httpget = new HttpGet(memberpage);
         HttpResponse response = httpclient.execute(httpget); // 必须是同一个HttpClient!
         HttpEntity entity = response.getEntity();
         entity = response.getEntity();
         String html = EntityUtils.toString(entity, "GBK" );
         httpget.releaseConnection();
 
         System.out.println(html);
     }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值