1、http请求发送-工具类
/**
* http请求发送
*/
@Slf4j
public class HttpClientUtil {
public static String sendGet(String uri, String headerName, String headerValue)
throws IOException {
Content content = Request.Get(uri).setHeader(headerName, headerValue)
.execute().returnContent();
return content.asString();
}
public static String sendPost(String url, String body, String contentType, String headerName, String headerValue)
throws IOException {
Content content = Request.Post(url)
.setHeader(headerName, headerValue)
.bodyString(body, ContentType.create(contentType))
.execute().returnContent();
return content.asString();
}
public static String toGMTString(Date date) {
SimpleDateFormat df = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z", Locale.UK);
df.setTimeZone(new java.util.SimpleTimeZone(0, "GMT"));
return df.format(date);
}
public static String sendPost(String url, String body, String akId, String akSecret) {
return sendPostOrPut(url, "POST", body, akId, akSecret);
}
public static String sendPut(String url, String body, String akId, String akSecret) {
return sendPostOrPut(url, "PUT", body, akId, akSecret);
}
private static String sendPostOrPut(String url, String method, String body, String akId, String akSecret) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
String accept = "json";
String contentType = "application/json";
String path = realUrl.getFile();
String date = toGMTString(new Date());
String bodyMd5 = DigestUtil.md5Base64(body);
String stringToSign = method + "\n" + accept + "\n" + bodyMd5 + "\n" + contentType + "\n" + date + "\n"
+ path;
String signature = DigestUtil.hmacsha1(stringToSign, akSecret);
String authHeader = "dtboost-proxy " + akId + ":" + signature;
HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
conn.setRequestMethod(method);
conn.setRequestProperty("accept", accept);
conn.setRequestProperty("content-type", contentType);
conn.setRequestProperty("date", date);
conn.setRequestProperty("Authorization", authHeader);
conn.setRequestProperty("Content-MD5", bodyMd5);
conn.setDoOutput(true);
conn.setDoInput(true);
out = new PrintWriter(conn.getOutputStream());
out.print(body);
out.flush();
if (conn.getResponseCode() != 200) {
in = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
} else {
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 (IOException ex) {
ex.printStackTrace();
}
}
return result;
}
public static String sendAKGet(String url, String akId, String akSecret) {
String result = "";
BufferedReader in = null;
try {
URL realUrl = new URL(url);
String method = "GET";
String accept = "json";
String contentType = "application/json";
String path = realUrl.getFile();
String date = toGMTString(new Date());
String stringToSign = method + "\n" + accept + "\n" + "" + "\n" + contentType + "\n" + date + "\n" + path;
String signature = DigestUtil.hmacsha1(stringToSign, akSecret);
String authHeader = "dtboost-proxy " + akId + ":" + signature;
HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
connection.setRequestProperty("accept", accept);
connection.setRequestProperty("content-type", contentType);
connection.setRequestProperty("date", date);
connection.setRequestProperty("Authorization", authHeader);
connection.connect();
if (connection.getResponseCode() != 200) {
in = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
} else {
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
}
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
/**
* http head请求
* @throws Exception
*/
public static String sendHeadGet(String path, Long projectId) throws Exception{
String value = "{'extraInfo':{'projectId':'"+projectId+"'}}";
try {
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//connection.setRequestProperty("设置请求头key", "请求头value");
connection.addRequestProperty("CityBrain-User-Profile", value);
//连接会话
connection.connect();
// 获取输入流
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
String line;
StringBuilder sb = new StringBuilder();
while (Objects.nonNull((line = br.readLine()))) {
sb.append(line);
}
br.close();
connection.disconnect();
String result = sb.toString();
return result;
} catch (Exception e) {
log.error("请求失败,url:{}", path);
throw e;
}
}
}