接口API http://suo.im/
package com.gbcom.gidata.util;
import java.util.HashMap;
import java.util.Map;
/**
* TODO(工具類:藉助第三方,將長連接轉換爲短連接)
* <p>
*
*/
public class ShortUrlUtil {
// http://suo.im/api.htm?
// url=urlencode('要縮短的網址')&
// key=5d26a7a5b1a9c76cebd798e9@g244bad85b6b80baf183867585c7c6bfe&
// expireDate=2019-03-31
public static String generateShortUrl(String url,String expireDate){
Map<String,String> map = new HashMap<>();
map.put("url",url);
map.put("expireDate",expireDate);
map.put("key","5d26a7a5b1a9c76cebd798e9@g244bad85b6b80baf183867585c7c6bfe");
String result = HttpClientUtil.doGet("http://suo.im/api.htm",map);
return result;
}
public static void main(String[] args) {
System.out.println(generateShortUrl("http://localhost:9528/#/login","2019-07-21"));
//http://suo.im/5ep5Yz
}
}
若調用失敗,可能是開發者key失效,可以登錄http://suo.im/,添加自己的key。
調用接口工具類
package com.gbcom.gidata.util;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.http.Consts;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
/**
* @author chenhang
* @date 2019年5月16日
*
*/
public class HttpClientUtil {
/**
* 帶參數的get請求
* @param url
* @param param
* @return String
*/
public static String doGet(String url, Map<String, String> param) {
// 創建Httpclient對象
CloseableHttpClient httpclient = HttpClients.createDefault();
String resultString = "";
CloseableHttpResponse response = null;
try {
// 創建uri
URIBuilder builder = new URIBuilder(url);
if (param != null) {
for (String key : param.keySet()) {
builder.addParameter(key, param.get(key));
}
}
URI uri = builder.build();
// 創建http GET請求
HttpGet httpGet = new HttpGet(uri);
// 執行請求
response = httpclient.execute(httpGet);
// 判斷返回狀態是否爲200
if (response.getStatusLine().getStatusCode() == 200) {
resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (response != null) {
response.close();
}
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return resultString;
}
/**
* 不帶參數的get請求
* @param url
* @return String
*/
public static String doGet(String url) {
return doGet(url, null);
}
/**
* 帶參數的post請求
* @param url
* @param param
* @return String
*/
public static String doPost(String url, Map<String, String> param) {
// 創建Httpclient對象
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String resultString = "";
try {
// 創建Http Post請求
HttpPost httpPost = new HttpPost(url);
// 創建參數列表
if (param != null) {
List<NameValuePair> paramList = new ArrayList<>();
for (String key : param.keySet()) {
paramList.add(new BasicNameValuePair(key, param.get(key)));
}
// 模擬表單
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList,Consts.UTF_8);
httpPost.setEntity(entity);
}
// 執行http請求
response = httpClient.execute(httpPost);
resultString = EntityUtils.toString(response.getEntity(), "utf-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return resultString;
}
/**
* 不帶參數的post請求
* @param url
* @return String
*/
public static String doPost(String url) {
return doPost(url, null);
}
/**
* 傳送json類型的post請求
* @param url
* @param json
* @return String
*/
public static String doPostJson(String url, String json) {
// 創建Httpclient對象
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String resultString = "";
try {
// 創建Http Post請求
HttpPost httpPost = new HttpPost(url);
// 創建請求內容
StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
httpPost.setEntity(entity);
// 執行http請求
response = httpClient.execute(httpPost);
resultString = EntityUtils.toString(response.getEntity(), "utf-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return resultString;
}
}