Java post 表单请求和json请求第三方接口

1、表单请求
public final <Q, R> R getRequest(HttpExecutor httpExecutor, String uri, Class<R> clazz) {
        R response = null;
        List<NameValuePair> headers = new ArrayList<NameValuePair>();

        headers.add(new BasicNameValuePair("Accept", "*/*"));
        headers.add(new BasicNameValuePair("Accept-Encoding", "gzip, deflate"));
        headers.add(new BasicNameValuePair("Accept-Language", "zh-CN,zh;q=0.8,en-us;q=0.5,en;q=0.3,*;q=0.2"));
        headers.add(new BasicNameValuePair("Connection", "keep-alive"));
        headers.add(new BasicNameValuePair("Cache-Control", "max-age=0"));
        headers.add(new BasicNameValuePair("Pragma", "no-cache"));
        headers.add(new BasicNameValuePair("User-Agent", "Mozilla/5.0"));
        headers.add(new BasicNameValuePair("clientCode", clientCode));
        headers.add(new BasicNameValuePair("tradeCode", tradeCode));
        headers.add(new BasicNameValuePair("requestId", UUID.randomUUID().toString()));

        try {
            HttpExecutor.Response resp = httpExecutor.executeGet(uri, headers);

            if (resp.isSuccess()) {
                String result = resp.toString();
                if (clazz.equals(String.class)) {
                    response = (R) result;
                } else {
                    response = JSON.parseObject(result, clazz);
                }

            } else if (!resp.isNoError()) {
                throw new ServiceException(resp.getErrorInfo(), NETWORK_ERROR);
            } else {
                throw new IllegalStateException(String.format("unexpected status code '%d'", resp.statusCode));
            }
        } catch (Exception e) {
            throw wrapServiceException(e);
        }

        return response;
    }
2、json请求
  /**
     * 组装请求数据
     *
     * @param httpExecutor
     * @param uri
     * @param requestBody
     * @param token
     * @return
     */
    public final String getRequest(HttpExecutor httpExecutor, String uri, String requestBody, String token) {
        List<NameValuePair> headers = new ArrayList<>();
        headers.add(new BasicNameValuePair("Content-Type", "application/json"));
        if (StringUtils.isNotEmpty(token)) {
            headers.add(new BasicNameValuePair("token", token));
        }
        try {
            log.info("CallServiceImpl.getRequest headers:{}", JSON.toJSONString(headers));
            HttpExecutor.Response resp = httpExecutor.executeStringPost(uri, requestBody, null, headers);
            if (resp.isSuccess()) {
                return resp.toString();
            } else if (!resp.isNoError()) {
                throw new ServiceException(resp.getErrorInfo(), NETWORK_ERROR);
            } else {
                throw new IllegalStateException(String.format("unexpected status code '%d'", resp.statusCode));
            }
        } catch (Exception e) {
            throw wrapServiceException(e);
        }
    }

不同的是请求体的封装方式。

3、区别
  • 1 表单提交

(1)从前端传过来的请求参数是key=value形式的

(2)springmvc自动进行参数的绑定

  • 2 json格式提交

(1)前端传过来的参数是字符串,以json格式呈现

(2)springmvc接收需要使用@RequestBody注解,对json字符串进行解析

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值