All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.utils.HttpUtils Maven / Gradle / Ivy

The newest version!
package com.utils;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.text.SimpleDateFormat;
import java.util.Map;
import java.util.zip.GZIPInputStream;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;

@Component
@Scope("prototype")
@ConfigurationProperties(prefix = "httputils")
public class HttpUtils {
	@Autowired
	private ProxyUtils pu;
	private int connecttime = 60000;
	private int readtime = 60000;
	public Logger common = CustomLogger.getLogger("common1.log");
	public JSONObject httpget(String urlString) {
		return httpget(urlString, null, null);
	}

	public JSONObject httpget(String urlString, ProxyUtils pu) {
		return httpget(urlString, null, pu);
	}

	public JSONObject httpget(String urlString, Map map) {
		return httpget(urlString, map, null);
	}

	public JSONObject httpgetwithproxy(String urlString) {
		return httpget(urlString, null, pu);
	}

	public JSONObject httpgetwithproxy(String urlString, Map map) {
		return httpget(urlString, map, pu);
	}

	public JSONObject httpget(String urlString, Map map, ProxyUtils pu) {
		JSONObject json = new JSONObject();
		JSONObject proxyJson = null;
		try {
			URL url = new URL(urlString);
			HttpURLConnection conn = null;
			if (pu != null) {
				proxyJson = pu.getNewProxy();
				if (proxyJson != null) {
					String usrString = proxyJson.getString("usr");
					String pwdString = proxyJson.getString("pwd");
					String ipString = proxyJson.getString("ip");
					int port = proxyJson.getIntValue("port");
					if (!usrString.equals("") && !pwdString.equals("")) {
						ThreadLocalAuthenticator.getUserthreadlocal().set(usrString);
						ThreadLocalAuthenticator.getPasswordthreadlocal().set(pwdString);
					}
					Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ipString, port));
					conn = (HttpURLConnection) url.openConnection(proxy);
				} else {
					conn = (HttpURLConnection) url.openConnection();
				}
			} else {
				conn = (HttpURLConnection) url.openConnection();
			}
			verifyHttps(conn);
			conn.setConnectTimeout(connecttime);
			conn.setReadTimeout(readtime);
			if (map != null)
				for (Map.Entry entry : map.entrySet()) {
					conn.setRequestProperty(entry.getKey(), entry.getValue());
				}
			conn.connect();

