导包
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.6</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>
HttpClient工具类
package com.njsc.credit.util;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
public class HttpClientUtil {
public static String doGet(String url, Map<String, String> param) {
CloseableHttpClient httpclient = HttpClients.createDefault();
String resultString = "";
CloseableHttpResponse response = null;
try {
URIBuilder builder = new URIBuilder(url);
if (param != null) {
for (String key : param.keySet()) {
builder.addParameter(key, param.get(key));
}
}
URI uri = builder.build();
HttpGet httpGet = new HttpGet(uri);
response = httpclient.execute(httpGet);
if (response.getStatusLine().getStatusCode() == 200) {
resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (response != null) {
response.close();
}
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return resultString;
}
public static String doGet(String url) {
return doGet(url, null);
}
public static String doPost(String url, Map<String, String> param) {
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String resultString = "";
try {
HttpPost httpPost = new HttpPost(url);
if (param != null) {
List<NameValuePair> paramList = new ArrayList<>();
for (String key : param.keySet()) {
paramList.add(new BasicNameValuePair(key, param.get(key)));
}
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList);
httpPost.setEntity(entity);
}
response = httpClient.execute(httpPost);
resultString = EntityUtils.toString(response.getEntity(), "utf-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return resultString;
}
public static String doPost(String url) {
return doPost(url, null);
}
public static String doPostJson(String url, String json) {
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String resultString = "";
try {
HttpPost httpPost = new HttpPost(url);
StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
httpPost.setEntity(entity);
response = httpClient.execute(httpPost);
resultString = EntityUtils.toString(response.getEntity(), "utf-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return resultString;
}
}
json工具类
package com.eqianxian.commons.utils.json;
import java.util.List;
import java.util.Map;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.PropertyFilter;
import com.alibaba.fastjson.serializer.SerializerFeature;
public class JsonUtils {
public static final int TYPE_FASTJSON = 0;
public static final int TYPE_GSON = 1;
public static final String toJson(final Object obj) {
return JSON.toJSONString(obj);
}
public static final String toJson(final Object obj, SerializerFeature... features) {
return JSON.toJSONString(obj, features);
}
public static final String toJson(final Object obj, final boolean format) {
return JSON.toJSONString(obj, format);
}
public static final String toJson(final Object obj, final String[] fields, final boolean ignore,
SerializerFeature... features) {
if (fields == null || fields.length < 1) {
return toJson(obj);
}
if (features == null)
features = new SerializerFeature[] { SerializerFeature.QuoteFieldNames };
return JSON.toJSONString(obj, new PropertyFilter() {
@Override
public boolean apply(Object object, String name, Object value) {
for (int i = 0; i < fields.length; i++) {
if (name.equals(fields[i])) {
return !ignore;
}
}
return ignore;
}
}, features);
}
@SuppressWarnings("unchecked")
public static final <E> E parse(final String json, final String path) {
String[] keys = path.split(",");
JSONObject obj = JSON.parseObject(json);
for (int i = 0; i < keys.length - 1; i++) {
obj = obj.getJSONObject(keys[i]);
}
return (E) obj.get(keys[keys.length - 1]);
}
public static final <T> T parse(final String json, final Class<T> clazz) {
return JSON.parseObject(json, clazz);
}
public static final <T> T parse(final String json, final String path, final Class<T> clazz) {
String[] keys = path.split(",");
JSONObject obj = JSON.parseObject(json);
for (int i = 0; i < keys.length - 1; i++) {
obj = obj.getJSONObject(keys[i]);
}
String inner = obj.getString(keys[keys.length - 1]);
return parse(inner, clazz);
}
public static final <T> List<T> parseArray(final Object obj, final String[] fields, boolean ignore,
final Class<T> clazz, final SerializerFeature... features) {
String json = toJson(obj, fields, ignore, features);
return parseArray(json, clazz);
}
public static final <T> List<T> parseArray(final String json, final Class<T> clazz) {
return JSON.parseArray(json, clazz);
}
public static final <T> List<T> parseArray(final String json, final String path, final Class<T> clazz) {
String[] keys = path.split(",");
JSONObject obj = JSON.parseObject(json);
for (int i = 0; i < keys.length - 1; i++) {
obj = obj.getJSONObject(keys[i]);
}
String inner = obj.getString(keys[keys.length - 1]);
List<T> ret = parseArray(inner, clazz);
return ret;
}
public static final String correctJson(final String invalidJson) {
String content = invalidJson.replace("\\\"", "\"").replace("\"{", "{").replace("}\"", "}");
return content;
}
public static final String formatJson(String json) {
Map<?, ?> map = (Map<?, ?>) JSON.parse(json);
return JSON.toJSONString(map, true);
}
public static final String getSubJson(String json, String path) {
String[] keys = path.split(",");
JSONObject obj = JSON.parseObject(json);
for (int i = 0; i < keys.length - 1; i++) {
obj = obj.getJSONObject(keys[i]);
System.out.println(obj.toJSONString());
}
return obj != null ? obj.getString(keys[keys.length - 1]) : null;
}
}