|
@@ -1,6 +1,11 @@
|
|
package cn.com.qmth.scancloud.tools.utils;
|
|
package cn.com.qmth.scancloud.tools.utils;
|
|
|
|
|
|
import okhttp3.*;
|
|
import okhttp3.*;
|
|
|
|
+
|
|
|
|
+import java.io.File;
|
|
|
|
+import java.util.Map;
|
|
|
|
+import java.util.Map.Entry;
|
|
|
|
+
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
@@ -16,13 +21,43 @@ public class HttpHelper {
|
|
}
|
|
}
|
|
|
|
|
|
RequestBody requestBody = FormBody.create(json != null ? json : "", MediaType.parse(CONTENT_TYPE));
|
|
RequestBody requestBody = FormBody.create(json != null ? json : "", MediaType.parse(CONTENT_TYPE));
|
|
- Request request = new Request.Builder()
|
|
|
|
- .post(requestBody)
|
|
|
|
- .url(url)
|
|
|
|
- .build();
|
|
|
|
|
|
+ Request request = new Request.Builder().post(requestBody).url(url).build();
|
|
|
|
+
|
|
|
|
+ try (Response response = HttpClientBuilder.getClient().newCall(request).execute();
|
|
|
|
+ ResponseBody body = response.body();) {
|
|
|
|
+ String bodyStr = body.string();
|
|
|
|
+ if (response.isSuccessful()) {
|
|
|
|
+ return bodyStr;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ log.error("[POST] {}, response code = {}, body = {}", url, response.code(), bodyStr);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error("[POST] {}, error = {}", url, e.getMessage(), e);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ throw new StatusException("接口调用失败!");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @SuppressWarnings("deprecation")
|
|
|
|
+ public static String post(String url, Map<String, String> params, File file) {
|
|
|
|
+
|
|
|
|
+ okhttp3.MultipartBody.Builder multipartBodyBuilder = new MultipartBody.Builder()
|
|
|
|
+ .setType(MultipartBody.ALTERNATIVE);
|
|
|
|
+
|
|
|
|
+ if (null != params) {
|
|
|
|
+ for (Entry<String, String> entry : params.entrySet()) {
|
|
|
|
+ multipartBodyBuilder.addFormDataPart(entry.getKey(), entry.getValue());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ MediaType type = MediaType.parse("application/octet-stream");
|
|
|
|
+ RequestBody fileBody = RequestBody.create(type, file);
|
|
|
|
+ multipartBodyBuilder.addFormDataPart("file", file.getName(), fileBody);
|
|
|
|
+
|
|
|
|
+ Request request = new Request.Builder().url(url).post(multipartBodyBuilder.build()).build();
|
|
|
|
|
|
try (Response response = HttpClientBuilder.getClient().newCall(request).execute();
|
|
try (Response response = HttpClientBuilder.getClient().newCall(request).execute();
|
|
- ResponseBody body = response.body();) {
|
|
|
|
|
|
+ ResponseBody body = response.body();) {
|
|
String bodyStr = body.string();
|
|
String bodyStr = body.string();
|
|
if (response.isSuccessful()) {
|
|
if (response.isSuccessful()) {
|
|
return bodyStr;
|
|
return bodyStr;
|