package tools;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.ImmutableMap;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
public class HttpRequests {
public static String GetRequest(String url) {
URLConnection con = null;
BufferedReader buffer = null;
StringBuffer resultBuffer = null;
try {
URL requstUrl = new URL(url);
con = requstUrl.openConnection();
con.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
con.setDoOutput(true);
con.setDoInput(true);
con.setUseCaches(false);
InputStream inputStream = con.getInputStream();
resultBuffer = new StringBuffer();
String line;
buffer = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
while ((line = buffer.readLine()) != null) {
resultBuffer.append(line);
}
return resultBuffer.toString();
} catch (Exception e) {
e.printStackTrace();
}
return resultBuffer.toString();
}
public static String GetRequest(String url, String token) {
BufferedReader in = null;
CloseableHttpResponse response = null;
CloseableHttpClient httpClient = HttpClients.createDefault();
try {
httpClient = HttpClients.createDefault();
URIBuilder uriBuilder = new URIBuilder(url);
HttpGet httpGet = new HttpGet(uriBuilder.build());
if (!StringUtils.isBlank(token)) {
httpGet.setHeader("Authorization", token);
}
System.out.println(httpGet);
response = httpClient.execute(httpGet);
if (response.getStatusLine().getStatusCode() == 200) {
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "utf-8"));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
in.close();
return sb.toString();
}
System.out.println(response.getStatusLine().getStatusCode());
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String GetRequest(String url, String token, JSONObject data) {
String parm = data.toString();
String result = "";
PrintWriter out = null;
BufferedReader in = null;
try {
java.net.URL realUrl;
realUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "application/json");
conn.addRequestProperty("Authorization", token);
conn.setDoOutput(true);
conn.setDoInput(true);
out = new PrintWriter(conn.getOutputStream());
out.print(parm);
out.flush();
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
return result;
}
public static String PostRequest(String url, JSONObject data) {
String parm = data.toString();
String result = "";
PrintWriter out = null;
BufferedReader in = null;
try {
java.net.URL realUrl;
realUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
conn.setDoOutput(true);
conn.setDoInput(true);
out = new PrintWriter(conn.getOutputStream());
out.print(parm);
out.flush();
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
return result;
}
public static String PostRequest(String url, String data) {
String result = "";
PrintWriter out = null;
BufferedReader in = null;
try {
java.net.URL realUrl;
realUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
conn.setDoOutput(true);
conn.setDoInput(true);
out = new PrintWriter(conn.getOutputStream());
out.print(data);
out.flush();
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
return result;
}
public static String PostRequest(String url, String token, JSONObject data) {
String parm = data.toString();
String result = "";
PrintWriter out = null;
BufferedReader in = null;
try {
java.net.URL realUrl;
realUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
conn.addRequestProperty("Authorization", token);
conn.setDoOutput(true);
conn.setDoInput(true);
out = new PrintWriter(conn.getOutputStream());
out.print(parm);
out.flush();
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
return result;
}
public static String PostRequest(String url, String token, ImmutableMap<Object, Object> parameter) {
CloseableHttpClient httpClient = HttpClients.createDefault();
String entityStr = null;
CloseableHttpResponse response = null;
try {
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("Authorization", token);
StringEntity requestBody = new StringEntity(JSON.toJSONString(parameter));
requestBody.setContentType("application/json;charset=UTF-8");
requestBody.setContentEncoding(StandardCharsets.UTF_8.name());
httpPost.setEntity(requestBody);
response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
entityStr = EntityUtils.toString(entity, "UTF-8");
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != response) {
try {
response.close();
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return entityStr;
}
public static String PostRequest(String url, String token, JSONObject data, String orgId, String cookie) {
String parm = data.toString();
String result = "";
PrintWriter out = null;
BufferedReader in = null;
try {
java.net.URL realUrl;
realUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
conn.addRequestProperty("Authorization", token);
conn.addRequestProperty("x-org-id", orgId);
conn.addRequestProperty("Cookie", cookie);
conn.setDoOutput(true);
conn.setDoInput(true);
out = new PrintWriter(conn.getOutputStream());
out.print(parm);
out.flush();
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
return result;
}
public static String DeleteRequest(String url, String token) {
HttpURLConnection httpCon = null;
BufferedReader in = null;
String result = "";
try {
httpCon = (HttpURLConnection) new URL(url).openConnection();
httpCon.setDoOutput(true);
httpCon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpCon.addRequestProperty("Authorization", token);
httpCon.setRequestMethod("DELETE");
httpCon.connect();
httpCon.disconnect();
in = new BufferedReader(new InputStreamReader(httpCon.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
return result;
}
}
}