import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
import java.util.Set;
@Slf4j
public class HttpUtil {
public static String doPost(String url, Map<String, Object> params) {
return doPost(url, params, null);
}
public static String doPost(String url, Map<String, Object> params, Map<String, String> httpParams) {
return doPost(url, params, httpParams, null);
}
public static String doPost(String url, Map<String, Object> params, Map<String, String> httpParams, Map<String, String> headParams) {
try {
url = setUrl(httpParams, url);
log.info("=POST=========================\n正在请求:{}", url);
URL urlObj = new URL(url);
HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection();
conn.setRequestMethod("POST");
conn.setConnectTimeout(5000);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.addRequestProperty("Content-Type", "application/json");
if (headParams != null) {
for (Map.Entry<String, String> item : headParams.entrySet()) {
conn.addRequestProperty(item.getKey(), item.getValue());
}
}
if (params != null) {
OutputStream os = conn.getOutputStream();
os.write(JSON.toJSONString(params).getBytes());
os.flush();
}
conn.connect();
int code = conn.getResponseCode();
if (code == 200) {
return receive(conn);
}
return "{\"errno\": \"" + code + "\", \"message\":\"" + receive(conn, true) + "\"}";
} catch (Exception e) {
e.printStackTrace();
return "{\"errno\":-1,\"error\": \"" + e.getMessage() + "\"}";
}
}
public static String doPut(String url, Map<String, Object> params, Map<String, String> httpParams) {
return doPut(url, params, httpParams, null);
}
public static String doPut(String url, Map<String, Object> params, Map<String, String> httpParams, Map<String, String> headParams) {
try {
url = setUrl(httpParams, url);
log.info("=PUT=========================\n正在请求:{}", url);
URL urlObj = new URL(url);
HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection();
conn.setRequestMethod("PUT");
conn.setConnectTimeout(5000);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.addRequestProperty("Content-Type", "application/json");
if (headParams != null) {
for (Map.Entry<String, String> item : headParams.entrySet()) {
conn.addRequestProperty(item.getKey(), item.getValue());
}
}
if (params != null) {
OutputStream os = conn.getOutputStream();
os.write(JSON.toJSONString(params).getBytes());
os.flush();
}
conn.connect();
int code = conn.getResponseCode();
if (code == 200) {
return receive(conn);
}
return "{\"errno\": \"" + code + "\"}";
} catch (Exception e) {
e.printStackTrace();
return "{\"errno\":-1,\"error\": \"" + e.getMessage() + "\"}";
}
}
public static String doGet(String url, Map<String, String> params, Map<String, String> headParams) {
try {
url = setUrl(params, url);
log.info("=GET=========================\n正在请求:{}", url);
URL urlObj = new URL(url);
HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
conn.setDoInput(true);
conn.setDoOutput(true);
if (headParams != null) {
for (Map.Entry<String, String> item : headParams.entrySet()) {
conn.addRequestProperty(item.getKey(), item.getValue());
}
}
conn.connect();
int code = conn.getResponseCode();
if (code == 200) {
return receive(conn);
}
return "{\"errno\": \"" + code + "\", \"message\":\"" + receive(conn, true) + "\"}";
} catch (Exception e) {
e.printStackTrace();
return "{\"errno\": -1,\"error\": \"" + e.getMessage() + "\"}";
}
}
public static String setUrl(Map httpParams, String url) {
if (httpParams != null) {
if (!url.contains("?")) {
url += "?";
}
if (!httpParams.isEmpty()) {
url += joinHttpParam(httpParams, "&");
}
}
return url;
}
public static String joinHttpParam(Map o, String flag) {
StringBuffer strBuff = new StringBuffer();
Set<Map.Entry> set = o.entrySet();
for (Map.Entry item : set) {
strBuff.append(item.getKey());
strBuff.append("=");
strBuff.append(item.getValue());
strBuff.append(flag);
}
return strBuff.toString().substring(0, strBuff.length() - 1);
}
private static String receive(HttpURLConnection conn) throws Exception {
return receive(conn, false);
}
public static String receive(HttpURLConnection conn, boolean error) throws Exception {
if (error) {
return "";
}
InputStream is = conn.getInputStream();
String result = "";
byte[] buffer = new byte[2048];
byte[] total = new byte[0];
int size = -1;
while ((size = is.read(buffer)) != -1) {
byte[] temp = new byte[size + total.length];
System.arraycopy(total, 0, temp, 0, total.length);
System.arraycopy(buffer, 0, temp, total.length, size);
total = temp;
}
result = new String(total, "UTF-8");
log.info("返回结果:{}\n===========================", result);
return result;
}
}