将http工具类整合在一起
This commit is contained in:
parent
2f49292672
commit
56d486e010
@ -1,19 +1,25 @@
|
|||||||
package cn.lili.common.utils;
|
package cn.lili.common.utils;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import com.xkcoding.http.HttpUtil;
|
import com.xkcoding.http.HttpUtil;
|
||||||
import com.xkcoding.http.config.HttpConfig;
|
import com.xkcoding.http.config.HttpConfig;
|
||||||
import com.xkcoding.http.support.HttpHeader;
|
import com.xkcoding.http.support.HttpHeader;
|
||||||
import com.xkcoding.http.support.httpclient.HttpClientImpl;
|
import com.xkcoding.http.support.httpclient.HttpClientImpl;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.net.URLEncoder;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.Iterator;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* HttpUtil 工具,统一处理 http 请求,方便对 simple-http 做定制
|
* HTTP 工具类
|
||||||
*
|
* @author liushuai
|
||||||
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
|
|
||||||
* @version 4.1
|
|
||||||
* @since 1.0.0
|
|
||||||
*/
|
*/
|
||||||
|
@Slf4j
|
||||||
public class HttpUtils {
|
public class HttpUtils {
|
||||||
|
|
||||||
public HttpUtils(HttpConfig config) {
|
public HttpUtils(HttpConfig config) {
|
||||||
@ -105,4 +111,258 @@ public class HttpUtils {
|
|||||||
public String post(String url, Map<String, String> params, HttpHeader header, boolean encode) {
|
public String post(String url, Map<String, String> params, HttpHeader header, boolean encode) {
|
||||||
return HttpUtil.post(url, params, header, encode);
|
return HttpUtil.post(url, params, header, encode);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 静态方法运行参数
|
||||||
|
*/
|
||||||
|
public static final int HTTP_CONN_TIMEOUT = 100000;
|
||||||
|
public static final int HTTP_SOCKET_TIMEOUT = 100000;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* POST 静态方法请求
|
||||||
|
*
|
||||||
|
* @param reqUrl
|
||||||
|
* @param parameters
|
||||||
|
* @param encoding
|
||||||
|
* @param connectTimeout
|
||||||
|
* @param readTimeout
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static String doPost(String reqUrl, Map<String, String> parameters, String encoding, int connectTimeout,
|
||||||
|
int readTimeout) {
|
||||||
|
HttpURLConnection urlConn = null;
|
||||||
|
try {
|
||||||
|
urlConn = sendPost(reqUrl, parameters, encoding, connectTimeout, readTimeout);
|
||||||
|
String responseContent = getContent(urlConn, encoding);
|
||||||
|
return responseContent.trim();
|
||||||
|
} finally {
|
||||||
|
if (urlConn != null) {
|
||||||
|
urlConn.disconnect();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* post携带json请求 静态方法
|
||||||
|
*
|
||||||
|
* @param reqUrl 请求地址
|
||||||
|
* @param jsonParameters 参数
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static String doPostWithJson(String reqUrl, Map<String, String> jsonParameters) {
|
||||||
|
|
||||||
|
BufferedReader reader = null;
|
||||||
|
try {
|
||||||
|
//创建连接
|
||||||
|
URL url = new URL(reqUrl);
|
||||||
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||||
|
connection.setDoOutput(true);
|
||||||
|
connection.setDoInput(true);
|
||||||
|
connection.setUseCaches(false);
|
||||||
|
connection.setInstanceFollowRedirects(true);
|
||||||
|
//设置请求方式
|
||||||
|
connection.setRequestMethod("POST");
|
||||||
|
//设置发送数据的格式
|
||||||
|
connection.setRequestProperty("Content-Type", "application/json");
|
||||||
|
connection.connect();
|
||||||
|
//一定要用BufferedReader 来接收响应, 使用字节来接收响应的方法是接收不到内容的
|
||||||
|
//utf-8编码
|
||||||
|
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), StandardCharsets.UTF_8);
|
||||||
|
out.append(JSONObject.toJSONString(jsonParameters));
|
||||||
|
out.flush();
|
||||||
|
out.close();
|
||||||
|
//读取响应
|
||||||
|
reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
|
||||||
|
String line;
|
||||||
|
String res = "";
|
||||||
|
while ((line = reader.readLine()) != null) {
|
||||||
|
res += line;
|
||||||
|
}
|
||||||
|
reader.close();
|
||||||
|
|
||||||
|
return res;
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error("post请求错误", e);
|
||||||
|
}
|
||||||
|
//自定义错误信息
|
||||||
|
return "error";
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* post携带json请求 静态方法
|
||||||
|
*
|
||||||
|
* @param reqUrl 请求地址
|
||||||
|
* @param object 对象
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static String doPostWithJson(String reqUrl, Object object) {
|
||||||
|
|
||||||
|
BufferedReader reader = null;
|
||||||
|
try {
|
||||||
|
//创建连接
|
||||||
|
URL url = new URL(reqUrl);
|
||||||
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||||
|
connection.setDoOutput(true);
|
||||||
|
connection.setDoInput(true);
|
||||||
|
connection.setUseCaches(false);
|
||||||
|
connection.setInstanceFollowRedirects(true);
|
||||||
|
//设置请求方式
|
||||||
|
connection.setRequestMethod("POST");
|
||||||
|
//设置发送数据的格式
|
||||||
|
connection.setRequestProperty("Content-Type", "application/json");
|
||||||
|
connection.connect();
|
||||||
|
//一定要用BufferedReader 来接收响应, 使用字节来接收响应的方法是接收不到内容的
|
||||||
|
//utf-8编码
|
||||||
|
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), StandardCharsets.UTF_8);
|
||||||
|
out.append(JSONObject.toJSONString(object));
|
||||||
|
out.flush();
|
||||||
|
out.close();
|
||||||
|
//读取响应
|
||||||
|
reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
|
||||||
|
String line;
|
||||||
|
String res = "";
|
||||||
|
while ((line = reader.readLine()) != null) {
|
||||||
|
res += line;
|
||||||
|
}
|
||||||
|
reader.close();
|
||||||
|
|
||||||
|
return res;
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error("post错误", e);
|
||||||
|
}
|
||||||
|
//自定义错误信息
|
||||||
|
return "error";
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送post请求
|
||||||
|
*
|
||||||
|
* @param reqUrl
|
||||||
|
* @param parameters
|
||||||
|
* @param encoding
|
||||||
|
* @param connectTimeout
|
||||||
|
* @param readTimeout
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private static HttpURLConnection sendPost(String reqUrl,
|
||||||
|
Map<String, String> parameters, String encoding, int connectTimeout, int readTimeout) {
|
||||||
|
HttpURLConnection urlConn = null;
|
||||||
|
try {
|
||||||
|
String params = generatorParamString(parameters, encoding);
|
||||||
|
URL url = new URL(reqUrl);
|
||||||
|
urlConn = (HttpURLConnection) url.openConnection();
|
||||||
|
urlConn.setRequestMethod("POST");
|
||||||
|
//(单位:毫秒)jdk
|
||||||
|
urlConn.setConnectTimeout(connectTimeout);
|
||||||
|
//(单位:毫秒)jdk 1.5换成这个,读操作超时
|
||||||
|
urlConn.setReadTimeout(readTimeout);
|
||||||
|
urlConn.setDoOutput(true);
|
||||||
|
//String按照字节处理是一个好方法
|
||||||
|
byte[] b = params.getBytes(encoding);
|
||||||
|
urlConn.getOutputStream().write(b, 0, b.length);
|
||||||
|
urlConn.getOutputStream().flush();
|
||||||
|
urlConn.getOutputStream().close();
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e.getMessage(), e);
|
||||||
|
}
|
||||||
|
return urlConn;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get 请求 静态方法
|
||||||
|
*
|
||||||
|
* @param urlConn
|
||||||
|
* @param encoding
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private static String getContent(HttpURLConnection urlConn, String encoding) {
|
||||||
|
try {
|
||||||
|
String responseContent = null;
|
||||||
|
InputStream in = urlConn.getInputStream();
|
||||||
|
BufferedReader rd = new BufferedReader(new InputStreamReader(in, encoding));
|
||||||
|
String tempLine = rd.readLine();
|
||||||
|
StringBuffer tempStr = new StringBuffer();
|
||||||
|
String crlf = System.getProperty("line.separator");
|
||||||
|
while (tempLine != null) {
|
||||||
|
tempStr.append(tempLine);
|
||||||
|
tempStr.append(crlf);
|
||||||
|
tempLine = rd.readLine();
|
||||||
|
}
|
||||||
|
responseContent = tempStr.toString();
|
||||||
|
rd.close();
|
||||||
|
in.close();
|
||||||
|
return responseContent;
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e.getMessage(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get 请求 静态方法
|
||||||
|
*
|
||||||
|
* @param link
|
||||||
|
* @param encoding
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static String doGet(String link, String encoding, int connectTimeout, int readTimeout) {
|
||||||
|
HttpURLConnection conn = null;
|
||||||
|
try {
|
||||||
|
URL url = new URL(link);
|
||||||
|
conn = (HttpURLConnection) url.openConnection();
|
||||||
|
conn.setRequestMethod("GET");
|
||||||
|
conn.setConnectTimeout(connectTimeout);
|
||||||
|
conn.setReadTimeout(readTimeout);
|
||||||
|
BufferedInputStream in = new BufferedInputStream(
|
||||||
|
conn.getInputStream());
|
||||||
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
|
byte[] buf = new byte[1024];
|
||||||
|
for (int i = 0; (i = in.read(buf)) > 0; ) {
|
||||||
|
out.write(buf, 0, i);
|
||||||
|
}
|
||||||
|
out.flush();
|
||||||
|
String s = out.toString(encoding);
|
||||||
|
return s;
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e.getMessage(), e);
|
||||||
|
} finally {
|
||||||
|
if (conn != null) {
|
||||||
|
conn.disconnect();
|
||||||
|
conn = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将parameters中数据转换成用"&"链接的http请求参数形式
|
||||||
|
*
|
||||||
|
* @param parameters
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private static String generatorParamString(Map<String, String> parameters, String encoding) {
|
||||||
|
StringBuffer params = new StringBuffer();
|
||||||
|
if (parameters != null) {
|
||||||
|
for (Iterator<String> iter = parameters.keySet().iterator(); iter
|
||||||
|
.hasNext(); ) {
|
||||||
|
String name = iter.next();
|
||||||
|
String value = parameters.get(name);
|
||||||
|
params.append(name + "=");
|
||||||
|
try {
|
||||||
|
params.append(URLEncoder.encode(value, encoding));
|
||||||
|
} catch (UnsupportedEncodingException e) {
|
||||||
|
throw new RuntimeException(e.getMessage(), e);
|
||||||
|
} catch (Exception e) {
|
||||||
|
String message = String.format("'%s'='%s'", name, value);
|
||||||
|
throw new RuntimeException(message, e);
|
||||||
|
}
|
||||||
|
if (iter.hasNext()) {
|
||||||
|
params.append("&");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return params.toString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,7 @@ import cn.lili.common.security.AuthUser;
|
|||||||
import cn.lili.common.security.context.UserContext;
|
import cn.lili.common.security.context.UserContext;
|
||||||
import cn.lili.common.security.token.Token;
|
import cn.lili.common.security.token.Token;
|
||||||
import cn.lili.common.utils.CookieUtil;
|
import cn.lili.common.utils.CookieUtil;
|
||||||
|
import cn.lili.common.utils.HttpUtils;
|
||||||
import cn.lili.common.utils.StringUtils;
|
import cn.lili.common.utils.StringUtils;
|
||||||
import cn.lili.modules.connect.entity.Connect;
|
import cn.lili.modules.connect.entity.Connect;
|
||||||
import cn.lili.modules.connect.entity.dto.ConnectAuthUser;
|
import cn.lili.modules.connect.entity.dto.ConnectAuthUser;
|
||||||
@ -27,7 +28,6 @@ import cn.lili.modules.system.entity.dto.connect.WechatConnectSetting;
|
|||||||
import cn.lili.modules.system.entity.dto.connect.dto.WechatConnectSettingItem;
|
import cn.lili.modules.system.entity.dto.connect.dto.WechatConnectSettingItem;
|
||||||
import cn.lili.modules.system.entity.enums.SettingEnum;
|
import cn.lili.modules.system.entity.enums.SettingEnum;
|
||||||
import cn.lili.modules.system.service.SettingService;
|
import cn.lili.modules.system.service.SettingService;
|
||||||
import cn.lili.modules.system.utils.HttpUtils;
|
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
@ -3,11 +3,11 @@ package cn.lili.modules.goods.util;
|
|||||||
import cn.hutool.json.JSONObject;
|
import cn.hutool.json.JSONObject;
|
||||||
import cn.lili.common.enums.ClientTypeEnum;
|
import cn.lili.common.enums.ClientTypeEnum;
|
||||||
import cn.lili.common.exception.ServiceException;
|
import cn.lili.common.exception.ServiceException;
|
||||||
|
import cn.lili.common.utils.HttpUtils;
|
||||||
import cn.lili.modules.goods.entity.dos.Commodity;
|
import cn.lili.modules.goods.entity.dos.Commodity;
|
||||||
import cn.lili.modules.goods.entity.dos.Studio;
|
import cn.lili.modules.goods.entity.dos.Studio;
|
||||||
import cn.lili.modules.goods.entity.dto.GoodsInfo;
|
import cn.lili.modules.goods.entity.dto.GoodsInfo;
|
||||||
import cn.lili.modules.message.util.WechatAccessTokenUtil;
|
import cn.lili.modules.message.util.WechatAccessTokenUtil;
|
||||||
import cn.lili.modules.system.utils.HttpUtils;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
@ -7,6 +7,7 @@ import cn.hutool.json.JSONUtil;
|
|||||||
import cn.lili.common.enums.ResultCode;
|
import cn.lili.common.enums.ResultCode;
|
||||||
import cn.lili.common.exception.ServiceException;
|
import cn.lili.common.exception.ServiceException;
|
||||||
import cn.lili.common.enums.ClientTypeEnum;
|
import cn.lili.common.enums.ClientTypeEnum;
|
||||||
|
import cn.lili.common.utils.HttpUtils;
|
||||||
import cn.lili.modules.message.entity.dos.WechatMPMessage;
|
import cn.lili.modules.message.entity.dos.WechatMPMessage;
|
||||||
import cn.lili.modules.message.entity.enums.WechatMessageItemEnums;
|
import cn.lili.modules.message.entity.enums.WechatMessageItemEnums;
|
||||||
import cn.lili.modules.message.mapper.WechatMPMessageMapper;
|
import cn.lili.modules.message.mapper.WechatMPMessageMapper;
|
||||||
@ -14,7 +15,6 @@ import cn.lili.modules.message.service.WechatMPMessageService;
|
|||||||
import cn.lili.modules.message.util.WechatAccessTokenUtil;
|
import cn.lili.modules.message.util.WechatAccessTokenUtil;
|
||||||
import cn.lili.modules.message.util.WechatMessageUtil;
|
import cn.lili.modules.message.util.WechatMessageUtil;
|
||||||
import cn.lili.modules.order.order.entity.enums.OrderStatusEnum;
|
import cn.lili.modules.order.order.entity.enums.OrderStatusEnum;
|
||||||
import cn.lili.modules.system.utils.HttpUtils;
|
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
@ -6,6 +6,7 @@ import cn.hutool.json.JSONUtil;
|
|||||||
import cn.lili.common.enums.ResultCode;
|
import cn.lili.common.enums.ResultCode;
|
||||||
import cn.lili.common.exception.ServiceException;
|
import cn.lili.common.exception.ServiceException;
|
||||||
import cn.lili.common.enums.ClientTypeEnum;
|
import cn.lili.common.enums.ClientTypeEnum;
|
||||||
|
import cn.lili.common.utils.HttpUtils;
|
||||||
import cn.lili.modules.message.entity.dos.WechatMessage;
|
import cn.lili.modules.message.entity.dos.WechatMessage;
|
||||||
import cn.lili.modules.message.entity.enums.WechatMessageItemEnums;
|
import cn.lili.modules.message.entity.enums.WechatMessageItemEnums;
|
||||||
import cn.lili.modules.message.mapper.WechatMessageMapper;
|
import cn.lili.modules.message.mapper.WechatMessageMapper;
|
||||||
@ -13,7 +14,6 @@ import cn.lili.modules.message.service.WechatMessageService;
|
|||||||
import cn.lili.modules.message.util.WechatAccessTokenUtil;
|
import cn.lili.modules.message.util.WechatAccessTokenUtil;
|
||||||
import cn.lili.modules.message.util.WechatMessageUtil;
|
import cn.lili.modules.message.util.WechatMessageUtil;
|
||||||
import cn.lili.modules.order.order.entity.enums.OrderStatusEnum;
|
import cn.lili.modules.order.order.entity.enums.OrderStatusEnum;
|
||||||
import cn.lili.modules.system.utils.HttpUtils;
|
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
@ -20,7 +20,7 @@ import cn.lili.modules.order.order.entity.dos.Order;
|
|||||||
import cn.lili.modules.order.order.entity.dos.OrderItem;
|
import cn.lili.modules.order.order.entity.dos.OrderItem;
|
||||||
import cn.lili.modules.order.order.service.OrderItemService;
|
import cn.lili.modules.order.order.service.OrderItemService;
|
||||||
import cn.lili.modules.order.order.service.OrderService;
|
import cn.lili.modules.order.order.service.OrderService;
|
||||||
import cn.lili.modules.system.utils.HttpUtils;
|
import cn.lili.common.utils.HttpUtils;
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
@ -1,243 +0,0 @@
|
|||||||
package cn.lili.modules.system.utils;
|
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSONObject;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
|
|
||||||
import java.io.*;
|
|
||||||
import java.net.HttpURLConnection;
|
|
||||||
import java.net.URL;
|
|
||||||
import java.net.URLEncoder;
|
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Http工具
|
|
||||||
*
|
|
||||||
* @author pikachu
|
|
||||||
* @since 2018/3/13
|
|
||||||
*/
|
|
||||||
@Slf4j
|
|
||||||
public final class HttpUtils {
|
|
||||||
|
|
||||||
public static final int HTTP_CONN_TIMEOUT = 100000;
|
|
||||||
public static final int HTTP_SOCKET_TIMEOUT = 100000;
|
|
||||||
|
|
||||||
public static String doPost(String reqUrl, Map<String, String> parameters, String encoding, int connectTimeout,
|
|
||||||
int readTimeout) {
|
|
||||||
HttpURLConnection urlConn = null;
|
|
||||||
try {
|
|
||||||
urlConn = sendPost(reqUrl, parameters, encoding, connectTimeout, readTimeout);
|
|
||||||
String responseContent = getContent(urlConn, encoding);
|
|
||||||
return responseContent.trim();
|
|
||||||
} finally {
|
|
||||||
if (urlConn != null) {
|
|
||||||
urlConn.disconnect();
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* post携带json请求
|
|
||||||
*
|
|
||||||
* @param reqUrl 请求地址
|
|
||||||
* @param jsonParameters 参数
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public static String doPostWithJson(String reqUrl, Map<String, String> jsonParameters) {
|
|
||||||
|
|
||||||
BufferedReader reader = null;
|
|
||||||
try {
|
|
||||||
//创建连接
|
|
||||||
URL url = new URL(reqUrl);
|
|
||||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
|
||||||
connection.setDoOutput(true);
|
|
||||||
connection.setDoInput(true);
|
|
||||||
connection.setUseCaches(false);
|
|
||||||
connection.setInstanceFollowRedirects(true);
|
|
||||||
//设置请求方式
|
|
||||||
connection.setRequestMethod("POST");
|
|
||||||
//设置发送数据的格式
|
|
||||||
connection.setRequestProperty("Content-Type", "application/json");
|
|
||||||
connection.connect();
|
|
||||||
//一定要用BufferedReader 来接收响应, 使用字节来接收响应的方法是接收不到内容的
|
|
||||||
//utf-8编码
|
|
||||||
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), StandardCharsets.UTF_8);
|
|
||||||
out.append(JSONObject.toJSONString(jsonParameters));
|
|
||||||
out.flush();
|
|
||||||
out.close();
|
|
||||||
//读取响应
|
|
||||||
reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
|
|
||||||
String line;
|
|
||||||
String res = "";
|
|
||||||
while ((line = reader.readLine()) != null) {
|
|
||||||
res += line;
|
|
||||||
}
|
|
||||||
reader.close();
|
|
||||||
|
|
||||||
return res;
|
|
||||||
} catch (IOException e) {
|
|
||||||
log.error("post请求错误", e);
|
|
||||||
}
|
|
||||||
//自定义错误信息
|
|
||||||
return "error";
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* post携带json请求
|
|
||||||
*
|
|
||||||
* @param reqUrl 请求地址
|
|
||||||
* @param object 对象
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public static String doPostWithJson(String reqUrl, Object object) {
|
|
||||||
|
|
||||||
BufferedReader reader = null;
|
|
||||||
try {
|
|
||||||
//创建连接
|
|
||||||
URL url = new URL(reqUrl);
|
|
||||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
|
||||||
connection.setDoOutput(true);
|
|
||||||
connection.setDoInput(true);
|
|
||||||
connection.setUseCaches(false);
|
|
||||||
connection.setInstanceFollowRedirects(true);
|
|
||||||
//设置请求方式
|
|
||||||
connection.setRequestMethod("POST");
|
|
||||||
//设置发送数据的格式
|
|
||||||
connection.setRequestProperty("Content-Type", "application/json");
|
|
||||||
connection.connect();
|
|
||||||
//一定要用BufferedReader 来接收响应, 使用字节来接收响应的方法是接收不到内容的
|
|
||||||
//utf-8编码
|
|
||||||
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), StandardCharsets.UTF_8);
|
|
||||||
out.append(JSONObject.toJSONString(object));
|
|
||||||
out.flush();
|
|
||||||
out.close();
|
|
||||||
//读取响应
|
|
||||||
reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
|
|
||||||
String line;
|
|
||||||
String res = "";
|
|
||||||
while ((line = reader.readLine()) != null) {
|
|
||||||
res += line;
|
|
||||||
}
|
|
||||||
reader.close();
|
|
||||||
|
|
||||||
return res;
|
|
||||||
} catch (IOException e) {
|
|
||||||
log.error("post错误", e);
|
|
||||||
}
|
|
||||||
//自定义错误信息
|
|
||||||
return "error";
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private static HttpURLConnection sendPost(String reqUrl,
|
|
||||||
Map<String, String> parameters, String encoding, int connectTimeout, int readTimeout) {
|
|
||||||
HttpURLConnection urlConn = null;
|
|
||||||
try {
|
|
||||||
String params = generatorParamString(parameters, encoding);
|
|
||||||
URL url = new URL(reqUrl);
|
|
||||||
urlConn = (HttpURLConnection) url.openConnection();
|
|
||||||
urlConn.setRequestMethod("POST");
|
|
||||||
//(单位:毫秒)jdk
|
|
||||||
urlConn.setConnectTimeout(connectTimeout);
|
|
||||||
//(单位:毫秒)jdk 1.5换成这个,读操作超时
|
|
||||||
urlConn.setReadTimeout(readTimeout);
|
|
||||||
urlConn.setDoOutput(true);
|
|
||||||
//String按照字节处理是一个好方法
|
|
||||||
byte[] b = params.getBytes(encoding);
|
|
||||||
urlConn.getOutputStream().write(b, 0, b.length);
|
|
||||||
urlConn.getOutputStream().flush();
|
|
||||||
urlConn.getOutputStream().close();
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new RuntimeException(e.getMessage(), e);
|
|
||||||
}
|
|
||||||
return urlConn;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static String getContent(HttpURLConnection urlConn, String encoding) {
|
|
||||||
try {
|
|
||||||
String responseContent = null;
|
|
||||||
InputStream in = urlConn.getInputStream();
|
|
||||||
BufferedReader rd = new BufferedReader(new InputStreamReader(in, encoding));
|
|
||||||
String tempLine = rd.readLine();
|
|
||||||
StringBuffer tempStr = new StringBuffer();
|
|
||||||
String crlf = System.getProperty("line.separator");
|
|
||||||
while (tempLine != null) {
|
|
||||||
tempStr.append(tempLine);
|
|
||||||
tempStr.append(crlf);
|
|
||||||
tempLine = rd.readLine();
|
|
||||||
}
|
|
||||||
responseContent = tempStr.toString();
|
|
||||||
rd.close();
|
|
||||||
in.close();
|
|
||||||
return responseContent;
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new RuntimeException(e.getMessage(), e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param link
|
|
||||||
* @param encoding
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public static String doGet(String link, String encoding, int connectTimeout, int readTimeout) {
|
|
||||||
HttpURLConnection conn = null;
|
|
||||||
try {
|
|
||||||
URL url = new URL(link);
|
|
||||||
conn = (HttpURLConnection) url.openConnection();
|
|
||||||
conn.setRequestMethod("GET");
|
|
||||||
conn.setConnectTimeout(connectTimeout);
|
|
||||||
conn.setReadTimeout(readTimeout);
|
|
||||||
BufferedInputStream in = new BufferedInputStream(
|
|
||||||
conn.getInputStream());
|
|
||||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
||||||
byte[] buf = new byte[1024];
|
|
||||||
for (int i = 0; (i = in.read(buf)) > 0; ) {
|
|
||||||
out.write(buf, 0, i);
|
|
||||||
}
|
|
||||||
out.flush();
|
|
||||||
String s = out.toString(encoding);
|
|
||||||
return s;
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new RuntimeException(e.getMessage(), e);
|
|
||||||
} finally {
|
|
||||||
if (conn != null) {
|
|
||||||
conn.disconnect();
|
|
||||||
conn = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 将parameters中数据转换成用"&"链接的http请求参数形式
|
|
||||||
*
|
|
||||||
* @param parameters
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
private static String generatorParamString(Map<String, String> parameters, String encoding) {
|
|
||||||
StringBuffer params = new StringBuffer();
|
|
||||||
if (parameters != null) {
|
|
||||||
for (Iterator<String> iter = parameters.keySet().iterator(); iter
|
|
||||||
.hasNext(); ) {
|
|
||||||
String name = iter.next();
|
|
||||||
String value = parameters.get(name);
|
|
||||||
params.append(name + "=");
|
|
||||||
try {
|
|
||||||
params.append(URLEncoder.encode(value, encoding));
|
|
||||||
} catch (UnsupportedEncodingException e) {
|
|
||||||
throw new RuntimeException(e.getMessage(), e);
|
|
||||||
} catch (Exception e) {
|
|
||||||
String message = String.format("'%s'='%s'", name, value);
|
|
||||||
throw new RuntimeException(message, e);
|
|
||||||
}
|
|
||||||
if (iter.hasNext()) {
|
|
||||||
params.append("&");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return params.toString();
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user