import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
public class HttpRequestUtil {
private static final Logger log = LoggerFactory.getLogger(HttpRequestUtil.class);
private static final String POST = "POST";
private static final String PUT = "PUT";
private static final String GET = "GET";
private static final String DELETE = "DELETE";
public static final String CHARSET_UTF8 = "UTF-8";
public static final String CONTENT_TYPE = "Content-type";
public static final String CONTENT_TYPE_JSON = "application/json";
public static final String CONTENT_TYPE_FORMDATA = "multipart/form-data";
public static final String CONTENT_TYPE_TEXT_PLAIN = "text/plain";
public static final String MIME_TYPE_IMAGE_JPEG = "image/jpeg";
private static final String BOUNDARY = "----WebKitFormBoundary07I8UIuBx6LN2KyY";
public static String sendGet(String url, Map<String, String> params, String cookie, Map<String, String> headerMap) {
return send(GET, url, params, cookie, headerMap, null, null);
}
public static String sendDelete(String url, Map<String, String> params, String cookie, Map<String, String> headerMap) {
return send(DELETE, url, params, cookie, headerMap, null, null);
}
public static String sendPost(String url, Map<String, String> params, String cookie, Map<String, String> headerMap) {
return sendPostUplodFile(url, params, cookie, headerMap, null, null);
}
public static String sendPostUplodFile(String url, Map<String, String> params, String cookie, Map<String, String> headerMap, String fileName, byte[] bytes) {
return send(POST, url, params, cookie, headerMap, fileName, bytes);
}
public static String sendPut(String url, Map<String, String> params, String cookie, Map<String, String> headerMap) {
return send(PUT, url, params, cookie, headerMap, null, null);
}
public static String sendPutUplodFile(String url, Map<String, String> params, String cookie, Map<String, String> headerMap, String fileName, byte[] bytes) {
return send(PUT, url, params, cookie, headerMap, fileName, bytes);
}
private static String send(String type, String url, Map<String, String> params, String cookie, Map<String, String> headerMap, String fileName, byte[] bytes) {
DataOutputStream out = null;
BufferedReader in = null;
StringBuilder result = new StringBuilder();
try {
if (type.equals(GET) || type.equals(DELETE)) {
if (params != null) {
StringBuilder urlBuilder = new StringBuilder(url + "?");
for (String s : params.keySet()) {
urlBuilder.append(s).append("=").append(params.get(s)).append("&");
}
url = urlBuilder.toString();
}
}
HttpURLConnection conn = getHttpUrlConnection(url, type, cookie, headerMap);
if (type.equals(POST) || type.equals(PUT)) {
out = new DataOutputStream(conn.getOutputStream());
byte[] end_data = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();
if (params != null) {
for (String key : params.keySet()) {
String sb = "--" +
BOUNDARY +
"\r\n" +
"Content-Disposition: form-data;name=\"" + key + "\"" +
"\r\n" +
"\r\n" +
params.get(key) +
"\r\n";
out.write(sb.getBytes());
}
}
if (null != fileName && null != bytes) {
String sb = "--" +
BOUNDARY +
"\r\n" +
"Content-Disposition: form-data;name=\"file\";filename=\"" + fileName + "\"" +
"\r\n" +
"Content-Type: " + MIME_TYPE_IMAGE_JPEG +
"\r\n" +
"\r\n";
out.write(sb.getBytes());
out.write(bytes);
out.write("\r\n".getBytes());
}
out.write(end_data);
out.flush();
}
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result.append(line);
}
} catch (Exception e) {
log.error("发送请求出现异常:", e);
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
return result.toString();
}
private static HttpURLConnection getHttpUrlConnection(String url, String type, String cookie, Map<String, String> headerMap) throws IOException {
URL realUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
if (POST.equals(type) || PUT.equals(type)) {
conn.setDoOutput(true);
conn.setDoInput(true);
}
conn.setUseCaches(false);
conn.setRequestMethod(type);
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36");
conn.setRequestProperty("Charsert", CHARSET_UTF8);
if (cookie != null) {
conn.setRequestProperty("Cookie", cookie);
}
conn.setRequestProperty(CONTENT_TYPE, CONTENT_TYPE_FORMDATA + "; boundary=" + BOUNDARY);
if (headerMap != null && headerMap.keySet().size() != 0) {
for (String s : headerMap.keySet()) {
conn.setRequestProperty(s, headerMap.get(s));
}
}
conn.connect();
return conn;
}
}
|