189 lines
7.5 KiB
Java
189 lines
7.5 KiB
Java
package com.xkrs.util;
|
||
|
||
import org.apache.http.Consts;
|
||
import org.apache.http.HttpEntity;
|
||
import org.apache.http.NameValuePair;
|
||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||
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.mime.HttpMultipartMode;
|
||
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.BasicNameValuePair;
|
||
import org.apache.http.util.EntityUtils;
|
||
import org.slf4j.Logger;
|
||
import org.slf4j.LoggerFactory;
|
||
|
||
import java.io.File;
|
||
import java.io.IOException;
|
||
import java.nio.charset.StandardCharsets;
|
||
import java.util.*;
|
||
|
||
/**
|
||
* http请求工具
|
||
* @author tajochen
|
||
**/
|
||
public class RequestUtil {
|
||
|
||
private static final String FIREFOX_UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:92.0) Gecko/20100101 Firefox/92.0";
|
||
private static final String CHROME_UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " +
|
||
"(KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36";
|
||
|
||
/**
|
||
* 模拟 get请求
|
||
* @param url 链接
|
||
* @param map 参数列表
|
||
*/
|
||
public static String getStandard(String url, Map<String,String> map) {
|
||
String body = "";
|
||
// 1.拿到一个httpclient的对象
|
||
CloseableHttpClient httpClient = HttpClients.createDefault();
|
||
// 2.1 提交请求体
|
||
ArrayList<NameValuePair> parameters = new ArrayList<>();
|
||
URIBuilder builder = null;
|
||
// 装填参数
|
||
if(map!=null){
|
||
for (Map.Entry<String, String> entry : map.entrySet()) {
|
||
parameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
|
||
}
|
||
}
|
||
CloseableHttpResponse response = null;
|
||
try {
|
||
builder = new URIBuilder(url);
|
||
builder.setParameters(parameters);
|
||
// 2.设置请求方式和请求信息
|
||
HttpGet httpGet = new HttpGet(builder.build());
|
||
// 2.1 提交header头信息
|
||
httpGet.addHeader("user-agent", FIREFOX_UA);
|
||
// 3.执行请求
|
||
response = httpClient.execute(httpGet);
|
||
// 4.获取返回值
|
||
assert response != null;
|
||
// 按指定编码转换结果实体为String类型
|
||
body = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
|
||
// 5.关闭连接
|
||
response.close();
|
||
httpClient.close();
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
}
|
||
return body;
|
||
}
|
||
|
||
/**
|
||
* 模拟Post请求 application/x-www-form-urlencoded
|
||
* @param url 资源地址
|
||
* @param map 参数列表
|
||
* @return
|
||
*/
|
||
public static String postStandard(String url, Map<String,String> map) {
|
||
String body = "";
|
||
// 创建httpclient对象
|
||
CloseableHttpClient client = HttpClients.createDefault();
|
||
// 创建post方式请求对象
|
||
HttpPost httpPost = new HttpPost(url);
|
||
// 装填参数
|
||
List<NameValuePair> nvps = new ArrayList<>();
|
||
if(map!=null){
|
||
for (Map.Entry<String, String> entry : map.entrySet()) {
|
||
nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
|
||
}
|
||
}
|
||
try {
|
||
// 设置参数到请求对象中
|
||
httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
|
||
// 设置header报文头信息
|
||
httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");
|
||
httpPost.setHeader("User-Agent", FIREFOX_UA);
|
||
httpPost.setHeader("Accept", "application");
|
||
httpPost.setHeader("Accept-Encoding", "gzip, deflate");
|
||
// 执行请求操作,并拿到结果(同步阻塞)
|
||
CloseableHttpResponse response = client.execute(httpPost);
|
||
// 获取结果实体
|
||
HttpEntity entity = response.getEntity();
|
||
if (entity != null) {
|
||
// 按指定编码转换结果实体为String类型
|
||
body = EntityUtils.toString(entity, "UTF-8");
|
||
}
|
||
EntityUtils.consume(entity);
|
||
// 释放链接
|
||
response.close();
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
} finally {
|
||
try {
|
||
client.close();
|
||
} catch (IOException e) {
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
return body;
|
||
}
|
||
|
||
/**
|
||
* 模拟Post请求 multipart/form-data
|
||
* @param postFile 请求文件
|
||
* @param postUrl 请求链接
|
||
* @param postParam 请求参数
|
||
* @return
|
||
*/
|
||
public static String postFile(File postFile, String postUrl, Map<String,String> postParam) {
|
||
Logger log = LoggerFactory.getLogger(RequestUtil.class);
|
||
String body = "";
|
||
Map<String,Object> resultMap = new HashMap<>(16);
|
||
CloseableHttpClient httpClient = HttpClients.createDefault();
|
||
try{
|
||
// 把一个普通参数和文件上传给下面这个地址
|
||
HttpPost httpPost = new HttpPost(postUrl);
|
||
|
||
// 设置传输参数
|
||
MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();
|
||
// 解决乱码问题
|
||
multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
|
||
multipartEntity.setCharset(StandardCharsets.UTF_8);
|
||
|
||
// 设置文件参数 ,获取文件名postFile.getName()
|
||
// 把文件转换成流对象FileBody
|
||
multipartEntity.addBinaryBody("file",postFile,ContentType.create("multipart/form-data",
|
||
Consts.UTF_8),postFile.getName());
|
||
// 设计文件以外的参数
|
||
Set<String> keySet = postParam.keySet();
|
||
for (String key : keySet) {
|
||
// 相当于<input type="text" name="name" value=name>
|
||
multipartEntity.addPart(key, new StringBody(postParam.get(key), ContentType.create(
|
||
"multipart/form-data", Consts.UTF_8)));
|
||
}
|
||
HttpEntity reqEntity = multipartEntity.build();
|
||
httpPost.setEntity(reqEntity);
|
||
// 设置header报文头信息
|
||
httpPost.setHeader("User-Agent", FIREFOX_UA);
|
||
httpPost.setHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
|
||
httpPost.setHeader("Accept-Encoding","gzip, deflate");
|
||
|
||
// 发起请求,返回请求的响应
|
||
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
|
||
resultMap.put("statusCode", response.getStatusLine().getStatusCode());
|
||
// 获取响应对象
|
||
HttpEntity resEntity = response.getEntity();
|
||
if (resEntity != null) {
|
||
// 按指定编码转换结果实体为String类型
|
||
body = EntityUtils.toString(resEntity, StandardCharsets.UTF_8);
|
||
}
|
||
// 销毁
|
||
EntityUtils.consume(resEntity);
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
}
|
||
httpClient.close();
|
||
} catch (IOException e1) {
|
||
e1.printStackTrace();
|
||
}
|
||
return body;
|
||
}
|
||
}
|