Commit 8a806e9c by songxiang

长短链接转换改为POST请求

parent dcaceac2
package com.pcloud.common.utils.httpclient; 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.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet; 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.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils; import org.apache.http.util.EntityUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSONArray; import java.io.IOException;
import com.alibaba.fastjson.JSONObject; import java.nio.charset.Charset;
import com.pcloud.common.utils.rsa.MD5;
import com.pcloud.common.utils.string.StringUtil;
/** /**
* @author:songx * @author:songx
...@@ -35,7 +37,7 @@ public class UrlUtils { ...@@ -35,7 +37,7 @@ public class UrlUtils {
/** /**
* 请求地址(OWN) * 请求地址(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 { ...@@ -57,7 +59,7 @@ public class UrlUtils {
/** /**
* 发送请求(新浪) * 发送请求(新浪)
* *
* @param requestUrl * @param long_url
* @return * @return
*/ */
private static String callHttp(String long_url) { private static String callHttp(String long_url) {
...@@ -107,10 +109,18 @@ public class UrlUtils { ...@@ -107,10 +109,18 @@ public class UrlUtils {
return null; return null;
} }
CloseableHttpClient httpclient = HttpClients.createDefault(); CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(REQUEST_URL2 + long_url); HttpPost httpPost = new HttpPost(REQUEST_URL2);
String resContent = null; String resContent = null;
try { 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(); HttpEntity entity = response.getEntity();
if (entity != null) { 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