|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+package com.chinaitop.utils;
|
|
|
2
|
+
|
|
|
3
|
+
|
|
|
4
|
+
|
|
|
5
|
+import java.io.BufferedReader;
|
|
|
6
|
+import java.io.IOException;
|
|
|
7
|
+import java.io.InputStreamReader;
|
|
|
8
|
+import java.net.URI;
|
|
|
9
|
+import java.util.ArrayList;
|
|
|
10
|
+import java.util.Iterator;
|
|
|
11
|
+import java.util.List;
|
|
|
12
|
+import java.util.Map;
|
|
|
13
|
+
|
|
|
14
|
+import org.apache.commons.lang.ObjectUtils;
|
|
|
15
|
+import org.apache.http.HttpEntity;
|
|
|
16
|
+import org.apache.http.HttpResponse;
|
|
|
17
|
+import org.apache.http.HttpStatus;
|
|
|
18
|
+import org.apache.http.NameValuePair;
|
|
|
19
|
+import org.apache.http.StatusLine;
|
|
|
20
|
+import org.apache.http.client.HttpClient;
|
|
|
21
|
+import org.apache.http.client.entity.UrlEncodedFormEntity;
|
|
|
22
|
+import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
23
|
+import org.apache.http.client.methods.HttpGet;
|
|
|
24
|
+import org.apache.http.client.methods.HttpPost;
|
|
|
25
|
+import org.apache.http.entity.StringEntity;
|
|
|
26
|
+//import org.apache.http.entity.mime.MultipartEntityBuilder;
|
|
|
27
|
+import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
28
|
+import org.apache.http.impl.client.DefaultHttpClient;
|
|
|
29
|
+import org.apache.http.impl.client.HttpClients;
|
|
|
30
|
+import org.apache.http.message.BasicNameValuePair;
|
|
|
31
|
+import org.apache.http.protocol.HTTP;
|
|
|
32
|
+import org.apache.http.util.EntityUtils;
|
|
|
33
|
+import org.apache.log4j.LogManager;
|
|
|
34
|
+import org.apache.log4j.Logger;
|
|
|
35
|
+
|
|
|
36
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
37
|
+
|
|
|
38
|
+public class HTTPUtils {
|
|
|
39
|
+
|
|
|
40
|
+ private final static Logger logger = LogManager.getLogger(HTTPUtils.class);
|
|
|
41
|
+
|
|
|
42
|
+
|
|
|
43
|
+
|
|
|
44
|
+ /**
|
|
|
45
|
+ * get请求
|
|
|
46
|
+ * @return
|
|
|
47
|
+ */
|
|
|
48
|
+ public static String doGet(String url) {
|
|
|
49
|
+ try {
|
|
|
50
|
+ HttpClient client = new DefaultHttpClient();
|
|
|
51
|
+ //发送get请求
|
|
|
52
|
+ HttpGet request = new HttpGet(url);
|
|
|
53
|
+ HttpResponse response = client.execute(request);
|
|
|
54
|
+
|
|
|
55
|
+ /**请求发送成功,并得到响应**/
|
|
|
56
|
+ if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
|
|
|
57
|
+ /**读取服务器返回过来的json字符串数据**/
|
|
|
58
|
+ String strResult = EntityUtils.toString(response.getEntity());
|
|
|
59
|
+
|
|
|
60
|
+ return strResult;
|
|
|
61
|
+ }
|
|
|
62
|
+ }
|
|
|
63
|
+ catch (IOException e) {
|
|
|
64
|
+ e.printStackTrace();
|
|
|
65
|
+ }
|
|
|
66
|
+
|
|
|
67
|
+ return null;
|
|
|
68
|
+ }
|
|
|
69
|
+
|
|
|
70
|
+
|
|
|
71
|
+
|
|
|
72
|
+ /**
|
|
|
73
|
+ * post请求(用于key-value格式的参数)
|
|
|
74
|
+ * @param url
|
|
|
75
|
+ * @param params
|
|
|
76
|
+ * @return
|
|
|
77
|
+ */
|
|
|
78
|
+ public static String doPost(String url, Map params){
|
|
|
79
|
+
|
|
|
80
|
+ BufferedReader in = null;
|
|
|
81
|
+ try {
|
|
|
82
|
+ // 定义HttpClient
|
|
|
83
|
+ HttpClient client = new DefaultHttpClient();
|
|
|
84
|
+ // 实例化HTTP方法
|
|
|
85
|
+ HttpPost request = new HttpPost();
|
|
|
86
|
+ request.setURI(new URI(url));
|
|
|
87
|
+ //设置参数
|
|
|
88
|
+ List<NameValuePair> nvps = new ArrayList<NameValuePair>();
|
|
|
89
|
+ for (Iterator iter = params.keySet().iterator(); iter.hasNext();) {
|
|
|
90
|
+ String name = (String) iter.next();
|
|
|
91
|
+ String value = String.valueOf(params.get(name));
|
|
|
92
|
+ nvps.add(new BasicNameValuePair(name, value));
|
|
|
93
|
+
|
|
|
94
|
+ //System.out.println(name +"-"+value);
|
|
|
95
|
+ }
|
|
|
96
|
+ request.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
|
|
|
97
|
+
|
|
|
98
|
+ HttpResponse response = client.execute(request);
|
|
|
99
|
+ int code = response.getStatusLine().getStatusCode();
|
|
|
100
|
+ if(code == 200){ //请求成功
|
|
|
101
|
+ in = new BufferedReader(new InputStreamReader(response.getEntity()
|
|
|
102
|
+ .getContent(),"utf-8"));
|
|
|
103
|
+ StringBuffer sb = new StringBuffer("");
|
|
|
104
|
+ String line = "";
|
|
|
105
|
+ String NL = System.getProperty("line.separator");
|
|
|
106
|
+ while ((line = in.readLine()) != null) {
|
|
|
107
|
+ sb.append(line + NL);
|
|
|
108
|
+ }
|
|
|
109
|
+
|
|
|
110
|
+ in.close();
|
|
|
111
|
+
|
|
|
112
|
+ return sb.toString();
|
|
|
113
|
+ }
|
|
|
114
|
+ else{ //
|
|
|
115
|
+ System.out.println("状态码:" + code);
|
|
|
116
|
+ return null;
|
|
|
117
|
+ }
|
|
|
118
|
+ }
|
|
|
119
|
+ catch(Exception e){
|
|
|
120
|
+ e.printStackTrace();
|
|
|
121
|
+
|
|
|
122
|
+ return null;
|
|
|
123
|
+ }
|
|
|
124
|
+ }
|
|
|
125
|
+
|
|
|
126
|
+
|
|
|
127
|
+ /**
|
|
|
128
|
+ * post请求(用于请求json格式的参数)
|
|
|
129
|
+ * @param url
|
|
|
130
|
+ * @param params
|
|
|
131
|
+ * @return
|
|
|
132
|
+ */
|
|
|
133
|
+ public static String doPost(String url, String params) throws Exception {
|
|
|
134
|
+
|
|
|
135
|
+ CloseableHttpClient httpclient = HttpClients.createDefault();
|
|
|
136
|
+ HttpPost httpPost = new HttpPost(url);// 创建httpPost
|
|
|
137
|
+ httpPost.setHeader("Accept", "application/json");
|
|
|
138
|
+ httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
|
|
|
139
|
+ String charSet = "UTF-8";
|
|
|
140
|
+ StringEntity entity = new StringEntity(params, charSet);
|
|
|
141
|
+ httpPost.setEntity(entity);
|
|
|
142
|
+ CloseableHttpResponse response = null;
|
|
|
143
|
+
|
|
|
144
|
+ try {
|
|
|
145
|
+
|
|
|
146
|
+ response = httpclient.execute(httpPost);
|
|
|
147
|
+ StatusLine status = response.getStatusLine();
|
|
|
148
|
+ int state = status.getStatusCode();
|
|
|
149
|
+ if (state == HttpStatus.SC_OK) {
|
|
|
150
|
+ HttpEntity responseEntity = response.getEntity();
|
|
|
151
|
+ String jsonString = EntityUtils.toString(responseEntity);
|
|
|
152
|
+ return jsonString;
|
|
|
153
|
+ }
|
|
|
154
|
+ else{
|
|
|
155
|
+ logger.error("请求返回:"+state+"("+url+")");
|
|
|
156
|
+ }
|
|
|
157
|
+ }
|
|
|
158
|
+ finally {
|
|
|
159
|
+ if (response != null) {
|
|
|
160
|
+ try {
|
|
|
161
|
+ response.close();
|
|
|
162
|
+ } catch (IOException e) {
|
|
|
163
|
+ e.printStackTrace();
|
|
|
164
|
+ }
|
|
|
165
|
+ }
|
|
|
166
|
+ try {
|
|
|
167
|
+ httpclient.close();
|
|
|
168
|
+ } catch (IOException e) {
|
|
|
169
|
+ e.printStackTrace();
|
|
|
170
|
+ }
|
|
|
171
|
+ }
|
|
|
172
|
+ return null;
|
|
|
173
|
+ }
|
|
|
174
|
+
|
|
|
175
|
+
|
|
|
176
|
+ /**
|
|
|
177
|
+ * 中转文件
|
|
|
178
|
+ *
|
|
|
179
|
+ * @param file
|
|
|
180
|
+ * 上传的文件
|
|
|
181
|
+ * @return 响应结果
|
|
|
182
|
+ */
|
|
|
183
|
+ /*public static String httpClientUploadFile(MultipartFile file,String remoteurl) {
|
|
|
184
|
+ System.out.println("上传");
|
|
|
185
|
+ System.out.println("上传参数"+file);
|
|
|
186
|
+ System.out.println("上传参数"+remoteurl);
|
|
|
187
|
+ if(file.isEmpty()){
|
|
|
188
|
+ return null;
|
|
|
189
|
+ }
|
|
|
190
|
+ String remote_url=remoteurl;
|
|
|
191
|
+ CloseableHttpClient httpClient = HttpClients.createDefault();
|
|
|
192
|
+ String result = "";
|
|
|
193
|
+ try {
|
|
|
194
|
+ String fileName = file.getOriginalFilename();
|
|
|
195
|
+ Integer start = fileName.lastIndexOf(".");
|
|
|
196
|
+ Integer length = fileName.length();
|
|
|
197
|
+ String finalType = fileName.substring(start, length);
|
|
|
198
|
+ SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
|
|
|
199
|
+ fileName = df.format(new Date()) + "_" + new Random().nextInt(1000) + finalType;
|
|
|
200
|
+ HttpPost httpPost = new HttpPost(remote_url);
|
|
|
201
|
+ MultipartEntityBuilder builder = MultipartEntityBuilder.create();
|
|
|
202
|
+ builder.addBinaryBody("file", file.getInputStream(), ContentType.MULTIPART_FORM_DATA, fileName);// 文件流
|
|
|
203
|
+ builder.addTextBody("filename", fileName);// 类似浏览器表单提交,对应input的name和value
|
|
|
204
|
+ HttpEntity entity = builder.build();
|
|
|
205
|
+ httpPost.setEntity(entity);
|
|
|
206
|
+ HttpResponse response = httpClient.execute(httpPost);// 执行提交
|
|
|
207
|
+ HttpEntity responseEntity = response.getEntity();
|
|
|
208
|
+ if (responseEntity != null) {
|
|
|
209
|
+ // 将响应内容转换为字符串
|
|
|
210
|
+ result = EntityUtils.toString(responseEntity, Charset.forName("UTF-8"));
|
|
|
211
|
+ }
|
|
|
212
|
+ } catch (IOException e) {
|
|
|
213
|
+ e.printStackTrace();
|
|
|
214
|
+ } catch (Exception e) {
|
|
|
215
|
+ e.printStackTrace();
|
|
|
216
|
+ } finally {
|
|
|
217
|
+ try {
|
|
|
218
|
+ httpClient.close();
|
|
|
219
|
+ } catch (IOException e) {
|
|
|
220
|
+ e.printStackTrace();
|
|
|
221
|
+ }
|
|
|
222
|
+ }
|
|
|
223
|
+ return result;
|
|
|
224
|
+ }*/
|
|
|
225
|
+
|
|
|
226
|
+ public void main(String[] args) throws Exception {
|
|
|
227
|
+ //JSONObject json = new JSONObject();
|
|
|
228
|
+ //json.put("action", "test");
|
|
|
229
|
+ String url = "http://localhost:9022/Enum/findByEnum?id="+1162;
|
|
|
230
|
+ String strResult = doGet(url);
|
|
|
231
|
+ System.out.println(strResult);
|
|
|
232
|
+ if (!"".equals(strResult)) {
|
|
|
233
|
+ JSONObject enum_obj = JSONObject.parseObject(strResult);
|
|
|
234
|
+ String gbcode = ObjectUtils.toString(enum_obj.get("gbcode"), "");//行政区划
|
|
|
235
|
+ System.out.println("行政区划国标编码是"+gbcode);
|
|
|
236
|
+ }
|
|
|
237
|
+ }
|
|
|
238
|
+
|
|
|
239
|
+}
|