Sfoglia il codice sorgente

解决redirect延迟

lirong 2 anni fa
parent
commit
2d2f876b5f

+ 232 - 236
ibms-data-sdk/src/main/java/com/persagy/ibms/data/sdk/util/HttpClientUtil.java

@@ -24,266 +24,262 @@ import org.apache.http.util.EntityUtils;
 
 import cn.hutool.core.lang.Tuple;
 import sun.misc.BASE64Encoder;
+
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.ServletOutputStream;
 import javax.servlet.http.HttpServletResponse;
 
 @SuppressWarnings("restriction")
 public class HttpClientUtil {
-	private static Map<String, HttpClientUtil> instanceMap = new ConcurrentHashMap<String, HttpClientUtil>();
+    private static Map<String, HttpClientUtil> instanceMap = new ConcurrentHashMap<String, HttpClientUtil>();
 
-	private HttpClient client;
+    private HttpClient client;
 
-	public static HttpClientUtil instance() {
-		HttpClientUtil result = instance("default");
-		return result;
-	}
+    public static HttpClientUtil instance() {
+        HttpClientUtil result = instance("default");
+        return result;
+    }
 
-	public static HttpClientUtil instance(String name) {
-		if (!instanceMap.containsKey(name)) {
-			HttpClientUtil newObj = new HttpClientUtil();
-			newObj.client = HttpClients.createDefault();
-			instanceMap.putIfAbsent(name, newObj);
-		}
-		HttpClientUtil instance = instanceMap.get(name);
-		return instance;
-	}
+    public static HttpClientUtil instance(String name) {
+        if (!instanceMap.containsKey(name)) {
+            HttpClientUtil newObj = new HttpClientUtil();
+            newObj.client = HttpClients.createDefault();
+            instanceMap.putIfAbsent(name, newObj);
+        }
+        HttpClientUtil instance = instanceMap.get(name);
+        return instance;
+    }
 
-	public synchronized String put(String url, String content) throws Exception {
-		String result = put(url, content, 300000, null);
-		return result;
-	}
+    public synchronized String put(String url, String content) throws Exception {
+        String result = put(url, content, 300000, null);
+        return result;
+    }
 
-	public synchronized String put(String url, String content, Integer timeout, Map<String, String> headerMap) throws Exception {
-		if (Constant.http_interval > 0) {
-			Thread.sleep(Constant.http_interval);
-		}
+    public synchronized String put(String url, String content, Integer timeout, Map<String, String> headerMap) throws Exception {
+        if (Constant.http_interval > 0) {
+            Thread.sleep(Constant.http_interval);
+        }
 
-		HttpPut httpost = new HttpPut(url);
-		if (headerMap != null) {
-			for (String key : headerMap.keySet()) {
-				httpost.setHeader(key, headerMap.get(key));
-			}
-		}
-		if (timeout == null) {
-			timeout = 300000;
-		}
-		if (timeout != null) {
-			RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout).setConnectionRequestTimeout(timeout)
-					.setSocketTimeout(timeout).build();
-			httpost.setConfig(requestConfig);
-		}
-		StringEntity entity = new StringEntity(content, "UTF-8");
-		httpost.setEntity(entity);
-		entity.setContentType("application/json");
-		HttpResponse httpResponse = client.execute(httpost);
-		InputStream is = httpResponse.getEntity().getContent();
-		ByteArrayOutputStream baos = new ByteArrayOutputStream();
-		int b = -1;
-		while ((b = is.read()) != -1) {
-			baos.write(b);
-		}
-		baos.close();
-		is.close();
+        HttpPut httpost = new HttpPut(url);
+        if (headerMap != null) {
+            for (String key : headerMap.keySet()) {
+                httpost.setHeader(key, headerMap.get(key));
+            }
+        }
+        if (timeout == null) {
+            timeout = 300000;
+        }
+        if (timeout != null) {
+            RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout).setConnectionRequestTimeout(timeout)
+                    .setSocketTimeout(timeout).build();
+            httpost.setConfig(requestConfig);
+        }
+        StringEntity entity = new StringEntity(content, "UTF-8");
+        httpost.setEntity(entity);
+        entity.setContentType("application/json");
+        HttpResponse httpResponse = client.execute(httpost);
+        InputStream is = httpResponse.getEntity().getContent();
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        int b = -1;
+        while ((b = is.read()) != -1) {
+            baos.write(b);
+        }
+        baos.close();
+        is.close();
 
-		String result = baos.toString("UTF-8");
-		return result;
-	}
+        String result = baos.toString("UTF-8");
+        return result;
+    }
 
-	public synchronized String get(String url) throws Exception {
-		String result = get(url, 300000, null);
-		return result;
-	}
+    public synchronized String get(String url) throws Exception {
+        String result = get(url, 300000, null);
+        return result;
+    }
 