			String result = null;
			long start=System.currentTimeMillis();
			String encoding = conn.getContentEncoding();
			if(System.currentTimeMillis()-start>1000*120){
				common.warn(urlString+"---0000"+conn.getContentType());
			}
			if (encoding != null && encoding.equals("gzip")) {
				try (InputStream inputstream = conn.getInputStream(); GZIPInputStream gzipInputstream = new GZIPInputStream(inputstream); InputStreamReader input = new InputStreamReader(gzipInputstream, "UTF-8"); BufferedReader bufferedreader = new BufferedReader(input, 1024);) {
					result = "";
					String line;
					while ((line = bufferedreader.readLine()) != null) {
						result += line;
						if(System.currentTimeMillis()-start>1000*120){
							common.warn(new SimpleDateFormat("yyMMdd-HHmmss:SSS").format(System.currentTimeMillis())+"---"+urlString+"---"+line);
						}
					}
				}
			} else {
				try (InputStream inputstream = conn.getInputStream(); InputStreamReader input = new InputStreamReader(inputstream, "UTF-8"); BufferedReader bufferedreader = new BufferedReader(input, 1024);) {
					result = "";
					String line;
					while ((line = bufferedreader.readLine()) != null) {
						result += line;
						if(System.currentTimeMillis()-start>1000*120){
							common.warn(new SimpleDateFormat("yyMMdd-HHmmss:SSS").format(System.currentTimeMillis())+"---"+urlString+"---"+line);
						}
					}
				}
			}
			conn.disconnect();
			json.put("ec", 0);
			json.put("result", result);
			json.put("proxy", proxyJson);
		} catch (IOException e) {
			json.put("ec", -1);
			json.put("error", e.toString());
			json.put("url", urlString);
			json.put("proxy", proxyJson);
		}
		return json;
	}

	public JSONObject httppost(String urlString, String data) {
		return httppost(urlString, data, null, null);
	}

	public JSONObject httppost(String urlString, String data, ProxyUtils pu) {
		return httppost(urlString, data, null, pu);
	}

	public JSONObject httppost(String urlString, String data, Map map) {
		return httppost(urlString, data, map, null);
	}

	public JSONObject httppostwithproxy(String urlString, String data) {
		return httppost(urlString, data, null, pu);
	}

	public JSONObject httppostwithproxy(String urlString, String data, Map map) {
		return httppost(urlString, data, map, pu);
	}

	public JSONObject httppost(String urlString, String data, Map map, ProxyUtils pu) {
		JSONObject json = new JSONObject();
		JSONObject proxyJson = null;
		int rc = 0;
		try {
			byte[] payload = data.getBytes("utf-8");
			URL url = new URL(urlString);
			HttpURLConnection conn = null;

			if (pu != null) {
				proxyJson = pu.getNewProxy();
				if (proxyJson != null) {
					String usrString = proxyJson.getString("usr");
					String pwdString = proxyJson.getString("pwd");
					String ipString = proxyJson.getString("ip");
					int port = proxyJson.getIntValue("port");
					if (!usrString.equals("") && !pwdString.equals("")) {
						ThreadLocalAuthenticator.getUserthreadlocal().set(usrString);
						ThreadLocalAuthenticator.getPasswordthreadlocal().set(pwdString);
					}
					Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ipString, port));
					conn = (HttpURLConnection) url.openConnection(proxy);
				} else {
					conn = (HttpURLConnection) url.openConnection();
				}
			} else {
				conn = (HttpURLConnection) url.openConnection();
			}
			verifyHttps(conn);

			conn.setDoOutput(true);
			conn.setDoInput(true);
			conn.setUseCaches(false);
			conn.setConnectTimeout(connecttime);
			conn.setReadTimeout(readtime);
			conn.setRequestMethod("POST");
			conn.setRequestProperty("Content-length", "" + payload.length);
			if (map != null)
				for (Map.Entry entry : map.entrySet()) {
					conn.setRequestProperty(entry.getKey(), entry.getValue());
				}
			conn.connect();
			try (OutputStream output = conn.getOutputStream(); DataOutputStream outStream = new DataOutputStream(output);) {
				outStream.write(payload);
				outStream.flush();
			}

			String result = null;
			rc = conn.getResponseCode();
			if(rc>=400){
				try (InputStream inputstream = conn.getErrorStream(); InputStreamReader input = new InputStreamReader(inputstream, "UTF-8"); BufferedReader bufferedreader = new BufferedReader(input, 1024);) {
					result = "";
					String line;
					while ((line = bufferedreader.readLine()) != null) {
						result += line;
					}
				}
				json.put("ec", -2);
				json.put("rc", rc);
				json.put("result", result);
				json.put("proxy", proxyJson);
			}else{
				String encoding = conn.getContentEncoding();
				if (encoding != null && encoding.equals("gzip")) {
					try (InputStream inputstream = conn.getInputStream(); GZIPInputStream gzipInputstream = new GZIPInputStream(inputstream); InputStreamReader input = new InputStreamReader(gzipInputstream, "UTF-8"); BufferedReader bufferedreader = new BufferedReader(input, 1024);) {
						result = "";
						String line;
						while ((line = bufferedreader.readLine()) != null) {
							result += line;
						}
					}
				} else {
					try (InputStream inputstream = conn.getInputStream(); InputStreamReader input = new InputStreamReader(inputstream, "UTF-8"); BufferedReader bufferedreader = new BufferedReader(input, 1024);) {
						result = "";
						String line;
						while ((line = bufferedreader.readLine()) != null) {
							result += line;
						}
					}
				}
				json.put("ec", 0);
				json.put("rc", rc);
				json.put("result", result);
				json.put("proxy", proxyJson);
			}
			conn.disconnect();

		} catch (IOException e) {
			json.put("ec", -1);
			json.put("rc", rc);
			json.put("error", e.toString());
			json.put("url", urlString);
			json.put("proxy", proxyJson);
		}
		return json;
	}

	private void verifyHttps(HttpURLConnection conn) {
		if (conn instanceof HttpsURLConnection) {
			((HttpsURLConnection) conn).setHostnameVerifier(new HostnameVerifier() {
				public boolean verify(String hostname, SSLSession session) {
					return true;
				}
			});
			TrustManager[] tm = new TrustManager[1];
			tm[0] = new IgnoreCertificationTrustManger();
			SSLContext sslContext = null;
			try {
				sslContext = SSLContext.getInstance("SSL", "SunJSSE");
				sslContext.init(null, tm, new java.security.SecureRandom());
			} catch (NoSuchAlgorithmException | NoSuchProviderException | KeyManagementException e) {
				throw new MyException(e);
			}
			SSLSocketFactory ssf = sslContext.getSocketFactory();
			((HttpsURLConnection) conn).setSSLSocketFactory(ssf);
		}
	}

	public class IgnoreCertificationTrustManger implements X509TrustManager {

		private X509Certificate[] certificates;

		@Override
		public void checkClientTrusted(X509Certificate certificates[], String authType) throws CertificateException {
			if (this.certificates == null) {
				this.certificates = certificates;
			}
		}

		@Override
		public void checkServerTrusted(X509Certificate[] ax509certificate, String s) throws CertificateException {
			if (this.certificates == null) {
				this.certificates = ax509certificate;
			}
		}

		@Override
		public X509Certificate[] getAcceptedIssuers() {
			return null;
		}

	}

	public int getConnecttime() {
		return connecttime;
	}

	public void setConnecttime(int connecttime) {
		this.connecttime = connecttime;
	}

	public int getReadtime() {
		return readtime;
	}

	public void setReadtime(int readtime) {
		this.readtime = readtime;
	}

	@Override
	public String toString() {
		return "HttpUtils [pu=" + pu + ", connecttime=" + connecttime + ", readtime=" + readtime + "]";
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy