Przeglądaj źródła

质检给市平台推送数据的接口,并且把该表的关键字字段改为非关键字

mengy 5 lat temu
rodzic
commit
d34c3edab1
1 zmienionych plików z 245 dodań i 0 usunięć
  1. 245 0
      src/main/java/com/chinaitop/depot/utils/HttpUtil.java

+ 245 - 0
src/main/java/com/chinaitop/depot/utils/HttpUtil.java

@@ -0,0 +1,245 @@
1
+package com.chinaitop.depot.utils;
2
+
3
+import org.apache.http.*;
4
+import org.apache.http.client.HttpClient;
5
+import org.apache.http.client.entity.UrlEncodedFormEntity;
6
+import org.apache.http.client.methods.CloseableHttpResponse;
7
+import org.apache.http.client.methods.HttpGet;
8
+import org.apache.http.client.methods.HttpPost;
9
+import org.apache.http.entity.StringEntity;
10
+import org.apache.http.impl.client.CloseableHttpClient;
11
+import org.apache.http.impl.client.DefaultHttpClient;
12
+import org.apache.http.impl.client.HttpClientBuilder;
13
+import org.apache.http.impl.client.HttpClients;
14
+import org.apache.http.message.BasicNameValuePair;
15
+import org.apache.http.protocol.HTTP;
16
+import org.apache.http.util.EntityUtils;
17
+import org.springframework.http.HttpHeaders;
18
+import org.springframework.http.converter.HttpMessageConverter;
19
+import org.springframework.http.converter.StringHttpMessageConverter;
20
+import org.springframework.web.client.RestTemplate;
21
+
22
+import java.io.BufferedReader;
23
+import java.io.IOException;
24
+import java.io.InputStreamReader;
25
+import java.net.URI;
26
+import java.nio.charset.StandardCharsets;
27
+import java.util.ArrayList;
28
+import java.util.Iterator;
29
+import java.util.List;
30
+import java.util.Map;
31
+
32
+/**
33
+ * @author qingsong.han
34
+ * @description: http工具类
35
+ * @create 2020-04-19 12:48
36
+ */
37
+public class HttpUtil {
38
+
39
+    /**
40
+     * 使用apache的HttpClient发送http
41
+     *
42
+     * @param wsdlURL
43
+     *            请求URL
44
+     * @param contentType
45
+     *            如:application/json;charset=utf8
46
+     * @param content
47
+     *            数据内容
48
+     * @DATE 2018年9月22日 下午10:29:17
49
+     */
50
+    public static String doHttpPostByHttpClient(final String wsdlURL, final String contentType, final String content) throws IOException {
51
+        // 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的)
52
+        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
53
+        // 创建Post请求
54
+//        HttpPost httpPost = new HttpPost(wsdlURL);
55
+        HttpGet httpGet = new HttpGet(wsdlURL);
56
+        /*StringEntity entity = new StringEntity(content, "UTF-8");
57
+        // 将数据放入entity中
58
+        httpPost.setEntity(entity);
59
+        httpPost.setHeader("Content-Type", contentType);*/
60
+        // 响应模型
61
+        CloseableHttpResponse response = null;
62
+        String result = "";
63
+        try {
64
+            // 由客户端执行(发送)Post请求
65
+//            response = httpClient.execute(httpPost);
66
+            response = httpClient.execute(httpGet);
67
+            // 从响应模型中获取响应实体
68
+            // 注意:和doHttpPostByRestTemplate方法用的不是同一个HttpEntity
69
+            HttpEntity responseEntity = response.getEntity();
70
+            System.out.println("响应ContentType为:" + responseEntity.getContentType());
71
+            System.out.println("响应状态为:" + response.getStatusLine());
72
+            if (responseEntity != null) {
73
+                result = EntityUtils.toString(responseEntity);
74
+                System.out.println("响应内容为:" + result);
75
+            }
76
+        } finally {
77
+            // 释放资源
78
+            if (httpClient != null) {
79
+                httpClient.close();
80
+            }
81
+            if (response != null) {
82
+                response.close();
83
+            }
84
+        }
85
+        return result;
86
+    }
87
+
88
+    /**
89
+     * 使用springframework的RestTemplate发送http
90
+     *
91
+     * @param wsdlURL
92
+     *            请求URL
93
+     * @param contentType
94
+     *            如:application/json;charset=utf8
95
+     * @param content
96
+     *            数据内容
97
+     * @DATE 2018年9月22日 下午10:30:48
98
+     */
99
+    public static String doHttpPostByRestTemplate(final String wsdlURL, final String contentType, final String content) {
100
+        // http使用无参构造;https需要使用有参构造
101
+        RestTemplate restTemplate = new RestTemplate();
102
+        // 解决中文乱码
103
+        List<HttpMessageConverter<?>> converterList = restTemplate.getMessageConverters();
104
+        converterList.remove(1);
105
+        HttpMessageConverter<?> converter = new StringHttpMessageConverter(StandardCharsets.UTF_8);
106
+        converterList.add(1, converter);
107
+        restTemplate.setMessageConverters(converterList);
108
+        // 设置Content-Type
109
+        HttpHeaders headers = new HttpHeaders();
110
+        headers.remove("Content-Type");
111
+        headers.add("Content-Type", contentType);
112
+        // 数据信息封装
113
+        // 注意:和doHttpPostByHttpClient方法用的不是同一个HttpEntity
114
+        org.springframework.http.HttpEntity<String> formEntity = new org.springframework.http.HttpEntity<String>(
115
+                content, headers);
116
+        String result = restTemplate.postForObject(wsdlURL, formEntity, String.class);
117
+        return result;
118
+    }
119
+
120
+    /**
121
+     * post请求(用于key-value格式的参数)
122
+     * @param url
123
+     * @param params
124
+     * @return
125
+     */
126
+    public static String doPost(String url, Map params){
127
+
128
+        BufferedReader in = null;
129
+        try {
130
+            // 定义HttpClient
131
+            HttpClient client = new DefaultHttpClient();
132
+            // 实例化HTTP方法
133
+            HttpPost request = new HttpPost();
134
+            request.setURI(new URI(url));
135
+            //设置参数
136
+            List<NameValuePair> nvps = new ArrayList<NameValuePair>();
137
+            for (Iterator iter = params.keySet().iterator(); iter.hasNext();) {
138
+                String name = (String) iter.next();
139
+                String value = String.valueOf(params.get(name));
140
+                nvps.add(new BasicNameValuePair(name, value));
141
+
142
+                //System.out.println(name +"-"+value);
143
+            }
144
+            request.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
145
+
146
+            HttpResponse response = client.execute(request);
147
+            int code = response.getStatusLine().getStatusCode();
148
+            if(code == 200){	//请求成功
149
+                in = new BufferedReader(new InputStreamReader(response.getEntity()
150
+                        .getContent(),"utf-8"));
151
+                StringBuffer sb = new StringBuffer("");
152
+                String line = "";
153
+                String NL = System.getProperty("line.separator");
154
+                while ((line = in.readLine()) != null) {
155
+                    sb.append(line + NL);
156
+                }
157
+
158
+                in.close();
159
+
160
+                return sb.toString();
161
+            }
162
+            else{	//
163
+                System.out.println("状态码:" + code);
164
+                return null;
165
+            }
166
+        }
167
+        catch(Exception e){
168
+            e.printStackTrace();
169
+
170
+            return null;
171
+        }
172
+    }
173
+
174
+    /**
175
+     * get请求
176
+     * @return
177
+     */
178
+    public static String doGet(String url) {
179
+        try {
180
+            HttpClient client = new DefaultHttpClient();
181
+            //发送get请求
182
+            HttpGet request = new HttpGet(url);
183
+            HttpResponse response = client.execute(request);
184
+
185
+            /**请求发送成功,并得到响应**/
186
+            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
187
+                /**读取服务器返回过来的json字符串数据**/
188
+                String strResult = EntityUtils.toString(response.getEntity());
189
+
190
+                return strResult;
191
+            }
192
+        }
193
+        catch (IOException e) {
194
+            e.printStackTrace();
195
+        }
196
+
197
+        return null;
198
+    }
199
+
200
+    /**
201
+     * post请求(用于请求json格式的参数)
202
+     * @param url
203
+     * @param params
204
+     * @return
205
+     */
206
+    public static String doPost(String url, String params) throws Exception {
207
+
208
+        CloseableHttpClient httpclient = HttpClients.createDefault();
209
+        HttpPost httpPost = new HttpPost(url);// 创建httpPost
210
+        httpPost.setHeader("Accept", "application/json");
211
+//        httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
212
+        httpPost.setHeader("Content-Type", "application/json");
213
+        String charSet = "UTF-8";
214
+        StringEntity entity = new StringEntity(params, charSet);
215
+        httpPost.setEntity(entity);
216
+        CloseableHttpResponse response = null;
217
+
218
+        try {
219
+
220
+            response = httpclient.execute(httpPost);
221
+            StatusLine status = response.getStatusLine();
222
+            int state = status.getStatusCode();
223
+            if (state == HttpStatus.SC_OK) {
224
+                HttpEntity responseEntity = response.getEntity();
225
+                String jsonString = EntityUtils.toString(responseEntity);
226
+                return jsonString;
227
+            }
228
+        }
229
+        finally {
230
+            if (response != null) {
231
+                try {
232
+                    response.close();
233
+                } catch (IOException e) {
234
+                    e.printStackTrace();
235
+                }
236
+            }
237
+            try {
238
+                httpclient.close();
239
+            } catch (IOException e) {
240
+                e.printStackTrace();
241
+            }
242
+        }
243
+        return null;
244
+    }
245
+}