From 98b1a0affd69bbe63223c21fdd2c404e8bedfccb Mon Sep 17 00:00:00 2001
From: admin <weikou2014>
Date: 星期三, 20 五月 2020 17:25:08 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/div' into 2.1.2

---
 utils/src/main/java/org/yeshi/utils/HttpUtil.java |  175 +++++++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 166 insertions(+), 9 deletions(-)

diff --git a/utils/src/main/java/org/yeshi/utils/HttpUtil.java b/utils/src/main/java/org/yeshi/utils/HttpUtil.java
index 877fb52..161c024 100644
--- a/utils/src/main/java/org/yeshi/utils/HttpUtil.java
+++ b/utils/src/main/java/org/yeshi/utils/HttpUtil.java
@@ -9,10 +9,15 @@
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.net.URLEncoder;
+import java.security.KeyStore;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import javax.net.ssl.SSLContext;
 
 import org.apache.commons.httpclient.Header;
 import org.apache.commons.httpclient.HttpClient;
@@ -26,23 +31,41 @@
 import org.apache.commons.httpclient.methods.multipart.Part;
 import org.apache.commons.httpclient.methods.multipart.StringPart;
 import org.apache.commons.io.FileUtils;
+import org.apache.http.HttpEntity;
 import org.apache.http.HttpHost;
 import org.apache.http.auth.AuthScope;
 import org.apache.http.auth.UsernamePasswordCredentials;
 import org.apache.http.client.CredentialsProvider;
 import org.apache.http.client.config.RequestConfig;
-import org.apache.http.client.entity.UrlEncodedFormEntity;
 import org.apache.http.client.methods.CloseableHttpResponse;
 import org.apache.http.client.methods.HttpPost;
+import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
+import org.apache.http.conn.ssl.SSLContexts;
+import org.apache.http.entity.StringEntity;
 import org.apache.http.impl.client.BasicCredentialsProvider;
 import org.apache.http.impl.client.CloseableHttpClient;
 import org.apache.http.impl.client.HttpClients;
 import org.apache.http.util.EntityUtils;
 import org.yeshi.utils.entity.ProxyIP;
 
