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 |   74 ++++++++++++++++++++++++++++++++----
 1 files changed, 65 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 9449393..161c024 100644
--- a/utils/src/main/java/org/yeshi/utils/HttpUtil.java
+++ b/utils/src/main/java/org/yeshi/utils/HttpUtil.java
@@ -10,8 +10,12 @@
 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;
 
@@ -48,6 +52,21 @@
 
 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 {
 			URL url = new URL(host);
@@ -59,13 +78,13 @@
 	}
 
 	public static String getShortLink(String url) {
-		return url;
-//		 String shortLink = getShortLink3(url);
-//		 if (StringUtil.isNullOrEmpty(shortLink))
-//		 shortLink = getShortLink1(url);
-//		 if (StringUtil.isNullOrEmpty(shortLink))
-//		 shortLink = getShortLink2(url);
-//		 return shortLink;
+		// 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) {
@@ -101,8 +120,7 @@
 
 	private static String getShortLink3(String url) {
 		try {
-			String totalUrl = String.format(
-					"http://h5.flq001.com/short/createShort?url=%s",
+			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);
@@ -475,6 +493,24 @@
 		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);
+			return convertInputStreamToString(method.getResponseBodyAsStream());
+		} catch (HttpException e) {
+			e.printStackTrace();
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+		return "";
+	}
+
 	public static String postInputStream(String url, File file) throws FileNotFoundException {
 		HttpClient client = new HttpClient();
 		PostMethod postMethod = new PostMethod(url);
@@ -584,4 +620,24 @@
 
 		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