You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
140 lines
5.1 KiB
140 lines
5.1 KiB
package com.fr.plugin.utils; |
|
|
|
import com.fr.log.FineLoggerFactory; |
|
import com.fr.third.fasterxml.jackson.core.JsonProcessingException; |
|
import com.fr.third.fasterxml.jackson.databind.ObjectMapper; |
|
import com.fr.third.org.apache.commons.codec.digest.DigestUtils; |
|
import com.fr.third.org.apache.commons.io.IOUtils; |
|
|
|
import javax.net.ssl.*; |
|
import java.io.*; |
|
import java.net.URL; |
|
import java.nio.charset.Charset; |
|
import java.nio.charset.StandardCharsets; |
|
import java.security.cert.CertificateException; |
|
import java.security.cert.X509Certificate; |
|
import java.time.LocalDate; |
|
|
|
public class HttpUtils { |
|
public static String inputStream2String(InputStream inputStream) { |
|
StringBuilder sb = new StringBuilder(); |
|
BufferedReader reader = null; |
|
|
|
try { |
|
reader = new BufferedReader(new InputStreamReader(inputStream, Charset.defaultCharset())); |
|
String line; |
|
while ((line = reader.readLine()) != null) { |
|
sb.append(line); |
|
} |
|
} catch (IOException e) { |
|
throw new RuntimeException(e); |
|
} finally { |
|
if (reader != null) { |
|
try { |
|
reader.close(); |
|
} catch (IOException e) { |
|
} |
|
} |
|
} |
|
return sb.toString(); |
|
} |
|
|
|
static class MyX509TrustManager implements X509TrustManager { |
|
|
|
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { |
|
} |
|
|
|
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { |
|
} |
|
|
|
public X509Certificate[] getAcceptedIssuers() { |
|
return null; |
|
} |
|
} |
|
|
|
public static String https(String requestUrl, String requestMethod, String outputStr) { |
|
String result = null; |
|
StringBuffer buffer = new StringBuffer(); |
|
HttpsURLConnection httpUrlConn = null; |
|
try { |
|
// 创建SSLContext对象,并使用我们指定的信任管理器初始化 |
|
TrustManager[] tm = {new MyX509TrustManager()}; |
|
SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE"); |
|
sslContext.init(null, tm, new java.security.SecureRandom()); |
|
// 从上述SSLContext对象中得到SSLSocketFactory对象 |
|
SSLSocketFactory ssf = sslContext.getSocketFactory(); |
|
|
|
URL url = new URL(requestUrl); |
|
httpUrlConn = (HttpsURLConnection) url.openConnection(); |
|
httpUrlConn.setSSLSocketFactory(ssf); |
|
|
|
httpUrlConn.setDoOutput(false); |
|
httpUrlConn.setDoInput(true); |
|
httpUrlConn.setUseCaches(false); |
|
// 设置请求方式(GET/POST) |
|
httpUrlConn.setRequestMethod(requestMethod); |
|
httpUrlConn.connect(); |
|
|
|
// 将返回的输入流转换成字符串 |
|
InputStream inputStream = httpUrlConn.getInputStream(); |
|
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8"); |
|
BufferedReader bufferedReader = new BufferedReader(inputStreamReader); |
|
|
|
String str = null; |
|
while ((str = bufferedReader.readLine()) != null) { |
|
buffer.append(str); |
|
} |
|
bufferedReader.close(); |
|
inputStreamReader.close(); |
|
// 释放资源 |
|
inputStream.close(); |
|
// inputStream = null; |
|
httpUrlConn.disconnect(); |
|
result = buffer.toString(); |
|
FineLoggerFactory.getLogger().error("请求响应时间:{}", result); |
|
// jsonObject = JSONObject.fromObject(buffer.toString()); |
|
} catch (Exception e) { |
|
if (httpUrlConn != null) { |
|
InputStream errorStream = httpUrlConn.getErrorStream(); |
|
if (errorStream != null) { |
|
try { |
|
String s = IOUtils.toString(errorStream, StandardCharsets.UTF_8); |
|
FineLoggerFactory.getLogger().error("resp logger :{}", s); |
|
} catch (IOException ioException) { |
|
ioException.printStackTrace(); |
|
} |
|
} |
|
} |
|
printException2Frlog(e); |
|
} |
|
return result; |
|
} |
|
|
|
public static void printException2Frlog(Exception e) { |
|
StringWriter writer = new StringWriter(); |
|
e.printStackTrace(new PrintWriter(writer)); |
|
String s = writer.toString(); |
|
FineLoggerFactory.getLogger().error("错误:{}", s); |
|
} |
|
|
|
public static String obj2JsonStr(Object obj) { |
|
ObjectMapper mapper = new ObjectMapper(); |
|
String json = ""; |
|
try { |
|
json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj); |
|
} catch (JsonProcessingException e) { |
|
e.printStackTrace(); |
|
} |
|
return json; |
|
} |
|
|
|
public static String genToken() { |
|
LocalDate date = LocalDate.now(); |
|
String key = "fr123"; |
|
return DigestUtils.md5Hex(date.toString() + key).toLowerCase(); |
|
} |
|
|
|
public static boolean checkToken(String tokens){ |
|
return genToken().equals(tokens); |
|
} |
|
}
|
|
|