You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
131 lines
4.6 KiB
131 lines
4.6 KiB
3 years ago
|
package com.qs.serve.common.util;
|
||
|
|
||
|
import com.alibaba.fastjson.JSONObject;
|
||
|
import com.qs.serve.common.model.dto.R;
|
||
|
import org.apache.http.HttpEntity;
|
||
|
import org.apache.http.ParseException;
|
||
|
import org.apache.http.client.ClientProtocolException;
|
||
|
import org.apache.http.client.HttpClient;
|
||
|
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.HttpClientBuilder;
|
||
|
import org.apache.http.impl.client.HttpClients;
|
||
|
import org.apache.http.protocol.HTTP;
|
||
|
import org.apache.http.util.EntityUtils;
|
||
|
|
||
|
import java.io.IOException;
|
||
|
import java.util.HashMap;
|
||
|
|
||
|
/**
|
||
|
* @author YenHex
|
||
|
* @since 2022/11/23
|
||
|
*/
|
||
|
public class HttpUtil {
|
||
|
|
||
|
private final static String CONTENT_TYPE = "application/json";
|
||
|
private final static String CHARSET = "UTF-8";
|
||
|
private final static String EN_CONING = "UTF-8";
|
||
|
|
||
|
public static String doPost(String url, String jsonStr, HashMap<String,String> headers) {
|
||
|
CloseableHttpClient client = HttpClientBuilder.create().build();
|
||
|
CloseableHttpResponse response = null;
|
||
|
try {
|
||
|
//设post请求
|
||
|
HttpPost post = new HttpPost(url);
|
||
|
//创建请求实体传参
|
||
|
StringEntity postingString = new StringEntity(jsonStr,CHARSET);
|
||
|
postingString.setContentEncoding(EN_CONING);
|
||
|
//设置post请求参数
|
||
|
post.setEntity(postingString);
|
||
|
post.addHeader(HTTP.CONTENT_TYPE,CONTENT_TYPE);
|
||
|
if (headers != null) {
|
||
|
for (String key : headers.keySet()) {
|
||
|
post.addHeader(key,headers.get(key));
|
||
|
}
|
||
|
}
|
||
|
//执行post请求
|
||
|
response = client.execute(post);
|
||
|
//访问成功状态码为200
|
||
|
if (response.getStatusLine().getStatusCode() == 200) {
|
||
|
return EntityUtils.toString(response.getEntity());
|
||
|
}
|
||
|
} catch (IOException e) {
|
||
|
e.printStackTrace();
|
||
|
}finally {
|
||
|
try {
|
||
|
client.close();
|
||
|
if(response!=null){
|
||
|
response.close();
|
||
|
}
|
||
|
} catch (IOException e) {
|
||
|
e.printStackTrace();
|
||
|
}
|
||
|
}
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
public static String doGet(String url, HashMap<String,String> headers) {
|
||
|
CloseableHttpClient httpclient = HttpClients.createDefault();
|
||
|
try {
|
||
|
HttpGet httpget = new HttpGet(url);
|
||
|
httpget.addHeader(HTTP.CONTENT_TYPE,CONTENT_TYPE);
|
||
|
if (headers != null) {
|
||
|
for (String key : headers.keySet()) {
|
||
|
httpget.addHeader(key,headers.get(key));
|
||
|
}
|
||
|
}
|
||
|
CloseableHttpResponse response = httpclient.execute(httpget);
|
||
|
try {
|
||
|
if (response.getStatusLine().getStatusCode() == 200) {
|
||
|
return EntityUtils.toString(response.getEntity());
|
||
|
}
|
||
|
} finally {
|
||
|
response.close();
|
||
|
}
|
||
|
} catch (ClientProtocolException e) {
|
||
|
e.printStackTrace();
|
||
|
} catch (ParseException e) {
|
||
|
e.printStackTrace();
|
||
|
} catch (IOException e) {
|
||
|
e.printStackTrace();
|
||
|
} finally {
|
||
|
try {
|
||
|
httpclient.close();
|
||
|
} catch (IOException e) {
|
||
|
e.printStackTrace();
|
||
|
}
|
||
|
}
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
public static String createUrl(HashMap<String,String> param){
|
||
|
StringBuilder sb = new StringBuilder();
|
||
|
for (String key : param.keySet()) {
|
||
|
sb.append("&"+key+"="+param.get(key));
|
||
|
}
|
||
|
return sb.toString();
|
||
|
}
|
||
|
|
||
|
public static R<String> doGetResult(String url,HashMap<String,String> headers){
|
||
|
String result = HttpUtil.doGet(url,headers);
|
||
|
JSONObject jsonObject = JSONObject.parseObject(result);
|
||
|
Integer status = jsonObject.getInteger("status");
|
||
|
String msg = jsonObject.getString("msg");
|
||
|
String data = jsonObject.getString("data");
|
||
|
return new R(status,msg,data);
|
||
|
}
|
||
|
|
||
|
public static R<String> doPostResult(String url, String jsonStr, HashMap<String,String> headers){
|
||
|
String result = HttpUtil.doPost(url,jsonStr,headers);
|
||
|
JSONObject jsonObject = JSONObject.parseObject(result);
|
||
|
Integer status = jsonObject.getInteger("status");
|
||
|
String msg = jsonObject.getString("msg");
|
||
|
String data = jsonObject.getString("data");
|
||
|
return new R(status,msg,data);
|
||
|
}
|
||
|
|
||
|
}
|