三个httpclient的小例子,希望对刚开始学习httpclient的小伙伴们有一个参考↖(^ω^)↗
1、简单的获取一个HTML页面的内容
1
2
3
4
5
6
7
8
|
public
void
getHTML()
throws
Exception {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpget =
new
HttpGet(
"http://www.baidu.com/"
);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
String html = EntityUtils.toString(entity);
httpclient.close();
}
|
2、下载一个图片到本地
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
|
public
class
DownloadFile {
public
static
void
main(String[] args)
throws
Exception {
//图片的网址
String url =
"http://ww2.sinaimg.cn/large/9d57a855jw1dqpv9fp4yuj.jpg"
;
String destfilename =
"D:\\TDDOWNLOAD\\cat.jpg"
;
//生成一个httpclient对象
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpget =
new
HttpGet(url);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
InputStream in = entity.getContent();
File file =
new
File(destfilename);
try
{
FileOutputStream fout =
new
FileOutputStream(file);
int
l = -
1
;
byte
[] tmp =
new
byte
[
1024
];
while
((l = in.read(tmp)) != -
1
) {
fout.write(tmp,
0
,l);
//注意这里如果用OutputStream.write(buff)的话,图片会失真,大家可以试试
}
fout.flush();
fout.close();
}
finally
{
// 关闭低层流。
in.close();
}
httpclient.close();
}
}
|
3.post模拟人人网登陆
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
|
import
java.io.IOException;
import
java.util.ArrayList;
import
java.util.List;
import
java.util.Scanner;
import
org.apache.http.Header;
import
org.apache.http.HttpResponse;
import
org.apache.http.NameValuePair;
import
org.apache.http.client.ClientProtocolException;
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.CloseableHttpClient;
import
org.apache.http.impl.client.HttpClients;
import
org.apache.http.message.BasicNameValuePair;
import
org.apache.http.util.EntityUtils;
public
class
LoginRR {
private
CloseableHttpClient httpclient;
private
HttpPost httppost;
//用于提交登陆数据
private
HttpGet httpget;
//用于获得登录后的页面
private
String login_success;
//用于构造上面的HttpGet
public
LoginRR()
{
httpclient = HttpClients.createDefault();
//人人的登陆界面网址
httppost =
new
HttpPost(
"http://www.renren.com/PLogin.do"
);
}
public
void
logIn(String name, String password)
throws
Exception
{
//打包将要传入的参数
List<NameValuePair> params =
new
ArrayList<NameValuePair>();
params.add(
new
BasicNameValuePair(
"email"
, name));
params.add(
new
BasicNameValuePair(
"password"
,password));
httppost.setEntity(
new
UrlEncodedFormEntity(params));
try
{
//提交登录数据
HttpResponse re = httpclient.execute(httppost);
//获得跳转的网址
Header locationHeader = re.getFirstHeader(
"Location"
);
//登陆不成功
if
(locationHeader ==
null
)
{
System.out.println(
"登陆不成功,请稍后再试!"
);
return
;
}
else
//成功
{
login_success=locationHeader.getValue();
//获取登陆成功之后跳转链接
System.out.println(
"成功之后跳转到的网页网址:"
+login_success);
}
}
catch
(ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch
(IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public
void
PrintText()
throws
IOException
{
httpget =
new
HttpGet(login_success);
HttpResponse re2 =
null
;
try
{
re2 = httpclient.execute(httpget);
//输出登录成功后的页面
String str=EntityUtils.toString(re2.getEntity());
System.out.println(str);
}
catch
(ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch
(IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
httppost.abort();
httpget.abort();
httpclient.close();
}
}
public
static
void
main(String[] args)
throws
Exception
{
String name =
"username"
, password =
"password "
;
//自己的账号,口令
LoginRR lr =
new
LoginRR();
lr.logIn(name, password);
lr.PrintText();
}
}
|