From 2fcd97552d16718cc7997629fd637a73a5a4483f Mon Sep 17 00:00:00 2001 From: 郑永安 <zyazyz250@sina.com> Date: 星期一, 19 六月 2023 14:44:19 +0800 Subject: [PATCH] 删除 --- src/main/java/com/gk/firework/Domain/Utils/WebUtils.java | 197 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 197 insertions(+), 0 deletions(-) diff --git a/src/main/java/com/gk/firework/Domain/Utils/WebUtils.java b/src/main/java/com/gk/firework/Domain/Utils/WebUtils.java new file mode 100644 index 0000000..40381d0 --- /dev/null +++ b/src/main/java/com/gk/firework/Domain/Utils/WebUtils.java @@ -0,0 +1,197 @@ +package com.gk.firework.Domain.Utils; + +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.method.HandlerMethod; + +import javax.net.ssl.*; +import java.io.*; +import java.net.HttpURLConnection; +import java.net.URL; +import java.security.SecureRandom; +import java.security.cert.CertificateException; +import java.security.cert.X509Certificate; +import java.util.Iterator; +import java.util.Map; + +/** + * Miscellaneous utilities for web applications. + * @author L.cm + */ +public class WebUtils extends org.springframework.web.util.WebUtils { + /** + * 判断是否ajax请求 + * spring ajax 返回含有 ResponseBody 或者 RestController注解 + * @param handlerMethod HandlerMethod + * @return 是否ajax请求 + */ + + private static final String DEFAULT_CHARSET = "UTF-8"; + private static boolean ignoreSSLCheck = true; + private static boolean ignoreHostCheck = true; + + public static boolean isAjax(HandlerMethod handlerMethod) { + ResponseBody responseBody = handlerMethod.getMethodAnnotation(ResponseBody.class); + if (null != responseBody) { + return true; + } + RestController restAnnotation = handlerMethod.getBean().getClass().getAnnotation(RestController.class); + if (null != restAnnotation) { + return true; + } + return false; + } + + private static HttpURLConnection getConnection(URL url, String method, String ctype, Map<String, String> headerMap) throws IOException { + Object conn = (HttpURLConnection)url.openConnection(); + if(conn instanceof HttpsURLConnection) { + HttpsURLConnection i$ = (HttpsURLConnection)conn; + if(ignoreSSLCheck) { + try { + SSLContext entry = SSLContext.getInstance("TLS"); + entry.init((KeyManager[])null, new TrustManager[]{new TrustAllTrustManager()}, new SecureRandom()); + i$.setSSLSocketFactory(entry.getSocketFactory()); + i$.setHostnameVerifier(new HostnameVerifier() { + public boolean verify(String hostname, SSLSession session) { + return true; + } + }); + } catch (Exception var7) { + throw new IOException(var7.toString()); + } + } else if(ignoreHostCheck) { + i$.setHostnameVerifier(new HostnameVerifier() { + public boolean verify(String hostname, SSLSession session) { + return true; + } + }); + } + + conn = i$; + } + + ((HttpURLConnection)conn).setRequestMethod(method); + ((HttpURLConnection)conn).setDoInput(true); + ((HttpURLConnection)conn).setDoOutput(true); + if(headerMap != null && headerMap.get("TOP_HTTP_DNS_HOST") != null) { + ((HttpURLConnection)conn).setRequestProperty("Host", (String)headerMap.get("TOP_HTTP_DNS_HOST")); + } else { + ((HttpURLConnection)conn).setRequestProperty("Host", url.getHost()); + } + + ((HttpURLConnection)conn).setRequestProperty("Accept", "text/xml,text/javascript"); + ((HttpURLConnection)conn).setRequestProperty("Content-Type", ctype); + if(headerMap != null) { + Iterator i$1 = headerMap.entrySet().iterator(); + + while(i$1.hasNext()) { + Map.Entry entry1 = (Map.Entry)i$1.next(); + if(!"TOP_HTTP_DNS_HOST".equals(entry1.getKey())) { + ((HttpURLConnection)conn).setRequestProperty((String)entry1.getKey(), (String)entry1.getValue()); + } + } + } + + return (HttpURLConnection)conn; + } + + public static String getStreamAsString(InputStream stream, String charset) throws IOException { + try { + InputStreamReader reader = new InputStreamReader(stream, charset); + StringBuilder response = new StringBuilder(); + char[] buff = new char[1024]; + boolean read = false; + + int read1; + while((read1 = reader.read(buff)) > 0) { + response.append(buff, 0, read1); + } + + String var6 = response.toString(); + return var6; + } finally { + if(stream != null) { + stream.close(); + } + + } + } + + public static class TrustAllTrustManager implements X509TrustManager { + public TrustAllTrustManager() { + } + + public X509Certificate[] getAcceptedIssuers() { + return null; + } + + public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { + } + + public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { + } + } + + public static String ytoSendPost(String url, Map<String, String> params) + { + OutputStreamWriter out = null; + BufferedReader in = null; + StringBuilder result = new StringBuilder(); + try { + URL realUrl = new URL(url); + HttpURLConnection conn =(HttpURLConnection) realUrl.openConnection(); + // 发送POST请求必须设置如下两行 + conn.setDoOutput(true); + conn.setDoInput(true); + // POST方法 + conn.setRequestMethod("POST"); + // 设置通用的请求属性 + conn.setRequestProperty("accept", "*/*"); + conn.setRequestProperty("connection", "Keep-Alive"); + conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); + conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); + conn.connect(); + // 获取URLConnection对象对应的输出流 + out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8"); + // 发送请求参数 + if (params != null) { + StringBuilder param = new StringBuilder(); + for (Map.Entry<String, String> entry : params.entrySet()) { + if(param.length()>0){ + param.append("&"); + } + param.append(entry.getKey()); + param.append("="); + param.append(entry.getValue()); + } + out.write(param.toString()); + } + // flush输出流的缓冲 + out.flush(); + // 定义BufferedReader输入流来读取URL的响应 + in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8")); + String line; + while ((line = in.readLine()) != null) { + result.append(line); + } + } catch (Exception e) { + e.printStackTrace(); + } + //使用finally块来关闭输出流、输入流 + finally{ + try{ + if(out!=null){ + out.close(); + } + if(in!=null){ + in.close(); + } + } + catch(IOException ex){ + ex.printStackTrace(); + } + } + return result.toString(); + } + +} -- Gitblit v1.9.2