yujian
2019-09-05 e9a3f108de4f79fe6f246cd30f4427fc6aad5450
utils/src/main/java/org/yeshi/utils/HttpUtil.java
@@ -9,8 +9,11 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.security.KeyStore;
import java.util.Iterator;
import java.util.Map;
import javax.net.ssl.SSLContext;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
@@ -24,6 +27,7 @@
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;
@@ -31,6 +35,9 @@
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.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;
@@ -52,7 +59,7 @@
      return "";
   }
   public static String getShortLink(String url) {
   public static String getShortLink(String url) {
      String shortLink = getShortLink2(url);
      if (StringUtil.isNullOrEmpty(shortLink))
         shortLink = getShortLink3(url);
@@ -76,15 +83,11 @@
   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",
               "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 = JSONObject.fromObject(result);
         JSONArray array = data.optJSONArray("urls");
         data = array.optJSONObject(0);
         String shortUrl = data.optString("url_short");
         if (!StringUtil.isNullOrEmpty(shortUrl))
            return shortUrl;
         JSONObject data = JSONArray.fromObject(result).optJSONObject(0);
         return data.optString("url_short");
      } catch (Exception e) {
         e.printStackTrace();
      }
@@ -94,15 +97,11 @@
   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",
               "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 = JSONObject.fromObject(result);
         JSONArray array = data.optJSONArray("urls");
         data = array.optJSONObject(0);
         String shortUrl = data.optString("url_short");
         if (!StringUtil.isNullOrEmpty(shortUrl))
            return shortUrl;
         JSONObject data = JSONArray.fromObject(result).optJSONObject(0);
         return data.optString("url_short");
      } catch (Exception e) {
         e.printStackTrace();
      }
@@ -321,7 +320,6 @@
      return null;
   }
   public static String postSimple(String url) {
      HttpClient client = new HttpClient();
      PostMethod method = new PostMethod(url);
@@ -335,8 +333,7 @@
      }
      return "";
   }
   public static String post(String url) {
      HttpClient client = new HttpClient();
      PostMethod method = new PostMethod(url);
@@ -543,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;
   }
}