|
|
@@ -0,0 +1,308 @@
|
|
|
+package com.kingdee.eas.custom.beisen.utils;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONException;
|
|
|
+import org.apache.http.HttpResponse;
|
|
|
+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.client.methods.HttpPut;
|
|
|
+import org.apache.http.client.utils.URIBuilder;
|
|
|
+import org.apache.http.entity.StringEntity;
|
|
|
+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;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.net.HttpURLConnection;
|
|
|
+import java.net.URISyntaxException;
|
|
|
+import java.net.URL;
|
|
|
+import java.util.*;
|
|
|
+import java.util.Map.Entry;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 提供一些常用的HTTP请求和文件下载的辅助方法。
|
|
|
+ */
|
|
|
+public class Helper {
|
|
|
+ /**
|
|
|
+ * 发送HTTP请求并返回JSON格式响应。
|
|
|
+ *
|
|
|
+ * @param url 请求的URL地址
|
|
|
+ * @param header 请求头信息,键值对形式
|
|
|
+ * @param requestBody 请求体,以JSONObject形式表示
|
|
|
+ * @param method 请求方法,支持"GET"、"POST"、"PUT"
|
|
|
+ * @return 服务器响应的JSON对象
|
|
|
+ * @throws URISyntaxException 如果URL格式不正确
|
|
|
+ * @throws JSONException 如果JSON解析出错
|
|
|
+ * @throws IOException 如果发生I/O错误
|
|
|
+ * @throws ClientProtocolException 如果HTTP协议出错
|
|
|
+ */
|
|
|
+ public static JSONObject getURL(String url, Map<String, String> header, JSONObject requestBody, String method)
|
|
|
+ throws URISyntaxException, JSONException, ClientProtocolException, IOException {
|
|
|
+ // 用于存储服务器响应的JSON对象
|
|
|
+ JSONObject responseJson = new JSONObject();
|
|
|
+ HttpResponse response;
|
|
|
+ // 创建一个默认的HTTP客户端
|
|
|
+ CloseableHttpClient httpClient = HttpClients.createDefault();
|
|
|
+
|
|
|
+ if ("GET".equals(method)) {
|
|
|
+ // 创建一个URIBuilder对象,用于构建包含请求参数的URL
|
|
|
+ URIBuilder uriBuilder = new URIBuilder(url);
|
|
|
+ // 获取请求体中的所有键
|
|
|
+ Set<String> strings = requestBody.keySet();
|
|
|
+ // 遍历键值对,将其添加到URL的查询参数中
|
|
|
+ for (String string : strings) {
|
|
|
+ String key = string;
|
|
|
+ String value = requestBody.getString(key);
|
|
|
+ uriBuilder.addParameter(key, value);
|
|
|
+ }
|
|
|
+ // 创建一个HTTP GET请求
|
|
|
+ HttpGet httpGet = new HttpGet(uriBuilder.build());
|
|
|
+ // 设置请求头信息
|
|
|
+ for (Entry<String, String> entry : header.entrySet()) {
|
|
|
+ httpGet.addHeader(entry.getKey(), entry.getValue());
|
|
|
+ }
|
|
|
+ // 执行HTTP GET请求
|
|
|
+ response = httpClient.execute(httpGet);
|
|
|
+ } else if ("POST".equals(method)) {
|
|
|
+ // 创建一个HTTP POST请求
|
|
|
+ HttpPost httpPost = new HttpPost(url);
|
|
|
+ // 设置请求头信息
|
|
|
+ for (Entry<String, String> entry : header.entrySet()) {
|
|
|
+ httpPost.addHeader(entry.getKey(), entry.getValue());
|
|
|
+ }
|
|
|
+ // 创建一个StringEntity对象,将请求体以UTF-8编码包装
|
|
|
+ StringEntity requestEntity = new StringEntity(requestBody.toString(), "UTF-8");
|
|
|
+ httpPost.setEntity(requestEntity);
|
|
|
+ // 执行HTTP POST请求
|
|
|
+ response = httpClient.execute(httpPost);
|
|
|
+ } else if ("PUT".equals(method)) {
|
|
|
+ // 创建一个HTTP PUT请求
|
|
|
+ HttpPut httpPut = new HttpPut(url);
|
|
|
+ // 设置请求头信息
|
|
|
+ for (Entry<String, String> entry : header.entrySet()) {
|
|
|
+ httpPut.addHeader(entry.getKey(), entry.getValue());
|
|
|
+ }
|
|
|
+ // 创建一个StringEntity对象,将请求体以UTF-8编码包装
|
|
|
+ StringEntity requestEntity = new StringEntity(requestBody.toString(), "UTF-8");
|
|
|
+ httpPut.setEntity(requestEntity);
|
|
|
+ // 执行HTTP PUT请求
|
|
|
+ response = httpClient.execute(httpPut);
|
|
|
+ } else {
|
|
|
+ throw new IllegalArgumentException("Unsupported HTTP method: " + method);
|
|
|
+ }
|
|
|
+ // 将响应实体转换为字符串
|
|
|
+ String responseBody = EntityUtils.toString(response.getEntity(), "UTF-8");
|
|
|
+ // 将响应字符串解析为JSON对象
|
|
|
+ responseJson = JSONObject.parseObject(responseBody);
|
|
|
+ // 关闭HTTP客户端
|
|
|
+ httpClient.close();
|
|
|
+ return responseJson;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送HTTP请求并返回JSON格式响应,请求体为JSONArray。
|
|
|
+ *
|
|
|
+ * @param url 请求的URL地址
|
|
|
+ * @param header 请求头信息,键值对形式
|
|
|
+ * @param requestBody 请求体,以JSONArray形式表示
|
|
|
+ * @param method 请求方法,支持"GET"、"POST"、"PUT"
|
|
|
+ * @return 服务器响应的JSON对象
|
|
|
+ * @throws URISyntaxException 如果URL格式不正确
|
|
|
+ * @throws JSONException 如果JSON解析出错
|
|
|
+ * @throws IOException 如果发生I/O错误
|
|
|
+ * @throws ClientProtocolException 如果HTTP协议出错
|
|
|
+ */
|
|
|
+ public static JSONObject getURL(String url, Map<String, String> header, JSONArray requestBody, String method)
|
|
|
+ throws URISyntaxException, JSONException, ClientProtocolException, IOException {
|
|
|
+ // 用于存储服务器响应的JSON对象
|
|
|
+ JSONObject responseJson = new JSONObject();
|
|
|
+ HttpResponse response;
|
|
|
+ // 创建一个默认的HTTP客户端
|
|
|
+ CloseableHttpClient httpClient = HttpClients.createDefault();
|
|
|
+
|
|
|
+ if ("GET".equals(method)) {
|
|
|
+ // 创建一个URIBuilder对象,用于构建包含请求参数的URL
|
|
|
+ URIBuilder uriBuilder = new URIBuilder(url);
|
|
|
+ // 创建一个列表,用于存储请求参数
|
|
|
+ List paramList = new ArrayList();
|
|
|
+ // 将JSONArray中的元素添加到参数列表中
|
|
|
+ for (int i = 0; i < requestBody.size(); i++) {
|
|
|
+ paramList.add(requestBody.get(i));
|
|
|
+ }
|
|
|
+ // 将参数列表添加到URL的查询参数中
|
|
|
+ uriBuilder.addParameters(paramList);
|
|
|
+ // 创建一个HTTP GET请求
|
|
|
+ HttpGet httpGet = new HttpGet(uriBuilder.build());
|
|
|
+ // 设置请求头信息
|
|
|
+ for (Entry<String, String> entry : header.entrySet()) {
|
|
|
+ httpGet.addHeader(entry.getKey(), entry.getValue());
|
|
|
+ }
|
|
|
+ // 执行HTTP GET请求
|
|
|
+ response = httpClient.execute(httpGet);
|
|
|
+ } else if ("POST".equals(method)) {
|
|
|
+ // 创建一个HTTP POST请求
|
|
|
+ HttpPost httpPost = new HttpPost(url);
|
|
|
+ // 设置请求头信息
|
|
|
+ for (Entry<String, String> entry : header.entrySet()) {
|
|
|
+ httpPost.addHeader(entry.getKey(), entry.getValue());
|
|
|
+ }
|
|
|
+ // 创建一个StringEntity对象,将请求体以UTF-8编码包装
|
|
|
+ StringEntity requestEntity = new StringEntity(requestBody.toString(), "UTF-8");
|
|
|
+ httpPost.setEntity(requestEntity);
|
|
|
+ // 执行HTTP POST请求
|
|
|
+ response = httpClient.execute(httpPost);
|
|
|
+ } else if ("PUT".equals(method)) {
|
|
|
+ // 创建一个HTTP PUT请求
|
|
|
+ HttpPut httpPut = new HttpPut(url);
|
|
|
+ // 设置请求头信息
|
|
|
+ for (Entry<String, String> entry : header.entrySet()) {
|
|
|
+ httpPut.addHeader(entry.getKey(), entry.getValue());
|
|
|
+ }
|
|
|
+ // 创建一个StringEntity对象,将请求体以UTF-8编码包装
|
|
|
+ StringEntity requestEntity = new StringEntity(requestBody.toString(), "UTF-8");
|
|
|
+ httpPut.setEntity(requestEntity);
|
|
|
+ // 执行HTTP PUT请求
|
|
|
+ response = httpClient.execute(httpPut);
|
|
|
+ } else {
|
|
|
+ throw new IllegalArgumentException("Unsupported HTTP method: " + method);
|
|
|
+ }
|
|
|
+ // 将响应实体转换为字符串
|
|
|
+ String responseBody = EntityUtils.toString(response.getEntity(), "UTF-8");
|
|
|
+ // 将响应字符串解析为JSON对象
|
|
|
+ responseJson = JSONObject.parseObject(responseBody);
|
|
|
+ // 关闭HTTP客户端
|
|
|
+ httpClient.close();
|
|
|
+ return responseJson;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送编码后的HTTP请求并返回JSON格式响应。
|
|
|
+ *
|
|
|
+ * @param url 请求的URL地址
|
|
|
+ * @param header 请求头信息,键值对形式
|
|
|
+ * @param requestBody 请求体,以JSONObject形式表示
|
|
|
+ * @param method 请求方法,支持"GET"、"POST"、"PUT"
|
|
|
+ * @return 服务器响应的JSON对象
|
|
|
+ * @throws URISyntaxException 如果URL格式不正确
|
|
|
+ * @throws JSONException 如果JSON解析出错
|
|
|
+ * @throws IOException 如果发生I/O错误
|
|
|
+ * @throws ClientProtocolException 如果HTTP协议出错
|
|
|
+ */
|
|
|
+ public static JSONObject getURLEncoded(String url, Map<String, String> header, JSONObject requestBody, String method)
|
|
|
+ throws URISyntaxException, JSONException, ClientProtocolException, IOException {
|
|
|
+ // 用于存储服务器响应的JSON对象
|
|
|
+ JSONObject responseJson = new JSONObject();
|
|
|
+ HttpResponse response;
|
|
|
+ // 创建一个默认的HTTP客户端
|
|
|
+ CloseableHttpClient httpClient = HttpClients.createDefault();
|
|
|
+
|
|
|
+ if ("GET".equals(method)) {
|
|
|
+ // 创建一个URIBuilder对象,用于构建包含请求参数的URL
|
|
|
+ URIBuilder uriBuilder = new URIBuilder(url);
|
|
|
+ // 创建一个HTTP GET请求
|
|
|
+ HttpGet httpGet = new HttpGet(uriBuilder.build());
|
|
|
+ // 设置请求头信息
|
|
|
+ for (Entry<String, String> entry : header.entrySet()) {
|
|
|
+ httpGet.addHeader(entry.getKey(), entry.getValue());
|
|
|
+ }
|
|
|
+ // 获取请求体中的所有键
|
|
|
+ Set<String> strings = requestBody.keySet();
|
|
|
+ // 遍历键值对,将其添加到URL的查询参数中
|
|
|
+ for (String string : strings) {
|
|
|
+ String key = string;
|
|
|
+ String value = requestBody.getString(key);
|
|
|
+ uriBuilder.addParameter(key, value);
|
|
|
+ }
|
|
|
+ // 执行HTTP GET请求
|
|
|
+ response = httpClient.execute(httpGet);
|
|
|
+ } else if ("POST".equals(method)) {
|
|
|
+ // 创建一个HTTP POST请求
|
|
|
+ HttpPost httpPost = new HttpPost(url);
|
|
|
+ // 设置请求头信息
|
|
|
+ for (Entry<String, String> entry : header.entrySet()) {
|
|
|
+ httpPost.addHeader(entry.getKey(), entry.getValue());
|
|
|
+ }
|
|
|
+ // 创建一个列表,用于存储请求参数
|
|
|
+ ArrayList<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
|
|
|
+ // 获取请求体中的所有键
|
|
|
+ Set<String> strings = requestBody.keySet();
|
|
|
+ // 遍历键值对,将其添加到参数列表中
|
|
|
+ for (String string : strings) {
|
|
|
+ String key = (String) string;
|
|
|
+ String value = requestBody.getString(key);
|
|
|
+ list.add(new BasicNameValuePair(key, value));
|
|
|
+ }
|
|
|
+ // 创建一个UrlEncodedFormEntity对象,将参数列表以UTF-8编码包装
|
|
|
+ httpPost.setEntity(new UrlEncodedFormEntity(list, "UTF-8"));
|
|
|
+ // 执行HTTP POST请求
|
|
|
+ response = httpClient.execute(httpPost);
|
|
|
+ } else if ("PUT".equals(method)) {
|
|
|
+ // 创建一个HTTP PUT请求
|
|
|
+ HttpPut httpPut = new HttpPut(url);
|
|
|
+ // 设置请求头信息
|
|
|
+ for (Entry<String, String> entry : header.entrySet()) {
|
|
|
+ httpPut.addHeader(entry.getKey(), entry.getValue());
|
|
|
+ }
|
|
|
+ // 创建一个列表,用于存储请求参数
|
|
|
+ ArrayList<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
|
|
|
+ // 获取请求体中的所有键
|
|
|
+ Set<String> strings = requestBody.keySet();
|
|
|
+ // 遍历键值对,将其添加到参数列表中
|
|
|
+ for (String string : strings) {
|
|
|
+ String key = (String) string;
|
|
|
+ String value = requestBody.getString(key);
|
|
|
+ list.add(new BasicNameValuePair(key, value));
|
|
|
+ }
|
|
|
+ // 创建一个UrlEncodedFormEntity对象,将参数列表以UTF-8编码包装
|
|
|
+ httpPut.setEntity(new UrlEncodedFormEntity(list, "UTF-8"));
|
|
|
+ // 执行HTTP PUT请求
|
|
|
+ response = httpClient.execute(httpPut);
|
|
|
+ } else {
|
|
|
+ throw new IllegalArgumentException("Unsupported HTTP method: " + method);
|
|
|
+ }
|
|
|
+ // 将响应实体转换为字符串
|
|
|
+ String responseBody = EntityUtils.toString(response.getEntity(), "UTF-8");
|
|
|
+ // 将响应字符串解析为JSON对象
|
|
|
+ responseJson = JSONObject.parseObject(responseBody);
|
|
|
+ // 关闭HTTP客户端
|
|
|
+ httpClient.close();
|
|
|
+ return responseJson;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通过网络URL获取文件的字节数组。
|
|
|
+ *
|
|
|
+ * @param urlStr 文件的网络URL地址
|
|
|
+ * @return 文件的字节数组
|
|
|
+ * @throws IOException 如果发生I/O错误
|
|
|
+ */
|
|
|
+ public static byte[] getBytesByNetURL(String urlStr) throws IOException {
|
|
|
+ // 创建一个URL对象
|
|
|
+ URL url = new URL(urlStr);
|
|
|
+ // 创建一个HTTP连接
|
|
|
+ HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
|
|
+ // 设置连接超时时间为5秒
|
|
|
+ conn.setConnectTimeout(5 * 1000);
|
|
|
+ // 获取输入流,用于读取文件内容
|
|
|
+ InputStream in = conn.getInputStream();
|
|
|
+ // 创建一个ByteArrayOutputStream对象,用于存储文件内容
|
|
|
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
|
+ byte[] buffer = new byte[1024];
|
|
|
+ int len;
|
|
|
+ // 循环读取文件内容,并写入到ByteArrayOutputStream中
|
|
|
+ while ((len = in.read(buffer)) != -1) {
|
|
|
+ outputStream.write(buffer, 0, len);
|
|
|
+ }
|
|
|
+ // 关闭输入流
|
|
|
+ in.close();
|
|
|
+ // 返回文件的字节数组
|
|
|
+ return outputStream.toByteArray();
|
|
|
+ }
|
|
|
+}
|