HTTPutils
package com.server.publish.utils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;
public class HttpUtils {
public static String get(String url, Map<String, String> headMap) throws Exception {
HttpGet httpGet = new HttpGet(url);
if (headMap != null) {
for (String key : headMap.keySet()) {
httpGet.addHeader(key, headMap.get(key));
}
}
HttpClient client = HttpClientBuilder.create().build();
HttpResponse response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
String resBody = EntityUtils.toString(entity, "UTF-8");
return resBody;
}
public static InputStream down(String url, Map<String, String> headMap) throws Exception {
url = url.replaceAll("\\\\", "/");
HttpGet httpGet = new HttpGet(url);
if (headMap != null) {
for (String key : headMap.keySet()) {
httpGet.setHeader(key, headMap.get(key));
}
}
HttpClient client = HttpClientBuilder.create().build();
HttpResponse response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
InputStream input = entity.getContent();
return input;
}
public static String post(String url, Map<String, String> headMap) throws Exception {
HttpPost httpPost = new HttpPost(url);
if (headMap != null) {
for (String key : headMap.keySet()) {
httpPost.setHeader(key, headMap.get(key));
}
}
HttpClient client = HttpClientBuilder.create().build();
HttpResponse response = client.execute(httpPost);
HttpEntity entity = response.getEntity();
String resBody = EntityUtils.toString(entity, "UTF-8");
return resBody;
}
public static String post(String url, List<NameValuePair> params, Map<String, String> headMap) throws Exception {
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
if (headMap != null) {
for (String key : headMap.keySet()) {
httpPost.setHeader(key, headMap.get(key));
}
}
HttpClient client = HttpClientBuilder.create().build();
HttpResponse response = client.execute(httpPost);
HttpEntity entity = response.getEntity();
String resBody = EntityUtils.toString(entity, "UTF-8");
return resBody;
}
public static String post(String url, String entityString, Map<String, String> headMap) throws Exception {
HttpPost httpPost = new HttpPost(url);
StringEntity stringEntity = new StringEntity(entityString, Charset.forName("UTF-8"));
stringEntity.setContentType("application/json");
httpPost.setEntity(stringEntity);
httpPost.setHeader("Content-Type", "application/json;charset=utf-8");
if (headMap != null) {
for (String key : headMap.keySet()) {
httpPost.setHeader(key, headMap.get(key));
}
}
HttpClient client = HttpClientBuilder.create().build();
HttpResponse response = client.execute(httpPost);
HttpEntity entity = response.getEntity();
String resBody = EntityUtils.toString(entity, "UTF-8");
return resBody;
}
public static String delete(String url, Map<String, String> headMap) throws Exception {
HttpDelete httpDelete = new HttpDelete(url);
if (headMap != null) {
for (String key : headMap.keySet()) {
httpDelete.setHeader(key, headMap.get(key));
}
}
HttpClient client = HttpClientBuilder.create().build();
HttpResponse response = client.execute(httpDelete);
HttpEntity entity = response.getEntity();
String resBody = EntityUtils.toString(entity, "UTF-8");
return resBody;
}
public static byte[] readInputStream(InputStream inStream) throws Exception {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
inStream.close();
return outStream.toByteArray();
}
}