HttpClientUtil.java
8.37 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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package com.sitech.ismp.util;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import java.net.URI;
import java.net.URL;
/**
*
* <p>
* Title: HttpClientUtil
* </p>
* <p>
* Description: HttpClient工具类
* </p>
* <p>
* Copyright: Copyright (c) 2013
* </p>
* <p>
* Company: SI-TECH
* </p>
*
* @author huojla
* @version 1.0
* @createtime 2013-6-16 上午10:17:57
*
*/
public class HttpClientUtil {
private static final Log LOG = LogFactory.getLog(HttpClientUtil.class);
private static final String CODE_ENCODING = "UTF-8";
/**
*
* @Title: post
* @Description: Http-Post方式提交数据 用于新建操作
* @param
* @return String
* @throws
* @author huojla
* @version 1.0
* @throws HttpClientException
* @createtime 2013-6-16 上午10:18:10
*/
public static <T> String post(String url, T clazzt) throws HttpClientException {
HttpParams httpParams = new BasicHttpParams();
httpParams.setParameter("charset", CODE_ENCODING);
HttpClient client = new DefaultHttpClient(httpParams);
HttpPost post = new HttpPost(url);
String response = "";
try {
post.addHeader("Content-Type", "application/json;charset=" + CODE_ENCODING);
post.setHeader("Accept", "application/json");
post.setHeader("Content-Type", "application/json;charset=" + CODE_ENCODING);
// 转换对象To Json字符串
String clazzToJson = JacksonUtil.toJson(clazzt);
// 转码,防止中文乱码
System.out.println("Post " + url + " Data " + clazzToJson);
LOG.info("Post " + url + " Data " + clazzToJson);
StringEntity s = new StringEntity(clazzToJson, CODE_ENCODING);
post.setEntity(s);
HttpResponse res = client.execute(post);
int statusCode = res.getStatusLine().getStatusCode();
System.out.println("Post " + url + " Data " + clazzToJson + " ResultCode : " + statusCode);
LOG.info("Post " + url + " Data " + clazzToJson + " ResultCode : " + statusCode);
// 判断返回编码是否为200
if (statusCode != 200 && statusCode != 201) {
throw new HttpClientException("Post " + url + "Error!Response Code " + statusCode);
}
HttpEntity entity = res.getEntity();
response = EntityUtils.toString(entity, CODE_ENCODING);
} catch (Exception e) {
LOG.error("Post " + url + "Error!" + e.getMessage(), e);
throw new HttpClientException("Post " + url + "Error!" + e.getMessage(), e);
}
return response;
}
/**
*
* @Title: delete
* @Description: Http Delete请求 用于删除操作
* @param
* @return String
* @throws
* @author huojla
* @version 1.0
* @throws HttpClientException
* @createtime 2013-7-24 上午9:14:18
*/
public static <T> String delete(String url) throws HttpClientException {
HttpParams httpParams = new BasicHttpParams();
httpParams.setParameter("charset", CODE_ENCODING);
HttpClient client = new DefaultHttpClient(httpParams);
HttpDelete delete = new HttpDelete(url);
String response = "";
try {
delete.addHeader("Content-Type", "application/json;charset=" + CODE_ENCODING);
delete.setHeader("Accept", "application/json");
delete.setHeader("Content-Type", "application/json;charset=" + CODE_ENCODING);
// 转码,防止中文乱码
System.out.println("Delete " + url);
HttpResponse res = client.execute(delete);
int statusCode = res.getStatusLine().getStatusCode();
System.out.println("Delete " + url + " ResultCode : " + statusCode);
LOG.info("Delete " + url + " ResultCode : " + statusCode);
// 判断返回编码是否为200
if (statusCode != 200 && statusCode != 201) {
throw new HttpClientException("Delete " + url + "Error!Response Code " + statusCode);
}
HttpEntity entity = res.getEntity();
response = EntityUtils.toString(entity, CODE_ENCODING);
} catch (Exception e) {
LOG.error("Delete " + url + "Error!" + e.getMessage(), e);
throw new HttpClientException("Delete " + url + "Error!" + e.getMessage(), e);
}
return response;
}
/**
*
* @Title: put
* @Description: Http Put 请求 用于修改操作
* @param
* @return String
* @throws
* @author huojla
* @version 1.0
* @throws HttpClientException
* @createtime 2013-7-24 上午9:50:05
*/
public static <T> String put(String url, T clazz) throws HttpClientException {
HttpParams httpParams = new BasicHttpParams();
httpParams.setParameter("charset", CODE_ENCODING);
HttpClient client = new DefaultHttpClient(httpParams);
HttpPut put = new HttpPut(url);
String response = "";
try {
put.addHeader("Content-Type", "application/json;charset=" + CODE_ENCODING);
put.setHeader("Accept", "application/json");
put.setHeader("Content-Type", "application/json;charset=" + CODE_ENCODING);
// 转换对象To Json字符串
String clazzToJson = JacksonUtil.toJson(clazz);
// 转码,防止中文乱码
System.out.println("Put " + url + " Data " + clazzToJson);
LOG.info("Put " + url + " Data " + clazzToJson);
StringEntity s = new StringEntity(clazzToJson, CODE_ENCODING);
put.setEntity(s);
HttpResponse res = client.execute(put);
int statusCode = res.getStatusLine().getStatusCode();
System.out.println("Put " + url + " Data " + clazzToJson + " ResultCode : " + statusCode);
LOG.info("Put " + url + " Data " + clazzToJson + " ResultCode : " + statusCode);
// 判断返回编码是否为200
if (statusCode != 200 && statusCode != 201) {
throw new HttpClientException("Put " + url + "Error!Response Code " + statusCode);
}
HttpEntity entity = res.getEntity();
response = EntityUtils.toString(entity, CODE_ENCODING);
} catch (Exception e) {
LOG.error("Put " + url + "Error!" + e.getMessage(), e);
throw new HttpClientException("Put " + url + "Error!" + e.getMessage(), e);
}
return response;
}
/**
*
* @Title: get
* @Description: Http get方式获取数据 用于查询
* @param
* @return String
* @throws
* @author huojla
* @version 1.0
* @throws HttpClientException
* @createtime 2013-6-16 上午10:26:20
*/
public static String get(String url) throws HttpClientException {
HttpParams httpParams = new BasicHttpParams();
httpParams.setParameter("charset", CODE_ENCODING);
HttpClient client = new DefaultHttpClient(httpParams);
HttpGet get = new HttpGet(url);
String response = "";
try {
get.addHeader("Content-Type", "application/json;charset=" + CODE_ENCODING);
get.setHeader("Accept", "application/json");
get.setHeader("Content-Type", "application/json;charset=" + CODE_ENCODING);
HttpResponse res = client.execute(get);
int statusCode = res.getStatusLine().getStatusCode();
System.out.println("Get " + url + " ResultCode : " + statusCode);
LOG.info("Get " + url + " ResultCode : " + statusCode);
// 判断返回编码是否为200
if (statusCode != 200 && statusCode != 201) {
throw new HttpClientException("Get " + url + "Error!Response Code " + statusCode);
}
HttpEntity entity = res.getEntity();
response = EntityUtils.toString(entity, CODE_ENCODING);
} catch (Exception e) {
LOG.error("Get " + url + "异常!" + e.getMessage(), e);
throw new HttpClientException("Get " + url + "异常!" + e.getMessage(), e);
}
return response;
}
/**
* 通过url来获取
*
* @param url
* @return
*/
public static String getByUrl(String url) {
HttpParams httpParams = new BasicHttpParams();
httpParams.setParameter("Accept", "application/json");
httpParams.setParameter("Content-Type", "application/json;charset=" + CODE_ENCODING);
HttpClient client = new DefaultHttpClient(httpParams);
String response = "";
try {
URL u = new URL(url);
URI uri = new URI(u.getProtocol(), null, u.getHost(), u.getPort(), u.getPath(), u.getQuery(), null);
HttpGet get = new HttpGet(uri);
HttpResponse res = client.execute(get);
System.out.println(url);
HttpEntity entity = res.getEntity();
response = EntityUtils.toString(entity, CODE_ENCODING);
} catch (Exception e) {
throw new RuntimeException(e);
}
return response;
}
}