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.
388 lines
14 KiB
388 lines
14 KiB
2 years ago
|
package com.ynxbd.common.helper.common;
|
||
|
|
||
|
import org.slf4j.Logger;
|
||
|
import org.slf4j.LoggerFactory;
|
||
|
|
||
|
import javax.servlet.ServletOutputStream;
|
||
|
import javax.servlet.http.HttpServletRequest;
|
||
|
import javax.servlet.http.HttpServletResponse;
|
||
|
import java.io.*;
|
||
|
import java.net.HttpURLConnection;
|
||
|
import java.net.InetSocketAddress;
|
||
|
import java.net.Proxy;
|
||
|
import java.net.URL;
|
||
|
import java.nio.charset.StandardCharsets;
|
||
|
import java.util.Date;
|
||
|
|
||
|
public class ProxyHelper {
|
||
|
private static final Logger logger = LoggerFactory.getLogger(ProxyHelper.class);
|
||
|
|
||
|
|
||
|
public static String post(String httpUrl, String proxyIp, Integer port, HttpServletRequest req, HttpServletResponse resp) throws IOException {
|
||
|
servletConfig(req, resp);
|
||
|
|
||
|
BufferedReader in = null;
|
||
|
BufferedReader reader = null;
|
||
|
HttpURLConnection connection = null;
|
||
|
// 方法
|
||
|
String method = req.getMethod();
|
||
|
// 参数
|
||
|
String params = req.getQueryString();
|
||
|
|
||
|
String contentType = req.getContentType();
|
||
|
|
||
|
try {
|
||
|
URL url = new URL(httpUrl + "?" + params);
|
||
|
|
||
|
//设置代理
|
||
|
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyIp, port));
|
||
|
connection = (HttpURLConnection) url.openConnection(proxy);
|
||
|
connection.setRequestMethod(method);
|
||
|
connection.setRequestProperty("accept", "*/*");
|
||
|
connection.setRequestProperty("connection", "Keep-Alive");
|
||
|
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
||
|
if (contentType == null) {
|
||
|
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
||
|
} else {
|
||
|
connection.setRequestProperty("Content-Type", contentType);
|
||
|
}
|
||
|
connection.setDoOutput(true);
|
||
|
connection.setDoInput(true);
|
||
|
connection.connect();
|
||
|
|
||
|
int status = connection.getResponseCode();
|
||
|
String message = connection.getResponseMessage();
|
||
|
String requestURI = req.getRequestURI();
|
||
|
|
||
|
logger.info("{}-{}-{url:[{}],contentType-[{}],status:[{}], message:[{}]}", method, requestURI, url, contentType, status, message);
|
||
|
|
||
|
// 读取响应数据
|
||
|
reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
|
||
|
StringBuilder result = new StringBuilder();
|
||
|
String line;
|
||
|
while ((line = reader.readLine()) != null) {
|
||
|
result.append(line);
|
||
|
}
|
||
|
resp.setStatus(status);
|
||
|
servletResp(result.toString(), resp);
|
||
|
} catch (IOException e) {
|
||
|
// 响应失败信息
|
||
|
servletResp(e.toString(), resp);
|
||
|
e.printStackTrace();
|
||
|
} finally {
|
||
|
try {
|
||
|
if (reader != null) {
|
||
|
reader.close();
|
||
|
}
|
||
|
|
||
|
if (connection != null) {
|
||
|
// 断开连接
|
||
|
connection.disconnect();
|
||
|
}
|
||
|
|
||
|
} catch (IOException e) {
|
||
|
e.printStackTrace();
|
||
|
}
|
||
|
}
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
public static String get(String httpUrl, String proxyIp, Integer port, HttpServletRequest req, HttpServletResponse resp) {
|
||
|
servletConfig(req, resp);
|
||
|
String header = resp.getContentType();
|
||
|
|
||
|
BufferedReader reader = null;
|
||
|
HttpURLConnection connection = null;
|
||
|
// 方法
|
||
|
String method = req.getMethod();
|
||
|
// 参数
|
||
|
String params = req.getQueryString();
|
||
|
|
||
|
String contentType = req.getContentType();
|
||
|
try {
|
||
|
URL url = new URL(httpUrl + "?" + params);
|
||
|
//设置代理
|
||
|
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyIp, port));
|
||
|
connection = (HttpURLConnection) url.openConnection(proxy);
|
||
|
connection.setRequestMethod(method);
|
||
|
connection.setRequestProperty("accept", "*/*");
|
||
|
connection.setRequestProperty("connection", "Keep-Alive");
|
||
|
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
||
|
|
||
|
connection.setRequestProperty("Content-Type", contentType == null ? "application/x-www-form-urlencoded" : contentType);
|
||
|
|
||
|
connection.setDoOutput(true);
|
||
|
connection.setDoInput(true);
|
||
|
connection.connect();
|
||
|
|
||
|
int status = connection.getResponseCode();
|
||
|
String message = connection.getResponseMessage();
|
||
|
String requestURI = req.getRequestURI();
|
||
|
|
||
|
logger.info("{}-{}-{url:[{}],contentType-[{}],status:[{}], message:[{}]}", method, requestURI, url, contentType, status, message);
|
||
|
|
||
|
// 读取响应数据
|
||
|
reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
|
||
|
StringBuilder result = new StringBuilder();
|
||
|
String line;
|
||
|
while ((line = reader.readLine()) != null) {
|
||
|
result.append(line);
|
||
|
}
|
||
|
resp.setStatus(status);
|
||
|
servletResp(result.toString(), resp);
|
||
|
} catch (IOException e) {
|
||
|
// 响应失败信息
|
||
|
servletResp(e.toString(), resp);
|
||
|
e.printStackTrace();
|
||
|
} finally {
|
||
|
try {
|
||
|
if (reader != null) reader.close();
|
||
|
|
||
|
if (connection != null) connection.disconnect(); // 断开连接
|
||
|
|
||
|
} catch (IOException e) {
|
||
|
e.printStackTrace();
|
||
|
}
|
||
|
}
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
|
||
|
public static String down(String httpUrl, String proxyIp, Integer port, HttpServletRequest req, HttpServletResponse resp, String msgInterface) {
|
||
|
servletConfig(req, resp);
|
||
|
|
||
|
BufferedReader reader = null;
|
||
|
HttpURLConnection connection = null;
|
||
|
// 方法
|
||
|
String method = req.getMethod();
|
||
|
// 参数
|
||
|
String params = req.getQueryString();
|
||
|
|
||
|
String contentType = req.getContentType();
|
||
|
InputStream in = null;
|
||
|
ServletOutputStream out = null;
|
||
|
try {
|
||
|
URL url = new URL(httpUrl + "?" + params);
|
||
|
//设置代理
|
||
|
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyIp, port));
|
||
|
connection = (HttpURLConnection) url.openConnection(proxy);
|
||
|
connection.setRequestMethod("GET");
|
||
|
connection.setRequestProperty("accept", "*/*");
|
||
|
connection.setRequestProperty("connection", "Keep-Alive");
|
||
|
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
||
|
connection.setRequestProperty("Content-Type", contentType == null ? "application/x-www-form-urlencoded" : contentType);
|
||
|
|
||
|
if (msgInterface != null) resp.setContentType(msgInterface);
|
||
|
resp.addHeader("Content-Disposition", "attachment; filename=" + new Date().getTime() + ".dcm");
|
||
|
|
||
|
connection.setDoOutput(true);
|
||
|
connection.setDoInput(true);
|
||
|
connection.connect();
|
||
|
|
||
|
int status = connection.getResponseCode();
|
||
|
String message = connection.getResponseMessage();
|
||
|
String requestURI = req.getRequestURI();
|
||
|
String respContentType = resp.getContentType();
|
||
|
resp.setStatus(status);
|
||
|
|
||
|
logger.info("{}-{}-{url:[{}],contentType-[{}],respContentType-[{}]status:[{}], message:[{}]}", method, requestURI, url, contentType, respContentType, status, message);
|
||
|
|
||
|
// 读取响应数据
|
||
|
in = connection.getInputStream();
|
||
|
// byte[] getData = readInputStream(in);
|
||
|
out = resp.getOutputStream();
|
||
|
|
||
|
byte[] buff = new byte[1024 * 1024];
|
||
|
while (in.read(buff) != -1) out.write(buff);
|
||
|
//\\DESKTOP-QKUSP27\pacs
|
||
|
|
||
|
// 读取响应数据
|
||
|
// reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||
|
// StringBuilder result = new StringBuilder();
|
||
|
// String line;
|
||
|
// while ((line = reader.readLine()) != null) {
|
||
|
// result.append(line).append("\n");
|
||
|
// }
|
||
|
// servletResp(result.toString(), resp);
|
||
|
|
||
|
} catch (IOException e) {
|
||
|
// 响应失败信息
|
||
|
servletResp(e.toString(), resp);
|
||
|
e.printStackTrace();
|
||
|
} finally {
|
||
|
try {
|
||
|
if (reader != null) reader.close();
|
||
|
|
||
|
if (out != null) out.close();
|
||
|
|
||
|
if (in != null) in.close();
|
||
|
|
||
|
if (connection != null) connection.disconnect(); // 断开连接
|
||
|
|
||
|
} catch (IOException e) {
|
||
|
e.printStackTrace();
|
||
|
}
|
||
|
}
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
/**
|
||
|
* 从输入流中获取字节数组
|
||
|
*
|
||
|
* @param is 输入流
|
||
|
* @return
|
||
|
*/
|
||
|
public static byte[] readInputStream(InputStream is) {
|
||
|
ByteArrayOutputStream bos = null;
|
||
|
while (true) {
|
||
|
try {
|
||
|
bos = new ByteArrayOutputStream();
|
||
|
|
||
|
int len;
|
||
|
byte[] buffer = new byte[1024];
|
||
|
if ((len = is.read(buffer)) == -1) break;
|
||
|
bos.write(buffer, 0, len);
|
||
|
|
||
|
} catch (IOException e) {
|
||
|
e.printStackTrace();
|
||
|
} finally {
|
||
|
try {
|
||
|
if (bos != null) bos.close();
|
||
|
} catch (IOException e) {
|
||
|
e.printStackTrace();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return bos.toByteArray();
|
||
|
}
|
||
|
|
||
|
|
||
|
public static void servletConfig(HttpServletRequest req, HttpServletResponse resp) {
|
||
|
try {
|
||
|
req.setCharacterEncoding("utf-8");
|
||
|
// resp.setCharacterEncoding("utf-8");
|
||
|
// resp.setContentType("application/json;charset=utf-8");
|
||
|
/* 允许跨域的主机地址 */
|
||
|
resp.setHeader("Access-Control-Allow-Origin", "*");
|
||
|
/* 允许跨域的请求方法GET, POST, HEAD 等 */
|
||
|
resp.setHeader("Access-Control-Allow-Methods", "*");
|
||
|
/* 重新预检验跨域的缓存时间 (s) */
|
||
|
resp.setHeader("Access-Control-Max-Age", "3600");
|
||
|
/* 允许跨域的请求头 */
|
||
|
resp.setHeader("Access-Control-Allow-Headers", "*");
|
||
|
/* 是否携带cookie */
|
||
|
resp.setHeader("Access-Control-Allow-Credentials", "true");
|
||
|
} catch (UnsupportedEncodingException e) {
|
||
|
e.printStackTrace();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* 设置响应内容
|
||
|
*
|
||
|
* @param respMsg 响应内容
|
||
|
* @param resp 响应流
|
||
|
*/
|
||
|
protected static void servletResp(String respMsg, HttpServletResponse resp) {
|
||
|
try {
|
||
|
resp.getWriter().write(respMsg);
|
||
|
} catch (IOException e) {
|
||
|
e.printStackTrace();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
// private static class TrustAnyTrustManager implements X509TrustManager {
|
||
|
//
|
||
|
// public void checkClientTrusted(X509Certificate[] chain, String authType) {
|
||
|
// }
|
||
|
//
|
||
|
// public void checkServerTrusted(X509Certificate[] chain, String authType) {
|
||
|
// }
|
||
|
//
|
||
|
// public X509Certificate[] getAcceptedIssuers() {
|
||
|
// return new X509Certificate[]{};
|
||
|
// }
|
||
|
// }
|
||
|
//
|
||
|
// private static class TrustAnyHostnameVerifier implements HostnameVerifier {
|
||
|
// public boolean verify(String hostname, SSLSession session) {
|
||
|
// return true;
|
||
|
// }
|
||
|
// }
|
||
|
|
||
|
|
||
|
// public static String HttpsProxy(String url, String param, String proxy, int port) {
|
||
|
// HttpsURLConnection httpsConn = null;
|
||
|
// PrintWriter out = null;
|
||
|
// BufferedReader in = null;
|
||
|
// StringBuilder result = new StringBuilder();
|
||
|
// BufferedReader reader = null;
|
||
|
// try {
|
||
|
// URL urlClient = new URL(url);
|
||
|
// System.out.println("请求的URL========:" + urlClient);
|
||
|
//
|
||
|
// SSLContext sc = SSLContext.getInstance("SSL");
|
||
|
// // 指定信任https
|
||
|
// sc.init(null, new TrustManager[]{new TrustAnyTrustManager()}, new java.security.SecureRandom());
|
||
|
// //创建代理虽然是https也是Type.HTTP
|
||
|
// Proxy proxy1 = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxy, port));
|
||
|
// //设置代理
|
||
|
// httpsConn = (HttpsURLConnection) urlClient.openConnection(proxy1);
|
||
|
//
|
||
|
// httpsConn.setSSLSocketFactory(sc.getSocketFactory());
|
||
|
// httpsConn.setHostnameVerifier(new TrustAnyHostnameVerifier());
|
||
|
// // 设置通用的请求属性
|
||
|
// httpsConn.setRequestProperty("accept", "*/*");
|
||
|
// httpsConn.setRequestProperty("connection", "Keep-Alive");
|
||
|
// httpsConn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
||
|
// // 发送POST请求必须设置如下两行
|
||
|
// httpsConn.setDoOutput(true);
|
||
|
// httpsConn.setDoInput(true);
|
||
|
// // 获取URLConnection对象对应的输出流
|
||
|
// out = new PrintWriter(httpsConn.getOutputStream());
|
||
|
// // 发送请求参数
|
||
|
// out.print(param);
|
||
|
// // flush输出流的缓冲
|
||
|
// out.flush();
|
||
|
// // 定义BufferedReader输入流来读取URL的响应
|
||
|
// in = new BufferedReader(
|
||
|
// new InputStreamReader(httpsConn.getInputStream()));
|
||
|
// String line;
|
||
|
// while ((line = in.readLine()) != null) {
|
||
|
// result.append(line);
|
||
|
// }
|
||
|
// // 断开连接
|
||
|
// httpsConn.disconnect();
|
||
|
// System.out.println("====result====" + result);
|
||
|
// System.out.println("返回结果:" + httpsConn.getResponseMessage());
|
||
|
//
|
||
|
// } catch (Exception e) {
|
||
|
// e.printStackTrace();
|
||
|
// } finally {
|
||
|
// try {
|
||
|
// if (reader != null) {
|
||
|
// reader.close();
|
||
|
// }
|
||
|
// } catch (IOException e) {
|
||
|
// }
|
||
|
// try {
|
||
|
// if (in != null) {
|
||
|
// in.close();
|
||
|
// }
|
||
|
// } catch (IOException e) {
|
||
|
// e.printStackTrace();
|
||
|
// }
|
||
|
// if (out != null) {
|
||
|
// out.close();
|
||
|
// }
|
||
|
// }
|
||
|
//
|
||
|
// return result.toString();
|
||
|
// }
|
||
|
}
|