-	public synchronized String get(String url, Integer timeout, Map<String, String> headerMap) throws Exception {
-		if (Constant.http_interval > 0) {
-			Thread.sleep(Constant.http_interval);
-		}
+    public synchronized String get(String url, Integer timeout, Map<String, String> headerMap) throws Exception {
+        if (Constant.http_interval > 0) {
+            Thread.sleep(Constant.http_interval);
+        }
 
-		HttpGet httpget = new HttpGet(url);
-		if (headerMap != null) {
-			for (String key : headerMap.keySet()) {
-				httpget.setHeader(key, headerMap.get(key));
-			}
-		}
-		if (timeout == null) {
-			timeout = 300000;
-		}
-		if (timeout != null) {
-			RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout).setConnectionRequestTimeout(timeout)
-					.setSocketTimeout(timeout).build();
-			httpget.setConfig(requestConfig);
-		}
-		// httpget.setHeader(new BasicHeader("Content-type", "text/plain;charset=utf-8"));
-		// httpget.setHeader(new BasicHeader("Content-type", "application/json;charset=utf-8"));
-		HttpResponse response = client.execute(httpget);
-		InputStream is = response.getEntity().getContent();
-		ByteArrayOutputStream os = new ByteArrayOutputStream();
-		int i = -1;
-		while ((i = is.read()) != -1) {
-			os.write(i);
-		}
-		is.close();
-		os.close();
+        HttpGet httpget = new HttpGet(url);
+        if (headerMap != null) {
+            for (String key : headerMap.keySet()) {
+                httpget.setHeader(key, headerMap.get(key));
+            }
+        }
+        if (timeout == null) {
+            timeout = 300000;
+        }
+        if (timeout != null) {
+            RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout).setConnectionRequestTimeout(timeout)
+                    .setSocketTimeout(timeout).build();
+            httpget.setConfig(requestConfig);
+        }
+        // httpget.setHeader(new BasicHeader("Content-type", "text/plain;charset=utf-8"));
+        // httpget.setHeader(new BasicHeader("Content-type", "application/json;charset=utf-8"));
+        HttpResponse response = client.execute(httpget);
+        InputStream is = response.getEntity().getContent();
+        ByteArrayOutputStream os = new ByteArrayOutputStream();
+        int i = -1;
+        while ((i = is.read()) != -1) {
+            os.write(i);
+        }
+        is.close();
+        os.close();
 
-		String result = os.toString("UTF-8");
-		return result;
-	}
+        String result = os.toString("UTF-8");
+        return result;
+    }
 
-	public synchronized String post_compress(String url, String content) throws Exception {
-		if (Constant.zkt_dmp_compress) {
-			String resultOri = post(url + "&compress=true", content);
-			String result = GZIPCompressUtil.unzipBase64(resultOri);
-			return result;
-		} else {
-			String result = post(url, content);
-			return result;
-		}
-	}
+    public synchronized String post_compress(String url, String content) throws Exception {
+        if (Constant.zkt_dmp_compress) {
+            String resultOri = post(url + "&compress=true", content);
+            String result = GZIPCompressUtil.unzipBase64(resultOri);
+            return result;
+        } else {
+            String result = post(url, content);
+            return result;
+        }
+    }
 
-	public synchronized String post(String url, String content) throws Exception {
-		String result = post(url, content, 300000, null,null,null);
-		return result;
-	}
+    public synchronized String post(String url, String content) throws Exception {
+        String result = post(url, content, 300000, null, null, null);
+        return result;
+    }
 
-	public synchronized String post(String url, String content,HttpServletResponse response) throws Exception {
-		String result = post(url, content, 300000, null,response,null);
-		return result;
-	}
+    public synchronized String post(String url, String content, HttpServletResponse response) throws Exception {
+        String result = post(url, content, 300000, null, response, null);
+        return result;
+    }
 
