package org.jeecg.modules.utils;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.codec.binary.Base64;
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.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.util.EntityUtils;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Map;
import java.util.Set;
/**
* @author feilong
* @date 2023/8/17
*/
public class WXHttpUtil {
public static String doPostJson(String url, String json, String token) {
// 创建Httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String resultString = "";
try {
// 创建Http Post请求
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Authorization", token);
// 创建请求内容
StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
System.out.println(entity.toString());
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) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return resultString;
}
public static String sendGet(String url, Map<String,String> headMap){
// 创建Httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String resultString = "";
try {
// 创建Http get请求
HttpGet httpGet = new HttpGet(url);
if(!headMap.isEmpty()){
Set<String> nameSet = headMap.keySet();
for(String name:nameSet){
httpGet.addHeader(name,headMap.get(name));
}
}
// 执行http请求
response = httpClient.execute(httpGet);
resultString = EntityUtils.toString(response.getEntity(), "utf-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return resultString;
}
public static String sendGet(String url){
// 创建Httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String resultString = "";
try {
// 创建Http get请求
HttpGet httpGet = new HttpGet(url);
// 执行http请求
response = httpClient.execute(httpGet);
resultString = EntityUtils.toString(response.getEntity(), "utf-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return resultString;
}
public static byte[] getFileBytes(String url){
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
byte[] data = null;
try {
// 创建Http get请求
HttpGet httpGet = new HttpGet(url);
// 执行http请求
response = httpClient.execute(httpGet);
InputStream in = response.getEntity().getContent();
ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
byte[] b = new byte[1000];
int n;
while ((n = in.read(b)) != -1) {
bos.write(b, 0, n);
}
in.close();
data = bos.toByteArray();
bos.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return data;
}
public static JSONObject decryptFile(String url, String sEncodingAESKey){
byte[] data = getFileBytes(url);
byte[] key = Base64.decodeBase64(sEncodingAESKey + "=");
byte[] original = null;
try {
// 设置解密模式为AES的CBC模式
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
IvParameterSpec iv = new IvParameterSpec(Arrays.copyOfRange(key, 0, 16));
cipher.init(Cipher.DECRYPT_MODE, keySpec, iv);
original = cipher.doFinal(data);
} catch (Exception e) {
e.printStackTrace();
}
assert original != null;
String result = new String(original);
JSONObject jsonObject = JSONObject.parseObject(result);
return jsonObject;
}
}
HttpClient工具类
最新推荐文章于 2024-08-23 15:54:50 发布