package com.dhtech.trumpet.common;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
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.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.util.EntityUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author syp
* @className com.dhtech.trumpet.common.HttpClientService
* @date 2021/07/13
* @Version 1.0
* @describe:
*/
@Slf4j
public class HttpClientService {
/**
* 返回成功状态码
*/
private static final int SUCCESS_CODE = 200;
/**
* 发送GET请求
*
* @param url 请求url
* @param nameValuePairList 请求参数
* @return JSON或者字符串
* @throws Exception
*/
public static String sendGet(String url, List<NameValuePair> nameValuePairList) throws Exception {
CloseableHttpClient client = null;
CloseableHttpResponse response = null;
try {
client = HttpClients.createDefault();
URIBuilder uriBuilder = new URIBuilder(url);
uriBuilder.addParameters(nameValuePairList);
HttpGet httpGet = new HttpGet(uriBuilder.build());
httpGet.setHeader(new BasicHeader("Content-Type", "application/json; charset=utf-8"));
httpGet.setHeader(new BasicHeader("Accept", "application/json;charset=utf-8"));
response = client.execute(httpGet);
int statusCode = response.getStatusLine().getStatusCode();
if (SUCCESS_CODE == statusCode) {
HttpEntity entity = response.getEntity();
/**
* 通过EntityUitls获取返回内容
*/
String result = EntityUtils.toString(entity, "UTF-8");
return result;
} else {
log.error("HttpClientService-line: {}, errorMsg{}", 97, "GET请求失败!");
}
} catch (Exception e) {
log.error("HttpClientService-line: {}, Exception: {}", 100, e);
} finally {
response.close();
client.close();
}
return null;
}
/**
* 发送POST请求
*
* @param url
* @param file
* @return java.lang.Object
* @author xianghl
* @date 2021/7/14 9:21
*/
public static Object sendPost(String url, File file) throws Exception {
CloseableHttpClient httpClient = HttpClients.createDefault();
String result = "";
try {
String fileName = file.getName();
String base64 = file2Base64(file);
HttpPost httpPost = new HttpPost(url);
Map<String, String> map = new HashMap<>();
map.put("fileName", fileName);
map.put("fileData", base64);
StringEntity s = new StringEntity(JSON.toJSONString(map));
s.setContentEncoding("UTF-8");
//发送json数据需要设置contentType
s.setContentType("application/json");
httpPost.setEntity(s);
// 执行提交
HttpResponse response = httpClient.execute(httpPost);
HttpEntity responseEntity = response.getEntity();
if (responseEntity != null) {
// 将响应内容转换为字符串
result = EntityUtils.toString(responseEntity, Charset.forName("UTF-8"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
httpClient.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
public static String file2Base64(File file) {
if (file == null) {
return null;
}
String base64 = null;
FileInputStream fin = null;
try {
fin = new FileInputStream(file);
byte[] buff = new byte[fin.available()];
fin.read(buff);
base64 = Base64.encode(buff);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fin != null) {
try {
fin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return base64;
}
}
CloseableHttpClient 发送数据请求
最新推荐文章于 2024-04-26 21:11:13 发布