|
|
@@ -32,8 +32,24 @@ import java.net.URLEncoder;
|
|
|
import java.util.*;
|
|
|
import java.util.Map.Entry;
|
|
|
|
|
|
+/**
|
|
|
+ * HTTP请求
|
|
|
+ *
|
|
|
+ * @author hejq
|
|
|
+ * @date 2018-06-28 18:00
|
|
|
+ */
|
|
|
public class HttpUtils {
|
|
|
|
|
|
+ /**
|
|
|
+ * ?标签
|
|
|
+ */
|
|
|
+ private static String QUOTATION_MARK = "?";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * &标签
|
|
|
+ */
|
|
|
+ private static String AND_MARK = "&";
|
|
|
+
|
|
|
/**
|
|
|
* 发起POST、PUT请求
|
|
|
*
|
|
|
@@ -48,10 +64,11 @@ public class HttpUtils {
|
|
|
try {
|
|
|
StringBuilder buf = new StringBuilder(url);
|
|
|
if (params != null && !params.isEmpty()) {
|
|
|
- if (url.indexOf("?") == -1)
|
|
|
- buf.append("?");
|
|
|
- else if (!url.endsWith("&"))
|
|
|
- buf.append("&");
|
|
|
+ if (url.indexOf(QUOTATION_MARK) == -1) {
|
|
|
+ buf.append(QUOTATION_MARK);
|
|
|
+ } else if (!url.endsWith(AND_MARK)) {
|
|
|
+ buf.append(AND_MARK);
|
|
|
+ }
|
|
|
Set<Entry<String, Object>> entrys = params.entrySet();
|
|
|
for (Entry<String, Object> entry : entrys) {
|
|
|
buf.append(entry.getKey()).append("=").append(URLEncoder.encode(String.valueOf(entry.getValue()), "UTF-8")).append("&");
|
|
|
@@ -107,6 +124,38 @@ public class HttpUtils {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 发起POST、PUT请求
|
|
|
+ *
|
|
|
+ * @param datas
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static Response doPost(String url, Collection<?> datas) throws Exception {
|
|
|
+ CloseableHttpClient httpClient = HttpClients.createDefault();
|
|
|
+ HttpEntityEnclosingRequestBase request = new HttpPost(url);
|
|
|
+ CloseableHttpResponse response = null;
|
|
|
+ try {
|
|
|
+ if (datas != null && !datas.isEmpty()) {
|
|
|
+ request.setEntity(new StringEntity(FlexJsonUtils.toJson(datas), ContentType.create("application/json", Consts.UTF_8)));
|
|
|
+ }
|
|
|
+ response = httpClient.execute(request);
|
|
|
+ return Response.getResponse(response);
|
|
|
+ } finally {
|
|
|
+ request.releaseConnection();
|
|
|
+ try {
|
|
|
+ httpClient.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ }
|
|
|
+ if (response != null) {
|
|
|
+ try {
|
|
|
+ response.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 发起POST、PUT请求
|
|
|
*
|
|
|
@@ -285,7 +334,7 @@ public class HttpUtils {
|
|
|
httpPost.setEntity(reqEntity);
|
|
|
response = httpClient.execute(httpPost);
|
|
|
Response res = Response.getResponse(response);
|
|
|
- if(res.getStatusCode() == 200) {
|
|
|
+ if (res.getStatusCode() == HttpStatus.SC_OK) {
|
|
|
JSONObject obj = JSONObject.parseObject(res.getResponseText());
|
|
|
path = (String) obj.get("path");
|
|
|
}
|
|
|
@@ -352,8 +401,9 @@ public class HttpUtils {
|
|
|
}
|
|
|
|
|
|
public static Response getResponse(HttpResponse response) throws IllegalStateException, IOException, Exception {
|
|
|
- if (response != null)
|
|
|
- return new Response(response);
|
|
|
+ if (response != null) {
|
|
|
+ return new Response(response);
|
|
|
+ }
|
|
|
return null;
|
|
|
}
|
|
|
}
|