| | |
| | | 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;
|
| | |
| | | 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.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;
|
| | |
| | | return "";
|
| | | }
|
| | |
|
| | | public static String getShortLink(String url) { |
| | | public static String getShortLink(String url) {
|
| | | String shortLink = getShortLink2(url);
|
| | | if (StringUtil.isNullOrEmpty(shortLink))
|
| | | shortLink = getShortLink3(url);
|
| | |
| | | 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();
|
| | | }
|
| | |
| | | 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();
|
| | | }
|
| | |
| | | return null;
|
| | | }
|
| | |
|
| | | |
| | | public static String postSimple(String url) {
|
| | | HttpClient client = new HttpClient();
|
| | | PostMethod method = new PostMethod(url);
|
| | |
| | | }
|
| | | return "";
|
| | | }
|
| | | |
| | | |
| | |
|
| | | public static String post(String url) {
|
| | | HttpClient client = new HttpClient();
|
| | | PostMethod method = new PostMethod(url);
|
| | |
| | | }
|
| | | 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;
|
| | | }
|
| | | }
|