yujian
2019-08-28 565c0a03a299de5a619f4d5b5fe593e46dbf35dd
utils/src/main/java/org/yeshi/utils/HttpUtil.java
@@ -9,9 +9,7 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.httpclient.Header;
@@ -31,7 +29,6 @@
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.impl.client.BasicCredentialsProvider;
@@ -41,6 +38,7 @@
import org.yeshi.utils.entity.ProxyIP;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class HttpUtil {
@@ -55,12 +53,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 +313,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 +403,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();