forked from aofeng/JavaTutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFluentApi.java
More file actions
85 lines (73 loc) · 2.81 KB
/
FluentApi.java
File metadata and controls
85 lines (73 loc) · 2.81 KB
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
/**
* 创建时间:2016-8-5
*/
package cn.aofeng.demo.httpclient;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.fluent.Request;
import org.apache.http.client.fluent.Response;
import org.apache.http.message.BasicNameValuePair;
import org.apache.log4j.Logger;
import cn.aofeng.demo.httpclient.server.SimpleHttpServer;
/**
* 使用Fluent API快速地进行简单的HTTP的请求和响应处理,启动{@link SimpleHttpServer}作为请求的服务端。
*
* @author <a href="mailto:aofengblog@163.com">聂勇</a>
*/
public class FluentApi {
private static Logger _logger = Logger.getLogger(FluentApi.class);
private static String _targetHost = "http://127.0.0.1:8888";
private static String _charset = "utf-8";
/**
* HTTP GET请求,关闭Keep-Alive。
*
* @param targetUrl 请求的地址
* @param charset 将响应流转换成字符串时使用的编码
*/
public static void get(String targetUrl, String charset) {
Response response = null;
try {
response = Request.Get(targetUrl).setHeader("Connection", "close").execute();
String content = response.returnContent().asString(Charset.forName(charset));
_logger.info(content);
} catch (ClientProtocolException e) {
_logger.error("协议问题", e);
} catch (IOException e) {
_logger.error("连接服务器或读取响应出错", e);
}
}
/**
* HTTP POST请求,默认开启Keep-Alive,表单数据使用utf-8编码。
*
* @param targetUrl 请求的地址
* @param charset 请求表单内容处理和将响应流转换成字符串时使用的编码
*/
public static void post(String targetUrl, String charset) {
List<NameValuePair> form = new ArrayList<NameValuePair>();
form.add(new BasicNameValuePair("hello", "喂"));
form.add(new BasicNameValuePair("gogogo", "走走走"));
Response response = null;
try {
response = Request.Post(targetUrl)
.bodyForm(form, Charset.forName(charset))
.execute();
String content = response.returnContent().asString(Charset.forName(charset));
_logger.info(content);
} catch (ClientProtocolException e) {
_logger.error("协议问题", e);
} catch (IOException e) {
_logger.error("连接服务器或读取响应出错", e);
}
}
/**
* @param args
*/
public static void main(String[] args) {
get(_targetHost+"/get", _charset);
post(_targetHost+"/post", _charset);
}
}