From e9a3f108de4f79fe6f246cd30f4427fc6aad5450 Mon Sep 17 00:00:00 2001 From: yujian <yujian@123.com> Date: 星期四, 05 九月 2019 18:00:06 +0800 Subject: [PATCH] Merge branch 'div' of ssh://193.112.35.168:29418/fanli-server into div --- utils/src/main/java/org/yeshi/utils/HttpUtil.java | 115 +++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 files changed, 106 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 491f9f9..98cf891 100644 --- a/utils/src/main/java/org/yeshi/utils/HttpUtil.java +++ b/utils/src/main/java/org/yeshi/utils/HttpUtil.java @@ -9,10 +9,11 @@ import java.net.MalformedURLException; import java.net.URL; import java.net.URLEncoder; -import java.util.ArrayList; +import java.security.KeyStore; import java.util.Iterator; -import java.util.List; import java.util.Map; + +import javax.net.ssl.SSLContext; import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.HttpClient; @@ -26,14 +27,17 @@ 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; @@ -41,6 +45,7 @@ import org.yeshi.utils.entity.ProxyIP; import net.sf.json.JSONArray; +import net.sf.json.JSONObject; public class HttpUtil { @@ -55,12 +60,50 @@ } 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"); + 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( + "http://api.t.sina.com.cn/short_url/shorten.json?source=3403499693&url_long=%s", + URLEncoder.encode(url)); + String result = get(totalUrl, null); + JSONObject data = JSONArray.fromObject(result).optJSONObject(0); + return data.optString("url_short"); + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + private static String getShortLink3(String url) { + try { + String totalUrl = String.format( + "http://api.t.sina.com.cn/short_url/shorten.json?source=2963429064&url_long=%s", + URLEncoder.encode(url)); + String result = get(totalUrl, null); + JSONObject data = JSONArray.fromObject(result).optJSONObject(0); + return data.optString("url_short"); + } catch (Exception e) { + e.printStackTrace(); } return null; } @@ -277,6 +320,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,6 +410,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(); @@ -482,4 +540,43 @@ } 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; + } } -- Gitblit v1.8.0