http&https数据传输

一、原生方式

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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值