From 33bfa1b3d03236922cc95b9435d7d10c91e8c4e5 Mon Sep 17 00:00:00 2001
From: yujian <yujian@123.com>
Date: 星期四, 22 八月 2019 09:22:59 +0800
Subject: [PATCH] 明细

---
 utils/src/main/java/org/yeshi/utils/HttpUtil.java |  117 +++++++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 110 insertions(+), 7 deletions(-)

diff --git a/utils/src/main/java/org/yeshi/utils/HttpUtil.java b/utils/src/main/java/org/yeshi/utils/HttpUtil.java
index 8f60fd8..d1fbf3f 100644
--- a/utils/src/main/java/org/yeshi/utils/HttpUtil.java
+++ b/utils/src/main/java/org/yeshi/utils/HttpUtil.java
@@ -24,9 +24,21 @@
 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.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.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpPost;
+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 {
 
@@ -40,13 +52,59 @@
 		return "";
 	}
 
-	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");
+	public static String getShortLink(String url) {
+		String shortLink = getShortLink2(url);
+		if (StringUtil.isNullOrEmpty(shortLink))
+			shortLink = getShortLink3(url);
+		if (StringUtil.isNullOrEmpty(shortLink))
+			shortLink = getShortLink1(url);
+		return shortLink;
+	}
+
+	private static String getShortLink1(String url) {
+		try {
+			String totalUrl = String.format("http://api.suolink.cn/api.php?url=%s", URLEncoder.encode(url));
+			String result = get(totalUrl, null);
+			if (!StringUtil.isNullOrEmpty(result) && result.startsWith("http"))
+				return result.trim();
+		} catch (Exception e) {
+			return url;
+		}
+		return null;
+	}
+
+	private static String getShortLink2(String url) {
+		try {
+			String totalUrl = String.format(
+					"https://api.weibo.com/2/short_url/shorten.json?source=3403499693&url_long=%s",
+					URLEncoder.encode(url));
+			String result = get(totalUrl, null);
+			JSONObject data = JSONObject.fromObject(result);
+			JSONArray array = data.optJSONArray("urls");
+			data = array.optJSONObject(0);
+			String shortUrl = data.optString("url_short");
+			if (!StringUtil.isNullOrEmpty(shortUrl))
+				return shortUrl;
+		} catch (Exception e) {
+			e.printStackTrace();
+		}
+		return null;
+	}
+
+	private static String getShortLink3(String url) {
+		try {
+			String totalUrl = String.format(
+					"https://api.weibo.com/2/short_url/shorten.json?source=2963429064&url_long=%s",
+					URLEncoder.encode(url));
+			String result = get(totalUrl, null);
+			JSONObject data = JSONObject.fromObject(result);
+			JSONArray array = data.optJSONArray("urls");
+			data = array.optJSONObject(0);
+			String shortUrl = data.optString("url_short");
+			if (!StringUtil.isNullOrEmpty(shortUrl))
+				return shortUrl;
+		} catch (Exception e) {
+			e.printStackTrace();
 		}
 		return null;
 	}
@@ -100,6 +158,7 @@
 		HttpClient client = new HttpClient();
 		if (ipInfo != null)
 			client.getHostConfiguration().setProxy(ipInfo.getIp(), ipInfo.getPort());
+		client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
 		try {
 			GetMethod method = new GetMethod(url);
 			if (headers != null) {
@@ -117,6 +176,33 @@
 			e.printStackTrace();
 		}
 		return "";
+	}
+
+	public static String get(String url, Map<String, String> headers, ProxyIP ipInfo, String userName,
+			String proxyPwd) {
+		CloseableHttpClient httpclient = null;
+		if (!StringUtil.isNullOrEmpty(userName)) {
+			CredentialsProvider credsProvider = new BasicCredentialsProvider();
+			credsProvider.setCredentials(new AuthScope(ipInfo.getIp(), ipInfo.getPort()),
+					new UsernamePasswordCredentials(userName, proxyPwd));
+			httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
+		} else
+			HttpClients.createDefault();
+		HttpHost proxy = null;
+		if (ipInfo.getPort() != 0 && ipInfo.getPort() != 80) {
+			proxy = new HttpHost(ipInfo.getIp(), ipInfo.getPort());
+		} else
+			proxy = new HttpHost(ipInfo.getIp());
+		RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
+		HttpPost post = new HttpPost(url);
+		try {
+			post.setConfig(config);
+			CloseableHttpResponse response = httpclient.execute(post);
+			return EntityUtils.toString(response.getEntity());
+		} catch (Exception e) {
+			return null;
+		}
+
 	}
 
 	public static String getAsString(String url, String fromCharset, String toCharset) {
@@ -235,6 +321,22 @@
 		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);
@@ -311,6 +413,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);
 		PostMethod pm = new PostMethod(url);// 鍒涘缓HttpPost瀵硅薄
 		NameValuePair[] ns = new NameValuePair[map.keySet().size()];
 		Iterator<String> its = map.keySet().iterator();

--
Gitblit v1.8.0