-import net.sf.json.JSONArray;
+import net.sf.json.JSONObject;
 
 public class HttpUtil {
+
+	public static List<String> getUrlListFromText(String text) {
+		List<String> urlList = new ArrayList<String>();
+		// 閾炬帴
+		String URL_REGEX = "(((http|https)://)|(www\\.))[a-zA-Z0-9\\._-]+\\.[a-zA-Z]{2,6}(:[0-9]{1,4})?(/[a-zA-Z0-9\\&%_\\./-~-]*)?";
+		Pattern p = Pattern.compile(URL_REGEX);
+		Matcher matcher = p.matcher(text);
+		while (matcher.find()) {
+			String url = matcher.group();
+			if (!StringUtil.isNullOrEmpty(url)) {
+				urlList.add(url);
+			}
+		}
+		return urlList;
+	}
 
 	public static String getHost(String host) {
 		try {
@@ -55,12 +78,55 @@
 	}
 
 	public static String getShortLink(String url) {
-		String totalUrl = String.format("http://api.t.sina.com.cn/short_url/shorten.json?source=3271760578&url_long=%s",
-				URLEncoder.encode(url));
-		String result = get(totalUrl, null);
-		JSONArray rtesultArray = JSONArray.fromObject(result);
-		if (rtesultArray.size() > 0) {
-			return rtesultArray.optJSONObject(0).optString("url_short");
+		// return url;
+		String shortLink = getShortLink3(url);
+		if (StringUtil.isNullOrEmpty(shortLink))
+			shortLink = getShortLink1(url);
+		if (StringUtil.isNullOrEmpty(shortLink))
+			shortLink = getShortLink2(url);
+		return shortLink;
+	}
+
+	private static String getShortLink1(String url) {
+		try {
+			String totalUrl = String.format(
+					"http://suo.im/api.htm?format=json&url=%s&key=5d7728078e676d45275f816b@681bb0446f1e6af4f8fc6ce3cad2a684",
+					URLEncoder.encode(url));
+			String result = get(totalUrl, null);
+			System.out.println(result);
+			try {
+				JSONObject json = JSONObject.fromObject(result);
+				return json.optString("url");
+			} catch (Exception e) {
+			}
+		} catch (Exception e) {
+		}
+		return null;
+	}
+
+	// 鎼滅嫄缃戝潃
+	private static String getShortLink2(String url) {
+		try {
+			String totalUrl = String.format("https://sohu.gg/api/?key=2zumZxsL8MuX&url=%s",
+					URLEncoder.encode(url, "UTF-8"));
+			String result = get(totalUrl, null);
+			if (result != null && result.startsWith("http"))
+				return result;
+		} catch (Exception e) {
+			e.printStackTrace();
+		}
+		return null;
+	}
+
+	private static String getShortLink3(String url) {
+		try {
+			String totalUrl = String.format("http://h5.flq001.com/short/createShort?url=%s",
+					URLEncoder.encode(url, "UTF-8"));
+			String result = get(totalUrl, null);
+			JSONObject data = JSONObject.fromObject(result);
+			return data.optString("short_url");
+		} catch (Exception e) {
+			e.printStackTrace();
 		}
 		return null;
 	}
@@ -277,6 +343,20 @@
 		return null;
 	}
 
+	public static String postSimple(String url) {
+		HttpClient client = new HttpClient();
+		PostMethod method = new PostMethod(url);
+		try {
+			client.executeMethod(method);
+			return method.getResponseBodyAsString();
+		} catch (HttpException e) {
+			e.printStackTrace();
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+		return "";
+	}
+
 	public static String post(String url) {
 		HttpClient client = new HttpClient();
 		PostMethod method = new PostMethod(url);
@@ -353,7 +433,7 @@
 
 	public static String post(String url, Map<String, String> map, Map<String, String> headers) {
 		HttpClient client = new HttpClient();
-		client.getHostConfiguration().setProxy("192.168.1.122", 8888);
+		// client.getHostConfiguration().setProxy("192.168.1.122", 8888);
 		PostMethod pm = new PostMethod(url);// 鍒涘缓HttpPost瀵硅薄
 		NameValuePair[] ns = new NameValuePair[map.keySet().size()];
 		Iterator<String> its = map.keySet().iterator();
@@ -401,6 +481,24 @@
 		PostMethod method = new PostMethod(url);
 		method.addRequestHeader("Content-Type", "text/html;charset=UTF-8");
 		method.setRequestHeader("Content-Type", "text/html;charset=UTF-8");
+		method.setRequestBody(entity);
+		try {
+			client.executeMethod(method);
+			return convertInputStreamToString(method.getResponseBodyAsStream());
+		} catch (HttpException e) {
+			e.printStackTrace();
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+		return "";
+	}
+
+	@SuppressWarnings("deprecation")
+	public static String post(String url, String entity, String charset) {
+		HttpClient client = new HttpClient();
+		PostMethod method = new PostMethod(url);
+		method.addRequestHeader("Content-Type", "text/html;charset=" + charset);
+		method.setRequestHeader("Content-Type", "text/html;charset=" + charset);
 		method.setRequestBody(entity);
 		try {
 			client.executeMethod(method);
@@ -483,4 +581,63 @@
 		}
 		return null;
 	}
+
+	@SuppressWarnings("deprecation")
+	public static String httpsPost(String url, String body, String pwd, InputStream cert) throws Exception {
+		String result = "";
+		KeyStore keyStore = KeyStore.getInstance("PKCS12");
+		InputStream instream = cert;
+		try {
+			keyStore.load(instream, pwd.toCharArray());
+		} finally {
+			instream.close();
+		}
+
+		// Trust own CA and all self-signed certs
+		SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, pwd.toCharArray()).build();
+		// Allow TLSv1 protocol only
+		SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" }, null,
+				SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
+		CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
+		try {
+			HttpPost httppost = new HttpPost(url);
+			httppost.setHeader("Content-Type", "text/html;charset=UTF-8");
+			System.out.println("executing request" + httppost.getRequestLine());
+			httppost.setEntity(new StringEntity(body, "UTF-8"));
+			CloseableHttpResponse response = httpclient.execute(httppost);
+			try {
+				HttpEntity entity = response.getEntity();
+				if (entity != null) {
+					result = convertInputStreamToString(entity.getContent());
+				}
+				EntityUtils.consume(entity);
+			} finally {
+				response.close();
+			}
+		} finally {
+			httpclient.close();
+		}
+
+		return result;
+	}
+
+	/**
+	 * 鑾峰彇post杩斿洖location
+	 * 
+	 * @param url
+	 * @return
+	 */
+	public static String getLocation(String url) {
+		HttpClient client = new HttpClient();
+		PostMethod method = new PostMethod(url);
+		try {
+			client.executeMethod(method);
+			return method.getResponseHeader("location").getValue();
+		} catch (HttpException e) {
+			e.printStackTrace();
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+		return null;
+	}
 }

--
Gitblit v1.8.0