Browse Source

修改pom文件中仓库位置

新增httpkit支持直接postJSON
master^2
左良 3 years ago
parent
commit
8304ca3fca
  1. 4
      pom.xml
  2. 54
      src/main/java/com/fanruan/api/net/http/HttpKit.java

4
pom.xml

@ -176,7 +176,7 @@
<repository> <repository>
<id>fanruan</id> <id>fanruan</id>
<name>fanruan</name> <name>fanruan</name>
<url>http://mvn.finedevelop.com/repository/maven-public/</url> <url>https://mvn.fanruan.com/repository/maven-public/</url>
<snapshots> <snapshots>
<enabled>true</enabled> <enabled>true</enabled>
<updatePolicy>always</updatePolicy> <updatePolicy>always</updatePolicy>
@ -188,7 +188,7 @@
<pluginRepository> <pluginRepository>
<id>fanruan</id> <id>fanruan</id>
<name>fanruan</name> <name>fanruan</name>
<url>http://mvn.finedevelop.com/repository/maven-public/</url> <url>https://mvn.fanruan.com/repository/maven-public/</url>
<snapshots> <snapshots>
<enabled>true</enabled> <enabled>true</enabled>
<updatePolicy>always</updatePolicy> <updatePolicy>always</updatePolicy>

54
src/main/java/com/fanruan/api/net/http/HttpKit.java

@ -1,20 +1,12 @@
package com.fanruan.api.net.http; package com.fanruan.api.net.http;
import com.fanruan.api.macro.EncodeConstants;
import com.fanruan.api.log.LogKit; 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.HttpRequest;
import com.fanruan.api.net.http.rs.HttpRequestType; import com.fanruan.api.net.http.rs.*;
import com.fanruan.api.net.http.rs.HttpResponseType; import com.fr.json.JSONObject;
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.fr.third.guava.collect.Maps; import com.fr.third.guava.collect.Maps;
import com.fr.third.org.apache.http.HttpEntity; import com.fr.third.org.apache.http.*;
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.client.HttpRequestRetryHandler; 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.config.RequestConfig;
import com.fr.third.org.apache.http.client.entity.UrlEncodedFormEntity; 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.socket.PlainConnectionSocketFactory;
import com.fr.third.org.apache.http.conn.ssl.SSLConnectionSocketFactory; 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.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.HttpMultipartMode;
import com.fr.third.org.apache.http.entity.mime.MultipartEntityBuilder; import com.fr.third.org.apache.http.entity.mime.MultipartEntityBuilder;
import com.fr.third.org.apache.http.impl.client.CloseableHttpClient; 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.SSLContext;
import javax.net.ssl.SSLException; import javax.net.ssl.SSLException;
import javax.net.ssl.SSLHandshakeException; import javax.net.ssl.SSLHandshakeException;
import java.io.ByteArrayInputStream; import java.io.*;
import java.io.File;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.io.UnsupportedEncodingException;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.ArrayList; import java.util.*;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import static com.fanruan.api.net.http.rs.HttpRequestType.POST; 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<String, String> 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请求并获取返回的文本 * 发起POST请求并获取返回的文本
* *
@ -438,6 +448,7 @@ public class HttpKit {
/** /**
* 从指定的地址下载文件 * 从指定的地址下载文件
*
* @param url 文件下载地址 * @param url 文件下载地址
* @return 文件的字节流 * @return 文件的字节流
* @throws IOException 下载过程中出现错误则抛出此异常 * @throws IOException 下载过程中出现错误则抛出此异常
@ -448,6 +459,7 @@ public class HttpKit {
/** /**
* 从指定的地址下载文件 * 从指定的地址下载文件
*
* @param url 文件下载地址 * @param url 文件下载地址
* @param params 参数对 * @param params 参数对
* @param responseEncoding 响应的文件编码 * @param responseEncoding 响应的文件编码

Loading…
Cancel
Save