一、原生方式
public static String httpRequest(String requestUrl, String RequestMethod, String data, String accessToken) {
@SuppressWarnings("unused")
String jsonObject = null;
BufferedReader in = null;
HttpURLConnection conn = null;
try {
URL realURL = new URL(requestUrl);
conn = (HttpURLConnection) realURL.openConnection();
conn.setDoInput(true);
conn.setRequestMethod(RequestMethod);
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
conn.setRequestProperty("accept", "application/json;charset=utf-8");
conn.setRequestProperty("Authorization", accessToken);
if (data != null) {
OutputStream out = conn.getOutputStream();
out.write(data.getBytes("UTF-8"));
out.close();
}
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
String line;
StringBuffer buffer = new StringBuffer();
while ((line = in.readLine()) != null) {
buffer.append(line);
}
jsonObject = buffer.toString();
System.out.println("jsonObject:" + jsonObject);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException ex) {
log.error("错误:" + ex.getMessage());
}
}
return jsonObject;
}
二、HttpRequest
import com.github.kevinsawicki.http.HttpRequest;
public static String httpRequestBatch(String accessToken, List<CouponVerification> couponVerificationList) {
String errcode;
Map<String, Object> map = new HashMap<>();
map.put("organization_id", organization_id);
List<String> list = new ArrayList<>();
for (CouponVerification couponVerification : couponVerificationList) {
Map<String, Object> map1 = new HashMap<>();
map1.put("card_code", couponVerification.getCard_code());
map1.put("channel", 6);
map1.put("consume_time", DateUtil.parseDateToStr(couponVerification.getConsume_time(), DateUtil.DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS));
map1.put("consume_store", couponVerification.getConsume_store());
map1.put("order_no", couponVerification.getOrder_no());
list.add(map1.toString());
}
map.put("consume_info", list);
try {
HttpRequest httpRequest = new HttpRequest(hexiaoBatchUrl, "POST");
httpRequest.contentType("application/json", "UTF-8");
httpRequest.header("Authorization", "Bearer " + accessToken);
String ret = httpRequest.form(map).body();
errcode = "0";
} catch (Exception ex) {
errcode = "40001";
System.out.println("券核销失败"+ex.getMessage());
}
return errcode;
}
三、forest框架
forest官网:请求方法 | Forest
<dependency>
<groupId>com.dtflys.forest</groupId>
<artifactId>forest-spring-boot-starter</artifactId>
<version>1.5.0</version>
</dependency>
public interface EtoClient {
@Post("${batchPicUrl}")
String batchPicUrl(@Var("batchPicUrl") String batchPicUrl, @JSONBody Map<String, Object> map, @Header("Authorization") String Authorization);
@Delete("${deleteProductUrl}")
String deleteProductUrl(@Var("deleteProductUrl") String deleteProductUrl, @JSONBody Map<String, Object> map, @Header("Authorization") String Authorization);
@Get("${getwxTokenUrl}")
String getwxTokenUrl(@Var("getwxTokenUrl") String getwxTokenUrl);
@Post("${getwxCodeUrl}")
byte[] getwxCodeUrl(@Var("getwxCodeUrl") String getwxCodeUrl, @JSONBody Map<String, Object> map);
}
@ForestScan(basePackages = "com.cloud.eto.client")
public class EtoCenterApplication {
public static void main(String[] args) {
SpringApplication.run(EtoCenterApplication.class, args);
}
}
forest:
bean-id: config0 # 在spring上下文中bean的id, 默认值为forestConfiguration
backend: okhttp3 # 后端HTTP API: okhttp3
max-connections: 1000 # 连接池最大连接数,默认值为500
max-route-connections: 500 # 每个路由的最大连接数,默认值为500
timeout: 3000 # 请求超时时间,单位为毫秒, 默认值为3000
connect-timeout: 3000 # 连接超时时间,单位为毫秒, 默认值为2000
retry-count: 1 # 请求失败后重试次数,默认为0次不重试
ssl-protocol: SSLv3 # 单向验证的HTTPS的默认SSL协议,默认为SSLv3
logEnabled: true # 打开或关闭日志,默认为true
log-request: true # 打开/关闭Forest请求日志(默认为 true)
log-response-status: true # 打开/关闭Forest响应状态日志(默认为 true)
log-response-content: true # 打开/关闭Forest响应内容日志(默认为 false)