-	public synchronized String post(String url, String content, Integer timeout, Map<String, String> headerMap,HttpServletResponse response, HttpServletRequest request) throws Exception {
-		if (Constant.http_interval > 0) {
-			Thread.sleep(Constant.http_interval);
-		}
+    public synchronized String post(String url, String content, Integer timeout, Map<String, String> headerMap, HttpServletResponse response, HttpServletRequest request) throws Exception {
+        if (Constant.http_interval > 0) {
+            Thread.sleep(Constant.http_interval);
+        }
 
-		HttpPost httpost = new HttpPost(url);
-		if (!url.contains("login")&&!url.contains("getCaptchaBase64"))
-		{
-			if(request!=null)
-			{
-				String authorization=request.getHeader("Authorization");
-				if(authorization!=null)
-				{
-					httpost.setHeader("Authorization", authorization);
-				}
-			}
-		}
-		if (headerMap != null) {
-			for (String key : headerMap.keySet()) {
-				httpost.setHeader(key, headerMap.get(key));
-			}
-		}
-		if (timeout == null) {
-			timeout = 300000;
-		}
-		//登录这里设置成短连接
-		if (url.contains("getCaptchaBase64") || url.contains("login")){
-			httpost.setProtocolVersion(HttpVersion.HTTP_1_0);
-			httpost.setHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE);
-		}
-		if (timeout != null) {
-			RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout).setConnectionRequestTimeout(timeout)
-					.setSocketTimeout(timeout).build();
-			httpost.setConfig(requestConfig);
-		}
-		StringEntity entity = new StringEntity(content, "UTF-8");
-		httpost.setEntity(entity);
-		entity.setContentType("application/json");
-		HttpResponse httpResponse = client.execute(httpost);
-		if(url.contains("login"))
-		{
-			Header responseFirstHeader= httpResponse.getFirstHeader("Authorization");
-			if(responseFirstHeader!=null)
-			{
-				response.setHeader(responseFirstHeader.getName(),responseFirstHeader.getValue());
-			}
-		}
-		InputStream is = httpResponse.getEntity().getContent();
-		ByteArrayOutputStream baos = new ByteArrayOutputStream();
-		int b = -1;
-		while ((b = is.read()) != -1) {
-			baos.write(b);
-		}
-		baos.close();
-		is.close();
+        HttpPost httpost = new HttpPost(url);
+        if (request != null) {
+            String authorization = request.getHeader("Authorization");
+            if (authorization != null) {
+                httpost.setHeader("Authorization", authorization);
+            }
+        }
+        if (headerMap != null) {
+            for (String key : headerMap.keySet()) {
+                httpost.setHeader(key, headerMap.get(key));
+            }
+        }
+        if (timeout == null) {
+            timeout = 300000;
+        }
+        //登录这里设置成短连接
+        if (url.contains("getCaptchaBase64") || url.contains("/person-center/user/login")) {
+            httpost.setProtocolVersion(HttpVersion.HTTP_1_0);
+            httpost.setHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE);
+        }
+        if (timeout != null) {
+            RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout).setConnectionRequestTimeout(timeout)
+                    .setSocketTimeout(timeout).build();
+            httpost.setConfig(requestConfig);
+        }
+        StringEntity entity = new StringEntity(content, "UTF-8");
+        httpost.setEntity(entity);
+        entity.setContentType("application/json");
+        HttpResponse httpResponse = client.execute(httpost);
+        if (url.contains("/person-center/user/login")) {
+            if (httpResponse != null) {
+                Header responseFirstHeader = httpResponse.getFirstHeader("Authorization");
+                if (responseFirstHeader != null) {
+                    response.setHeader(responseFirstHeader.getName(), responseFirstHeader.getValue());
+                }
+            }
+        }
+        InputStream is = httpResponse.getEntity().getContent();
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        int b = -1;
+        while ((b = is.read()) != -1) {
+            baos.write(b);
+        }
+        baos.close();
+        is.close();
 
-		String result = baos.toString("UTF-8");
-		return result;
-	}
+        String result = baos.toString("UTF-8");
+        return result;
+    }
 
