Http请求工具类
```java
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;
import org.springframework.util.Assert;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* http请求工具类
*
* @author Losisco
*/
public class HttpBuilder {
private String url;
private static volatile HttpBuilder instance;
private Map<String, String> headers;
private String body;
private HttpBuilder() {
}
synchronized public static HttpBuilder getInstance() {
if (instance == null) {
synchronized (HttpBuilder.class) {
if (instance == null) {
instance = new HttpBuilder();
}
}
}
return instance;
}
public HttpBuilder baseUrl(String baseUrl) {
this.url = baseUrl;
return this;
}
public HttpBuilder restParams(String... restParams) {
this.url = UrlBuilder.buildRestUrl(url, restParams);
return this;
}
public HttpBuilder urlParam(String key, String value) {
Map<String, String> param = new HashMap<>(1);
param.put(key, value);
this.url = UrlBuilder.buildUrl(url, param);
return this;
}
public HttpBuilder urlParams(Map<String, String> params) {
this.url = UrlBuilder.buildUrl(url, params);
return this;
}
public HttpBuilder body(String body) {
this.body = body;
return this;
}
public HttpBuilder body(Map<String, ?> body) {
this.body = JSON.toJSONString(body, SerializerFeature.DisableCircularReferenceDetect);
return this;
}
public HttpBuilder body(JSONObject body) {
this.body = JSON.toJSONString(body, SerializerFeature.DisableCircularReferenceDetect);
return this;
}
public HttpBuilder header(String key, String value) {
if (headers == null) {
headers = new HashMap<>(1);
}
headers.put(key, value);
return this;
}
public HttpBuilder headers(Map<String, String> headers) {
Assert.isTrue(headers != null && !headers.isEmpty(), "headers空");
if (this.headers == null) {
this.headers = new HashMap<>(headers.size());
}
this.headers.putAll(headers);
return this;
}
public String get() {
HttpClient client = HttpClientBuilder.create().build();
HttpGet httpGet = new HttpGet(url);
if (headers != null) {
httpGet.setHeaders(buildHeaders(headers));
}
HttpResponse response = null;
try {
response = client.execute(httpGet);
if (isSuccess(response)) {
return EntityUtils.toString(response.getEntity());
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public String post() {
HttpClient client = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost(url);
if (headers != null) {
httpPost.setHeaders(buildHeaders(headers));
}
ContentType contentType = ContentType.create(ContentType.APPLICATION_JSON.getMimeType(), "UTF-8");
if (StringUtil.isNotEmpty(body)) {
StringEntity body = new StringEntity(this.body, contentType);
httpPost.setEntity(body);
}
HttpResponse response = null;
try {
response = client.execute(httpPost);
if (isSuccess(response)) {
return EntityUtils.toString(response.getEntity());
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private boolean isSuccess(HttpResponse response) {
return response != null && response.getEntity() != null;
}
private Header[] buildHeaders(Map<String, String> headers) {
if (headers != null && !headers.isEmpty()) {
Header[] arr = new Header[headers.size()];
int index = 0;
for (String key : headers.keySet()) {
String value = headers.get(key);
arr[index] = new BasicHeader(key, value);
index++;
}
return arr;
}
return null;
}
}
```
Url处理类
```java
import java.util.Map;
/**
* Url拼接工具类
*
* @author Losisco
*/
public class UrlBuilder {
private UrlBuilder() {
}
public static String buildUrl(String url, Map<String, String> params) {
StringBuilder sb = new StringBuilder(url);
int len = params.size();
int index = 0;
if (!params.isEmpty()) {
sb.append("?");
for (String key : params.keySet()) {
String value = params.get(key);
if (StringUtil.isNotEmpty(value)) {
if (index == (len - 1)) {
sb.append(key).append("=").append(value);
} else {
sb.append(key).append("=").append(value).append("&");
}
}
index++;
}
}
return sb.toString();
}
public static String buildRestUrl(String url, String... params) {
StringBuilder sb = new StringBuilder(url);
int len = params.length;
if (len > 0) {
String flag = "/";
sb.append(flag);
for (int i = 0; i < len; i++) {
if (i == (len - 1)) {
sb.append(params[i]);
} else {
sb.append(params[i]).append(flag);
}
}
}
return sb.toString();
}
}
```