Commit 8a806e9c by songxiang

长短链接转换改为POST请求

parent dcaceac2
package com.pcloud.common.utils.httpclient;
import java.io.IOException;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.pcloud.common.utils.rsa.MD5;
import com.pcloud.common.utils.string.StringUtil;
import org.apache.http.HttpEntity;
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.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.pcloud.common.utils.rsa.MD5;
import com.pcloud.common.utils.string.StringUtil;
import java.io.IOException;
import java.nio.charset.Charset;
/**
* @author:songx
......@@ -35,7 +37,7 @@ public class UrlUtils {
/**
* 请求地址(OWN)
*/
private static final String REQUEST_URL2 = "https://rays.5rs.me/convert/v1.0/url/shortenUrl?originUrl=";
private static final String REQUEST_URL2 = "https://rays.5rs.me/convert/v1.0/url/shorten";
/**
* 获取短链接(新浪)
......@@ -57,7 +59,7 @@ public class UrlUtils {
/**
* 发送请求(新浪)
*
* @param requestUrl
* @param long_url
* @return
*/
private static String callHttp(String long_url) {
......@@ -107,10 +109,18 @@ public class UrlUtils {
return null;
}
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(REQUEST_URL2 + long_url);
HttpPost httpPost = new HttpPost(REQUEST_URL2);
String resContent = null;
try {
CloseableHttpResponse response = httpclient.execute(httpGet);
//设置参数到请求对象中
JSONObject jsonObject = new JSONObject();
jsonObject.put("originUrl", long_url);
StringEntity stringEntity = new StringEntity(jsonObject.toJSONString(), Charset.forName("UTF-8"));
httpPost.setEntity(stringEntity);
//设置header信息
//指定报文头【Content-type】、【User-Agent】
httpPost.setHeader("Content-type", "application/json;charset=UTF-8");
CloseableHttpResponse response = httpclient.execute(httpPost);
// 获取响应实体
HttpEntity entity = response.getEntity();
if (entity != null) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment