|
|
@ -17,15 +17,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
package com.tencent.cloud.polaris.util;
|
|
|
|
package com.tencent.cloud.polaris.util;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
|
|
|
|
import java.net.URL;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Objects;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
|
|
|
|
import com.squareup.okhttp.OkHttpClient;
|
|
|
|
|
|
|
|
import com.squareup.okhttp.Request;
|
|
|
|
|
|
|
|
import com.squareup.okhttp.Response;
|
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* okhttp util.
|
|
|
|
* okhttp util.
|
|
|
|
*
|
|
|
|
*
|
|
|
@ -38,52 +41,46 @@ public final class OkHttpUtil {
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public final static Logger LOGGER = LoggerFactory.getLogger(OkHttpUtil.class);
|
|
|
|
public final static Logger LOGGER = LoggerFactory.getLogger(OkHttpUtil.class);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* client.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private final static OkHttpClient HTTP_CLIENT = new OkHttpClient();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private OkHttpUtil() {
|
|
|
|
private OkHttpUtil() {
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* get request.
|
|
|
|
* get request.
|
|
|
|
* @param url url
|
|
|
|
* @param path path
|
|
|
|
* @param headers headers
|
|
|
|
* @param headers headers
|
|
|
|
* @return response
|
|
|
|
* @return response
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static boolean get(String url, Map<String, String> headers) {
|
|
|
|
public static boolean get(String path, Map<String, String> headers) {
|
|
|
|
|
|
|
|
HttpURLConnection conn = null;
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
Request.Builder builder = new Request.Builder();
|
|
|
|
URL url = new java.net.URL(path);
|
|
|
|
buildHeader(builder, headers);
|
|
|
|
conn = (HttpURLConnection) url.openConnection();
|
|
|
|
Request request = builder.url(url).build();
|
|
|
|
|
|
|
|
Response response = HTTP_CLIENT.newCall(request).execute();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (response.isSuccessful() && Objects.nonNull(response.body())) {
|
|
|
|
conn.setRequestMethod("GET");
|
|
|
|
String result = response.body().string();
|
|
|
|
conn.setConnectTimeout((int) TimeUnit.SECONDS.toMillis(2));
|
|
|
|
LOGGER.debug("exec get request, url: {} success, response data: {}", url, result);
|
|
|
|
conn.setReadTimeout((int) TimeUnit.SECONDS.toMillis(2));
|
|
|
|
|
|
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
|
|
|
|
|
|
|
|
StringBuffer buffer = new StringBuffer();
|
|
|
|
|
|
|
|
String str;
|
|
|
|
|
|
|
|
while ((str = reader.readLine()) != null) {
|
|
|
|
|
|
|
|
buffer.append(str);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
String responseBody = buffer.toString();
|
|
|
|
|
|
|
|
if (conn.getResponseCode() == 200 && StringUtils.hasText(responseBody)) {
|
|
|
|
|
|
|
|
LOGGER.debug("exec get request, url: {} success, response data: {}", url, responseBody);
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
catch (Exception e) {
|
|
|
|
LOGGER.error("exec get request, url: {} failed!", url, e);
|
|
|
|
LOGGER.error("exec get request, url: {} failed!", path, e);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
finally {
|
|
|
|
}
|
|
|
|
if (null != conn) {
|
|
|
|
|
|
|
|
conn.disconnect();
|
|
|
|
/**
|
|
|
|
}
|
|
|
|
* build header.
|
|
|
|
|
|
|
|
* @param builder builder
|
|
|
|
|
|
|
|
* @param headers headers
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private static void buildHeader(Request.Builder builder, Map<String, String> headers) {
|
|
|
|
|
|
|
|
if (Objects.nonNull(headers) && headers.size() > 0) {
|
|
|
|
|
|
|
|
headers.forEach((k, v) -> {
|
|
|
|
|
|
|
|
if (Objects.nonNull(k) && Objects.nonNull(v)) {
|
|
|
|
|
|
|
|
builder.addHeader(k, v);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|