-	public synchronized Tuple postImg(String url, String content, Integer timeout, Map<String, String> headerMap) throws Exception {
-		HttpPost httpost = new HttpPost(url);
-		if (headerMap != null) {
-			for (String key : headerMap.keySet()) {
-				httpost.setHeader(key, headerMap.get(key));
-			}
-		}
-		if (timeout == null) {
-			timeout = 300000;
-		}
-		if (timeout != null) {
-			RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout).setConnectionRequestTimeout(timeout)
-					.setSocketTimeout(timeout).build();
-			httpost.setConfig(requestConfig);
-		}
-		StringEntity entity = new StringEntity(content, "UTF-8");
-		httpost.setEntity(entity);
-		entity.setContentType("application/json");
-		HttpResponse httpResponse = client.execute(httpost);
-		String guid = httpResponse.getHeaders("guid")[0].getValue();
-		InputStream is = httpResponse.getEntity().getContent();
-		ByteArrayOutputStream baos = new ByteArrayOutputStream();
-		int b = -1;
-		while ((b = is.read()) != -1) {
-			baos.write(b);
-		}
-		baos.close();
-		is.close();
-		byte[] data = baos.toByteArray();
-		BASE64Encoder encoder = new BASE64Encoder();
-		String imageBase64 = "data:image/png;base64," + encoder.encodeBuffer(data).trim();
-		imageBase64 = imageBase64.replaceAll("\n", "").replaceAll("\r", "").replaceAll(" ", "");
-		Tuple tuple = new Tuple(guid, imageBase64);
-		return tuple;
-	}
+    public synchronized Tuple postImg(String url, String content, Integer timeout, Map<String, String> headerMap) throws Exception {
+        HttpPost httpost = new HttpPost(url);
+        if (headerMap != null) {
+            for (String key : headerMap.keySet()) {
+                httpost.setHeader(key, headerMap.get(key));
+            }
+        }
+        if (timeout == null) {
+            timeout = 300000;
+        }
+        if (timeout != null) {
+            RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout).setConnectionRequestTimeout(timeout)
+                    .setSocketTimeout(timeout).build();
+            httpost.setConfig(requestConfig);
+        }
+        StringEntity entity = new StringEntity(content, "UTF-8");
+        httpost.setEntity(entity);
+        entity.setContentType("application/json");
+        HttpResponse httpResponse = client.execute(httpost);
+        String guid = httpResponse.getHeaders("guid")[0].getValue();
+        InputStream is = httpResponse.getEntity().getContent();
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        int b = -1;
+        while ((b = is.read()) != -1) {
+            baos.write(b);
+        }
+        baos.close();
+        is.close();
+        byte[] data = baos.toByteArray();
+        BASE64Encoder encoder = new BASE64Encoder();
+        String imageBase64 = "data:image/png;base64," + encoder.encodeBuffer(data).trim();
+        imageBase64 = imageBase64.replaceAll("\n", "").replaceAll("\r", "").replaceAll(" ", "");
+        Tuple tuple = new Tuple(guid, imageBase64);
+        return tuple;
+    }
 
-	public synchronized void post_export(String url, String content, Integer timeout, Map<String, String> headerMap, HttpServletResponse response) throws Exception {
-		if (Constant.http_interval > 0) {
-			Thread.sleep(Constant.http_interval);
-		}
+    public synchronized void post_export(String url, String content, Integer timeout, Map<String, String> headerMap, HttpServletResponse response) throws Exception {
+        if (Constant.http_interval > 0) {
+            Thread.sleep(Constant.http_interval);
+        }
 
-		HttpPost httpost = new HttpPost(url);
-		if (headerMap != null) {
-			for (String key : headerMap.keySet()) {
-				httpost.setHeader(key, headerMap.get(key));
-			}
-		}
-		if (timeout == null) {
-			timeout = 300000;
-		}
-		if (timeout != null) {
-			RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout).setConnectionRequestTimeout(timeout)
-					.setSocketTimeout(timeout).build();
-			httpost.setConfig(requestConfig);
-		}
-		StringEntity entity = new StringEntity(content, "UTF-8");
-		httpost.setEntity(entity);
-		entity.setContentType("application/json");
-		HttpResponse httpResponse = client.execute(httpost);
-		InputStream is = httpResponse.getEntity().getContent();
-		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH_mm_ss.SSS");
-		String xlsFileName = sdf.format(new Date()) + ".xlsx";
-		xlsFileName = URLEncoder.encode(xlsFileName, "UTF-8");
-		response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
-		response.setHeader("Content-Disposition", "attachment;filename=" + xlsFileName);
-		ServletOutputStream out = response.getOutputStream();
-		// 输出
-		int len = 1;
-		byte[] bytes = new byte[1024];
-		while ((len = is.read(bytes)) != -1) {
-			out.write(bytes, 0, len);
-		}
-		out.close();
-		is.close();
-	}
+        HttpPost httpost = new HttpPost(url);
+        if (headerMap != null) {
+            for (String key : headerMap.keySet()) {
+                httpost.setHeader(key, headerMap.get(key));
+            }
+        }
+        if (timeout == null) {
+            timeout = 300000;
+        }
+        if (timeout != null) {
+            RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout).setConnectionRequestTimeout(timeout)
+                    .setSocketTimeout(timeout).build();
+            httpost.setConfig(requestConfig);
+        }
+        StringEntity entity = new StringEntity(content, "UTF-8");
+        httpost.setEntity(entity);
+        entity.setContentType("application/json");
+        HttpResponse httpResponse = client.execute(httpost);
+        InputStream is = httpResponse.getEntity().getContent();
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH_mm_ss.SSS");
+        String xlsFileName = sdf.format(new Date()) + ".xlsx";
+        xlsFileName = URLEncoder.encode(xlsFileName, "UTF-8");
+        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
+        response.setHeader("Content-Disposition", "attachment;filename=" + xlsFileName);
+        ServletOutputStream out = response.getOutputStream();
+        // 输出
+        int len = 1;
+        byte[] bytes = new byte[1024];
+        while ((len = is.read(bytes)) != -1) {
+            out.write(bytes, 0, len);
+        }
+        out.close();
+        is.close();
+    }
 }