http调用工具类(基于OkHttp4.x)
import manage.constant.ContentType;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import okhttp3.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Map;
public class HttpUtil {
private static final Logger LOG = LoggerFactory.getLogger(HttpUtil.class);
private static final OkHttpClient CLIENT = new OkHttpClient.Builder().build();
private static final ObjectMapper MAPPER = new ObjectMapper();
public static ResponseBody get(String url, Map<String, String> headersMap) {
if (url == null || "".equals(url.trim())) {
throw new RuntimeException("url is empty!");
}
Request.Builder builder = new Request.Builder();
if (headersMap != null && !headersMap.isEmpty()) {
builder = builder.headers(Headers.of(headersMap));
}
Request request = builder.url(url).get().build();
try {
return CLIENT.newCall(request).execute().body();
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
}
public static ResponseBody get(String url) {
return get(url, null);
}
public static String getToString(String url) {
return toJsonString(get(url, null));
}
public static ResponseBody post(String contentType, String url, Map<String, String> headersMap, Map<String, Object> params) {
MediaType mediaType;
if (contentType == null) {
mediaType = MediaType.get(ContentType.APPLICATION_JSON);
} else {
mediaType = MediaType.get(contentType);
}
RequestBody requestBody = RequestBody.create(params.toString(), mediaType);
Request.Builder builder = new Request.Builder();
if (headersMap != null && !headersMap.isEmpty()) {
builder.headers(Headers.of(headersMap));
}
Request request = builder.url(url).post(requestBody).build();
try {
return CLIENT.newCall(request).execute().body();
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
}
public static ResponseBody post(String contentType, String url, Map<String, Object> params) {
return post(contentType, url, null, params);
}
public static ResponseBody post(String url, Map<String, Object> params) {
return post(null, url, params);
}
public static String postToString(String contentType, String url, Map<String, Object> params) {
return toJsonString(post(contentType, url, null, params));
}
public static String postToString(String url, Map<String, Object> params) {
return toJsonString(post(null, url, params));
}
private static String toJsonString(Object object) {
if (object == null) {
LOG.warn("object is null");
return "{}";
}
try {
return MAPPER.writeValueAsString(object);
} catch (JsonProcessingException e) {
LOG.warn("JSON transformation error");
}
return null;
}
}
Http的content type
public class ContentType {
public static final String APPLICATION_X_WWW_FORM_URLENCODED = "application/x-www-form-urlencoded;charset=UTF-8";
public static final String APPLICATION_JSON = "application/json;charset=UTF-8";
public static final String MULTIPART_FORM_DATA = "multipart/form-data;charset=UTF-8";
public static final String TEXT_XML = "text/xml;charset=UTF-8";
}