|
@@ -7,11 +7,15 @@ import org.apache.http.Consts;
|
|
|
import org.apache.http.HttpEntity;
|
|
|
import org.apache.http.HttpResponse;
|
|
|
import org.apache.http.client.HttpClient;
|
|
|
+import org.apache.http.client.config.RequestConfig;
|
|
|
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.HttpUriRequest;
|
|
|
+import org.apache.http.entity.ContentType;
|
|
|
import org.apache.http.entity.StringEntity;
|
|
|
+import org.apache.http.entity.mime.HttpMultipartMode;
|
|
|
+import org.apache.http.entity.mime.MultipartEntityBuilder;
|
|
|
import org.apache.http.impl.client.HttpClients;
|
|
|
import org.apache.http.message.BasicHeader;
|
|
|
import org.apache.http.message.BasicNameValuePair;
|
|
@@ -71,33 +75,6 @@ public class HttpUtil {
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * post json
|
|
|
- *
|
|
|
- * @param url
|
|
|
- * @param json
|
|
|
- * @param secret
|
|
|
- * @param timestamp
|
|
|
- * @return
|
|
|
- * @throws IOException
|
|
|
- */
|
|
|
- public static String postJson(String url, String json, String secret, Long timestamp) throws IOException {
|
|
|
- // 构建post请求
|
|
|
- HttpPost post = new HttpPost(url);
|
|
|
- post.setHeader("authorization-token", secret);
|
|
|
- post.setHeader(SystemConstant.HEADER_TIME, String.valueOf(timestamp));
|
|
|
- post.setHeader(HTTP.CONTENT_TYPE, "application/json; charset=utf-8");
|
|
|
- post.setHeader("Accept", "application/json");
|
|
|
-
|
|
|
- String encoderJson = URLEncoder.encode(json, SystemConstant.CHARSET_NAME);
|
|
|
- StringEntity se = new StringEntity(encoderJson);
|
|
|
- se.setContentType("text/json");
|
|
|
- se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
|
|
|
- post.setEntity(se);
|
|
|
- // 执行请求,获取响应
|
|
|
- return getRespString(post);
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* post 请求
|
|
|
*
|
|
@@ -252,4 +229,78 @@ public class HttpUtil {
|
|
|
}
|
|
|
return file;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * post file
|
|
|
+ *
|
|
|
+ * @param url
|
|
|
+ * @param params
|
|
|
+ * @param secret
|
|
|
+ * @param timestamp
|
|
|
+ * @return
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public static String postFile(String url, Map<String, Object> params, String secret, Long timestamp) throws IOException {
|
|
|
+ RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(SystemConstant.CONNECT_TIME_OUT)// 连接主机服务超时时间
|
|
|
+ .setConnectionRequestTimeout(SystemConstant.CONNECT_TIME_OUT)// 请求超时时间
|
|
|
+ .setSocketTimeout(SystemConstant.SOCKET_CONNECT_TIME_OUT)// 数据读取超时时间
|
|
|
+ .build();
|
|
|
+ HttpPost post = new HttpPost(url);
|
|
|
+ post.setHeader(SystemConstant.HEADER_AUTHORIZATION, secret);
|
|
|
+ post.setHeader(SystemConstant.HEADER_TIME, String.valueOf(timestamp));
|
|
|
+
|
|
|
+ MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
|
|
|
+ // 解决中文文件名乱码问题
|
|
|
+ entityBuilder.setMode(HttpMultipartMode.RFC6532);
|
|
|
+ entityBuilder.setCharset(Consts.UTF_8);
|
|
|
+ ContentType contentType = ContentType.create(ContentType.TEXT_PLAIN.getMimeType(), Consts.UTF_8);
|
|
|
+
|
|
|
+ // 构建请求参数
|
|
|
+ if (params != null) {
|
|
|
+ for (String key : params.keySet()) {
|
|
|
+ if (key.contains("file")) {
|
|
|
+ File file = new File((String) params.get(key));
|
|
|
+ entityBuilder.addBinaryBody(key, file, ContentType.DEFAULT_BINARY, file.getName());
|
|
|
+ } else {
|
|
|
+ entityBuilder.addTextBody(key, String.valueOf(params.get(key)), contentType);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ post.setEntity(entityBuilder.build());
|
|
|
+ post.setConfig(requestConfig);
|
|
|
+ return getRespString(post);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * post json
|
|
|
+ *
|
|
|
+ * @param url
|
|
|
+ * @param json
|
|
|
+ * @param secret
|
|
|
+ * @param timestamp
|
|
|
+ * @return
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public static String postJson(String url, String json, String secret, Long timestamp) throws IOException {
|
|
|
+ RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(SystemConstant.CONNECT_TIME_OUT)// 连接主机服务超时时间
|
|
|
+ .setConnectionRequestTimeout(SystemConstant.CONNECT_TIME_OUT)// 请求超时时间
|
|
|
+ .setSocketTimeout(SystemConstant.SOCKET_CONNECT_TIME_OUT)// 数据读取超时时间
|
|
|
+ .build();
|
|
|
+
|
|
|
+ // 构建post请求
|
|
|
+ HttpPost post = new HttpPost(url);
|
|
|
+ post.setConfig(requestConfig);
|
|
|
+ post.setHeader(SystemConstant.HEADER_AUTHORIZATION, secret);
|
|
|
+ post.setHeader(SystemConstant.HEADER_TIME, String.valueOf(timestamp));
|
|
|
+ post.setHeader(HTTP.CONTENT_TYPE, "application/json; charset=utf-8");
|
|
|
+ post.setHeader("Accept", "application/json");
|
|
|
+
|
|
|
+ String encoderJson = URLEncoder.encode(json, SystemConstant.CHARSET_NAME);
|
|
|
+ StringEntity se = new StringEntity(encoderJson);
|
|
|
+ se.setContentType("text/json");
|
|
|
+ se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
|
|
|
+ post.setEntity(se);
|
|
|
+ // 执行请求,获取响应
|
|
|
+ return getRespString(post);
|
|
|
+ }
|
|
|
}
|