From 8304ca3fca978b09928e7aa86a6058935e0b7b32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B7=A6=E8=89=AF?= Date: Wed, 27 Apr 2022 21:57:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9pom=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E4=B8=AD=E4=BB=93=E5=BA=93=E4=BD=8D=E7=BD=AE=20=E6=96=B0?= =?UTF-8?q?=E5=A2=9Ehttpkit=E6=94=AF=E6=8C=81=E7=9B=B4=E6=8E=A5postJSON?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 4 +- .../com/fanruan/api/net/http/HttpKit.java | 60 +++++++++++-------- 2 files changed, 38 insertions(+), 26 deletions(-) diff --git a/pom.xml b/pom.xml index 099c654..8b91cff 100644 --- a/pom.xml +++ b/pom.xml @@ -176,7 +176,7 @@ fanruan fanruan - http://mvn.finedevelop.com/repository/maven-public/ + https://mvn.fanruan.com/repository/maven-public/ true always @@ -188,7 +188,7 @@ fanruan fanruan - http://mvn.finedevelop.com/repository/maven-public/ + https://mvn.fanruan.com/repository/maven-public/ true always diff --git a/src/main/java/com/fanruan/api/net/http/HttpKit.java b/src/main/java/com/fanruan/api/net/http/HttpKit.java index 6162745..57ae033 100644 --- a/src/main/java/com/fanruan/api/net/http/HttpKit.java +++ b/src/main/java/com/fanruan/api/net/http/HttpKit.java @@ -1,20 +1,12 @@ package com.fanruan.api.net.http; -import com.fanruan.api.macro.EncodeConstants; import com.fanruan.api.log.LogKit; -import com.fanruan.api.net.http.rs.BaseHttpResponseHandle; +import com.fanruan.api.macro.EncodeConstants; import com.fanruan.api.net.http.rs.HttpRequest; -import com.fanruan.api.net.http.rs.HttpRequestType; -import com.fanruan.api.net.http.rs.HttpResponseType; -import com.fanruan.api.net.http.rs.StreamResponseHandle; -import com.fanruan.api.net.http.rs.TextResponseHandle; -import com.fanruan.api.net.http.rs.UploadResponseHandle; +import com.fanruan.api.net.http.rs.*; +import com.fr.json.JSONObject; import com.fr.third.guava.collect.Maps; -import com.fr.third.org.apache.http.HttpEntity; -import com.fr.third.org.apache.http.HttpEntityEnclosingRequest; -import com.fr.third.org.apache.http.HttpHost; -import com.fr.third.org.apache.http.NameValuePair; -import com.fr.third.org.apache.http.NoHttpResponseException; +import com.fr.third.org.apache.http.*; import com.fr.third.org.apache.http.client.HttpRequestRetryHandler; import com.fr.third.org.apache.http.client.config.RequestConfig; import com.fr.third.org.apache.http.client.entity.UrlEncodedFormEntity; @@ -31,6 +23,7 @@ import com.fr.third.org.apache.http.conn.socket.LayeredConnectionSocketFactory; import com.fr.third.org.apache.http.conn.socket.PlainConnectionSocketFactory; import com.fr.third.org.apache.http.conn.ssl.SSLConnectionSocketFactory; import com.fr.third.org.apache.http.entity.FileEntity; +import com.fr.third.org.apache.http.entity.StringEntity; import com.fr.third.org.apache.http.entity.mime.HttpMultipartMode; import com.fr.third.org.apache.http.entity.mime.MultipartEntityBuilder; import com.fr.third.org.apache.http.impl.client.CloseableHttpClient; @@ -45,20 +38,13 @@ import org.jetbrains.annotations.Nullable; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLException; import javax.net.ssl.SSLHandshakeException; -import java.io.ByteArrayInputStream; -import java.io.File; -import java.io.IOException; -import java.io.InterruptedIOException; -import java.io.UnsupportedEncodingException; +import java.io.*; import java.net.URI; import java.net.URISyntaxException; import java.net.URLEncoder; import java.net.UnknownHostException; import java.nio.charset.Charset; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Map; +import java.util.*; import static com.fanruan.api.net.http.rs.HttpRequestType.POST; @@ -205,6 +191,30 @@ public class HttpKit { }); } + /** + * 发起POST JSON请求并获取返回的文本 + * + * @param url 响应请求的的服务器地址 + * @param params POST请求的参数 + * @return 服务器返回的文本内容 + */ + public static String postJSON(String url, JSONObject params, Map headers) throws IOException { + StringEntity jsonEntity = new StringEntity(params.toString(), "UTF-8"); + jsonEntity.setContentEncoding("UTF-8"); + if (headers != null) { + headers.put("Content-Type", "application/json"); + } else { + headers = new HashMap<>(); + headers.put("Content-Type", "application/json"); + } + return executeAndParse(HttpRequest + .custom() + .headers(headers) + .url(url) + .post(jsonEntity) + .build()); + } + /** * 发起POST请求并获取返回的文本 * @@ -438,6 +448,7 @@ public class HttpKit { /** * 从指定的地址下载文件 + * * @param url 文件下载地址 * @return 文件的字节流 * @throws IOException 下载过程中出现错误则抛出此异常 @@ -448,10 +459,11 @@ public class HttpKit { /** * 从指定的地址下载文件 - * @param url 文件下载地址 - * @param params 参数对 + * + * @param url 文件下载地址 + * @param params 参数对 * @param responseEncoding 响应的文件编码 - * @param headers 请求头 + * @param headers 请求头 * @return 文件的字节流 * @throws IOException 下载过程中出现错误则抛出此异常 */