Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
cn.net.wanmo.common.http.HttpReq Maven / Gradle / Ivy
package cn.net.wanmo.common.http;
import cn.net.wanmo.common.charset.CharsetUtil;
import cn.net.wanmo.common.http.enums.Method;
import cn.net.wanmo.common.http.enums.ResType;
import cn.net.wanmo.common.http.pojo.ResData;
import cn.net.wanmo.common.http.pojo.ResObj;
import cn.net.wanmo.common.http.pojo.UploadFile;
import cn.net.wanmo.common.http.pojo.Uploader;
import cn.net.wanmo.common.http.ssl.NullHostNameVerifier;
import cn.net.wanmo.common.http.ssl.SSLContextFactory;
import cn.net.wanmo.common.http.util.ResUtil;
import cn.net.wanmo.common.result.HttpResult;
import cn.net.wanmo.common.util.*;
import com.alibaba.fastjson.JSON;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.net.ssl.HttpsURLConnection;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;
import java.util.*;
/**
* HTTP 请求
*/
public class HttpReq {
private Logger logger = LoggerFactory.getLogger(getClass());
final String CRLF = "\r\n";
final String TWO_HYPHEN = "--";
final String BOUNDARY = "----WebKitFormBoundary" + IdGen.uuid();
/**
* 创建请求
*/
public static HttpReq create() {
return new HttpReq();
}
/**
* 创建请求
*/
public static HttpReq get() {
HttpReq req = create();
req.setMethod(Method.get);
return req;
}
/**
* 创建请求
*/
public static HttpReq post() {
HttpReq req = create();
req.setMethod(Method.post);
req.resJson();
return req;
}
/**
* 发送请求
*/
public HttpResult send() {
HttpResult result;
switch (resType) {
case file:
result = sendResAsFile(this.url, this.method, this.header, this.body, this.params, this.uploaderList);
break;
default:
result = sendResAsString(this.url, this.method, this.header, this.body, this.params, this.uploaderList);
}
return result;
}
/**
* 发送请求
*/
public HttpResult> send(Obj obj) {
HttpResult> res = new HttpResult<>();
{
HttpResult r;
switch (resType) {
case file:
r = sendResAsFile(this.url, this.method, this.header, this.body, this.params, this.uploaderList);
break;
default:
r = sendResAsString(this.url, this.method, this.header, this.body, this.params, this.uploaderList);
}
{
res.setCode(r.getCode());
res.setMsg(r.getMsg());
res.setStartTime(r.getStartTime());
res.setConsumeTime(r.getConsumeTime());
res.setDesc(r.getDesc());
}
{
ResData data = r.getData();
obj.parse(data.getBody());
data.setObj(obj);
res.setData(data);
}
}
return res;
}
// ---------------------===============---------------------------
/**
* 响应 json 数据
*/
public HttpReq resJson() {
// 代表发送端(客户端|服务器)发送的实体数据的数据类型
this.header.put("Content-Type", "application/json; charset=UTF-8");
// 代表发送端(客户端)希望接受的数据类型
this.header.put("accept", "application/json");
return this;
}
// ---------------------===============---------------------------
/**
* 请求URL地址
*/
private String url;
/**
* 设置连接主机超时
*/
private Integer connectTimeout = 3000;
/**
* 设置从主机读取数据超时
*/
private Integer readTimeout = 10000;
/**
* 响应解析,默认编码
*/
private Charset charset = CharsetUtil.UTF8;
/**
* 请求方式
*/
private Method method = Method.get;
/**
* 请求头信息
*/
private Map header = new HashMap<>();
/**
* 请求参数
*/
private Map params = new HashMap<>();
/**
* 请求体
*/
private String body = StringUtil.EMPTY;
/**
* 上传文件
*/
private List uploaderList = new ArrayList<>();
/**
* 是否 multipart
*/
private boolean isMultipart = false;
/**
* HTTP 请求,响应的数据
*/
private ResType resType = ResType.string;
/**
* HTTPS 服务器证书文件
*/
private String certFilePath = "";
/**
* HTTPS 服务器证书密码
*/
private String certFilePassword = "";
public HttpReq() {
}
public String getUrl() {
return url;
}
public HttpReq setUrl(String url) {
this.url = url;
return this;
}
public Integer getConnectTimeout() {
return connectTimeout;
}
public HttpReq setConnectTimeout(Integer connectTimeout) {
this.connectTimeout = connectTimeout;
return this;
}
public Integer getReadTimeout() {
return readTimeout;
}
public HttpReq setReadTimeout(Integer readTimeout) {
this.readTimeout = readTimeout;
return this;
}
public Charset getCharset() {
return charset;
}
public HttpReq setCharset(Charset charset) {
this.charset = charset;
return this;
}
public Method getMethod() {
return method;
}
public HttpReq setMethod(Method method) {
this.method = method;
return this;
}
public Map getHeader() {
return header;
}
public HttpReq setHeader(Map header) {
this.header = header;
return this;
}
public Map getParams() {
return params;
}
public HttpReq setParams(Map params) {
this.params = params;
return this;
}
public String getBody() {
return body;
}
public HttpReq setBody(String body) {
this.body = body;
return this;
}
public List getUploaderList() {
return uploaderList;
}
public HttpReq setUploaderList(List uploaderList) {
this.uploaderList = uploaderList;
this.isMultipart = true;
return this;
}
public ResType getResType() {
return resType;
}
public HttpReq setResType(ResType resType) {
this.resType = resType;
return this;
}
public String getCertFilePath() {
return certFilePath;
}
public HttpReq setCertFilePath(String certFilePath) {
this.certFilePath = certFilePath;
return this;
}
public String getCertFilePassword() {
return certFilePassword;
}
public HttpReq setCertFilePassword(String certFilePassword) {
this.certFilePassword = certFilePassword;
return this;
}
// ---------------------======= 发起请求 解析响应 ========---------------------------
/**
* 发起 HTTP 请求并获取结果
*
* @param url 请求地址
* @param method 请求方式(GET、POST)
* @param header 通用的请求属性
* @param reqBody 请求体
* @param uploaderList 上传文件
* @return 响应的字符串
*/
private HttpResult sendResAsString(String url, Method method, Map header, String reqBody, Map params, List uploaderList) {
HttpResult result = new HttpResult();
HttpURLConnection conn = null;
try {
conn = getConnection();
result.setCode(conn.getResponseCode());
result.setMsg("Http请求完成");
String charset = ResUtil.getResponseCharset(conn.getContentType());
String resBody = ResUtil.getResAsString(conn.getInputStream(), charset);
Map> headerFields = conn.getHeaderFields();
result.setData(ResData.build(resBody, headerFields));
logger.debug("响应头: {}", headerFields);
logger.debug("响应数据: {}", resBody);
} catch (Exception e) {
result.error("Http请求发生异常: " + e.getMessage());
logger.error("Http请求发生异常: " + url, e);
} finally {
if (conn != null) {
conn.disconnect();
}
}
logger.debug("响应结果:{} {}", result.getCode(), result.getMsg());
return result;
}
/**
* 发起 HTTP 请求并获取结果
*
* @param url 请求地址
* @param method 请求方式(GET、POST)
* @param header 通用的请求属性
* @param reqBody 请求体
* @param uploaderList 上传文件
* @return 响应的文件
*/
private HttpResult sendResAsFile(String url, Method method, Map header, String reqBody, Map params, List uploaderList) {
HttpResult result = new HttpResult();
HttpURLConnection conn = null;
long startTime = DateUtil.nowLong();
try {
conn = getConnection();
result.setCode(conn.getResponseCode());
result.setMsg("Http请求完成");
InputStream is = conn.getInputStream(); // 请求后远程返回的数据
File file = ResUtil.getResAsFile(is);
Map> headerFields = conn.getHeaderFields();
ResData resData = ResData.build(file, headerFields);
resData.setBody(file.getAbsolutePath());
result.setData(resData);
logger.debug("响应数据: {}", result.getData());
} catch (Exception e) {
result.error("Http请求发生异常: " + e.getMessage());
logger.error("Http请求发生异常: " + url, e);
} finally {
if (conn != null) {
conn.disconnect();
}
}
result.setConsumeTime(DateUtil.nowLong() - startTime);
logger.debug("响应结果:代码 {} , 消息 {}", result.getCode(), result.getMsg());
return result;
}
// ---------------------======= 初始化连接 ========---------------------------
private void initConnection(HttpURLConnection conn) {
{ // 设置超时
conn.setConnectTimeout(this.connectTimeout); // 设置连接主机超时(单位:毫秒)
conn.setReadTimeout(this.readTimeout); // 设置从主机读取数据超时(单位:毫秒)
}
{ // 设置常规属性
/*
* 设置请求头或响应头:HTTP请求允许一个key带多个用逗号分开的values,但是HttpURLConnection只提供了单个操作的方法:
* setRequestProperty(key,value):会覆盖已经存在的key的所有values,有清零重新赋值的作用
* addRequestProperty(key,value):是在原来key的基础上继续添加其他value
*/
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
conn.setRequestProperty("accept", "*/*"); // 设置接受的文件类型,*表示一切可以接受的
conn.setRequestProperty("Accept-Charset", "UTF-8"); // 新添加的请求头编码
// conn.setRequestProperty("connection", "Keep-Alive"); // 设置维持长连接
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 设定传送的内容类型是可序列化的java对象
// (如果不设此项,在传送序列化对象时,当WEB服务默认的不是这种类型时可能抛java.io.EOFException)
// conn.setRequestProperty("Content-type", "application/x-java-serialized-object");
}
if (MapUtil.isNotEmpty(this.header)) { // 属性覆盖或新增
for (Map.Entry entry : this.header.entrySet()) {
conn.setRequestProperty(entry.getKey(), entry.getValue());
}
}
}
private void initGet(HttpURLConnection conn) throws Exception {
// 设置请求方式 GET
conn.setRequestMethod(Method.get.getValue());
// 设置是否使用缓存 默认是true
conn.setUseCaches(true);
}
private void initPost(HttpURLConnection conn) throws Exception {
// 设置请求方式 POST
conn.setRequestMethod(Method.post.getValue());
// 设置是否从 httpUrlConnection 允许读入,默认情况下是 true;
conn.setDoInput(true);
// 默认情况下是 false; 设置是否向 httpUrlConnection 允许输出,因为这个是 post 请求,参数要放在 HTTP 正文内,因此需要设为 true,
conn.setDoOutput(true);
// Post 请求不能使用缓存
conn.setUseCaches(false);
if (isMultipart == false) {
if (MapUtil.isNotEmpty(params)) {
{ // 请求开始
conn.setRequestProperty("Content-Type", "multipart/form-data; charset=UTF-8; boundary=" + BOUNDARY); // 定义数据分隔线
conn.setRequestProperty("Content-Length", String.valueOf(BOUNDARY.length())); // 设置 boundary 的长度
}
OutputStream out = conn.getOutputStream(); // 获取输出流
for (Map.Entry entry : this.params.entrySet()) {
StringBuilder paramData = new StringBuilder();
paramData.append(TWO_HYPHEN).append(BOUNDARY).append(CRLF);
paramData.append("Content-Disposition: form-data; name=" + entry.getKey()).append(CRLF);
paramData.append("Content-Type: text/plain; charset=utf-8").append(CRLF);
paramData.append("Content-Transfer-Encoding: 8bit").append(CRLF);
paramData.append(CRLF); // 参数头设置完以后需要两个换行,然后才是参数内容
paramData.append(entry.getValue());
paramData.append(CRLF);
String paramDataStr = paramData.toString();
out.write(paramDataStr.getBytes());
out.flush();
logger.debug("isMultipart paramData:\r\n {}", paramDataStr);
}
{ // 请求截止
byte[] end_data = (TWO_HYPHEN + BOUNDARY + TWO_HYPHEN + CRLF).getBytes();
out.write(end_data);
out.flush();
IOUtils.closeQuietly(out);
}
}
if (StringUtil.isNotBlank(this.body)) { // 如果请求体不为空
byte[] paramsBytes = this.body.getBytes(this.charset);
OutputStream out = conn.getOutputStream(); // 获取输出流
{ // 当有数据需要提交时,请求参数传给服务器
{ // 方式一
out.write(paramsBytes);
out.flush();
}
{ // 方式二
// DataOutput dataOutput = new DataOutputStream(conn.getOutputStream());
// dataOutput.write(paramsBytes);
}
}
{ // 请求截止
byte[] end_data = (TWO_HYPHEN + BOUNDARY + TWO_HYPHEN + CRLF).getBytes();
out.write(end_data);
out.flush();
IOUtils.closeQuietly(out);
}
}
}
if (isMultipart == true) {
{ // 请求开始
conn.setRequestProperty("Content-Type", "multipart/form-data; charset=UTF-8; boundary=" + BOUNDARY); // 定义数据分隔线
conn.setRequestProperty("Content-Length", String.valueOf(BOUNDARY.length())); // 设置 boundary 的长度
conn.setRequestProperty("Connection", "Keep-Alive");
}
OutputStream out = conn.getOutputStream(); // 获取输出流
{ // 表单参数
{
if (MapUtil.isEmpty(params)) {
params = new HashMap<>();
}
params.putIfAbsent("description", "文件上传");
}
for (Map.Entry entry : this.params.entrySet()) {
StringBuilder paramData = new StringBuilder();
paramData.append(TWO_HYPHEN).append(BOUNDARY).append(CRLF);
paramData.append("Content-Disposition: form-data; name=" + entry.getKey()).append(CRLF);
paramData.append("Content-Type: text/plain; charset=utf-8").append(CRLF);
paramData.append("Content-Transfer-Encoding: 8bit").append(CRLF);
paramData.append(CRLF); // 参数头设置完以后需要两个换行,然后才是参数内容
paramData.append(entry.getValue());
paramData.append(CRLF);
String paramDataStr = paramData.toString();
out.write(paramDataStr.getBytes());
out.flush();
logger.debug("isMultipart paramData:\r\n {}", paramDataStr);
}
}
for (Uploader uploader : this.uploaderList) { // 文件参数
String name = uploader.getName();
List files = uploader.getFiles();
for (UploadFile uploadFile : files) {
{ // 开始构建 multipart/form-data 格式的数据包
StringBuilder mediaData = new StringBuilder();
mediaData.append(TWO_HYPHEN).append(BOUNDARY).append(CRLF);
mediaData.append("Content-Disposition: form-data; name=" + name + "; filename=" + uploadFile.getFilename() + "; filelength=" + uploadFile.getFile().length() + "; digest=" + uploadFile.getDigest() + CRLF); //
String contentType = URLConnection.guessContentTypeFromName(uploadFile.getFile().getName());
mediaData.append("Content-Type:" + (StringUtil.isBlank(contentType) ? "application/octet-stream" : contentType) + "; charset=UTF-8" + CRLF);
mediaData.append("Content-Transfer-Encoding: binary" + CRLF);
mediaData.append(CRLF); // 参数头设置完以后需要两个换行,然后才是参数内容
String mediaDataStr = mediaData.toString();
out.write(mediaDataStr.getBytes());
out.flush();
logger.debug("isMultipart mediaData: \r\n {}", mediaDataStr);
}
{ // 文件上传
DataInputStream fs = new DataInputStream(new FileInputStream(uploadFile.getFile()));
int bytes = 0;
byte[] bufferOut = new byte[1024];
while ((bytes = fs.read(bufferOut)) != -1) {
out.write(bufferOut, 0, bytes);
}
out.flush();
IOUtils.closeQuietly(fs);
}
{ // 换行
byte[] end_data = (CRLF).getBytes();
out.write(end_data);
out.flush();
}
}
}
{ // 请求截止
byte[] end_data = (TWO_HYPHEN + BOUNDARY + TWO_HYPHEN + CRLF).getBytes();
out.write(end_data);
out.flush();
}
{
IOUtils.closeQuietly(out);
}
}
}
// ---------------------======= 根据 http https 获取连接 ========---------------------------
private HttpURLConnection getConnection() throws Exception {
logger.debug("请求URL: {}", this.url);
logger.debug("请求方式: {}", this.method);
logger.debug("请求头: {}", JSON.toJSONString(this.header, true));
logger.debug("请求数据: {}", this.body);
logger.debug("请求文件: {}", JSON.toJSONString(this.uploaderList, true));
URL url = new URL(this.url);
HttpURLConnection conn = null;
if ("HTTPS".equals(url.getProtocol().toUpperCase())) { // 如果是安全连接则需要
conn = getConnectionHttps(url);
} else {
conn = getConnectionHttp(url);
}
initConnection(conn);
switch (method) { // 根据请求方式,初始化 conn
case get:
initGet(conn);
break;
case post:
initPost(conn);
break;
default:
throw new RuntimeException("不支持的请求方式:" + method);
}
// conn.connect();
return conn;
}
/**
* 获取 http 连接
*
* @param url
* @return
* @throws Exception
*/
private HttpURLConnection getConnectionHttp(URL url) throws Exception {
return (HttpURLConnection) url.openConnection();
}
/**
* 获取 https 连接
*
* @param url
* @return
* @throws Exception
*/
private HttpsURLConnection getConnectionHttps(URL url) throws Exception {
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setHostnameVerifier(new NullHostNameVerifier());
// connectionHttps.setSSLSocketFactory(getSSLSocket());
conn.setSSLSocketFactory(SSLContextFactory.createSSLContext(this.certFilePath, this.certFilePassword).getSocketFactory());
return conn;
}
// ---------------------======= 工具方法 ========---------------------------
/**
* 将map转为 参数字符串:name1=value1&name2=value2 的形式
*
* @param param
* @return 字符串
* @throws UnsupportedEncodingException
*/
public static String toParamStr(Map param) throws UnsupportedEncodingException {
String reStr = "";
if (MapUtil.isEmpty(param)) {
return reStr;
}
Set> entrySet = param.entrySet();
for (Map.Entry o : entrySet) {
if (o.getValue() == null || "null".equals(o.getValue()) || "class".equals(o.getKey())) {
continue;
}
String s = o.getKey() + "=" + o.getValue();
reStr += (s + "&");
}
return StringUtil.isBlank(reStr) ? "" : reStr.substring(0, reStr.length() - 1);
}
/**
* 将字符串 (name1=value1&name2=value2)的形式,转为 Map 形式
*
* @param param
* @return Map
* @throws UnsupportedEncodingException
*/
public static Map toParamMap(String param) throws UnsupportedEncodingException {
Map paramMap = new HashMap<>();
if (StringUtil.isBlank(param)) {
return paramMap;
}
String[] paramPairsArr = param.split("&");
for (String paramPair : paramPairsArr) {
String[] paramPairArr = paramPair.split("=");
if (paramPairArr.length == 2) {
paramMap.put(paramPairArr[0].trim(), paramPairArr[1].trim());
} else {
paramMap.put(paramPairArr[0].trim(), "");
}
}
return paramMap;
}
/**
* 判断是否是 HTTP URL
*
* @param url 请求 url
* @return 布尔值
*/
public static Boolean isHttpUrl(String url) {
boolean flag = false;
try {
flag = url.indexOf("://") != -1;
} catch (Exception e) {
}
return flag;
}
}