public static DefaultHttpClient httpclient;
static {
httpclient = new DefaultHttpClient();
//httpclient = (DefaultHttpClient) HttpClientConnectionManager.getSSLInstance(httpclient); // 接受任何证书的浏览器客户端
}
/**
* 生成短连接信息
*
* @author: zhaojinhui
* @date: 2015年11月11日下午11:11:11
*/
public static String generateShortUrl(String url) {
try {
HttpPost httpost = new HttpPost("http://dwz.cn/create.php");
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("url", url)); // 用户名称
httpost.setEntity(new UrlEncodedFormEntity(params, "utf-8"));
CloseableHttpResponse response = httpclient.execute(httpost);
String jsonStr = EntityUtils.toString((response).getEntity(), "utf-8");
System.out.println(jsonStr);
JSONObject object = JSON.parseObject(jsonStr);
System.out.println(object.getString("tinyurl"));
return object.getString("tinyurl");
} catch (Exception e) {
e.printStackTrace();
return "Error";
}
}