form表单形式请求数据

获取第三方数据,第三方给的接口是form表单形式,当时找了很多种方法都不太行,如果遇到第三方访问时候是一下代码这种情况时候:

Request request = new Request(taskConfig.getUrl());
        request.setMethod(HttpConstant.Method.POST);
        request.addHeader("accessToken", ”accessToken值”);
        Map<String, Object> param = new HashMap<>(10);
        param.put(“A”, 值);
        param.put(“B”, 值);
        request.setRequestBody(HttpRequestBody.form(param, Charsets.UTF_8.name()));
        return request;

可以使用这段代码进行访问对方接口,完成业务处理:

        String url = "url";

        PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
        connManager.setMaxTotal(1000);
        connManager.setDefaultMaxPerRoute(1000);
        CloseableHttpClient httpclient = HttpClients.custom().setConnectionManager(connManager)
                .setConnectionManagerShared(true).build();

        HttpPost httpPost = new HttpPost(url);// 创建httpPost
        HashMap<String, String> header = new HashMap<>();
        httpPost.setHeader("accessToken", "accessToken值");
        httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");

        RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(20000)
                .setSocketTimeout(20000).setConnectTimeout(20000).build();
        httpPost.setConfig(requestConfig);
        //设置参数
        String params = "A=值&B=值【from表单请求参数】";

        StringEntity entity = new StringEntity(params, "utf-8");
        entity.setContentEncoding("UTF-8");
        entity.setContentType("application/x-www-form-urlencoded");
        httpPost.setEntity(entity);
        CloseableHttpResponse response = null;
        try
        {
            response = httpclient.execute(httpPost);
            StatusLine status = response.getStatusLine();
            int state = status.getStatusCode();
            if (state == HttpStatus.SC_OK)
            {
                HttpEntity responseEntity = response.getEntity();
                //结果转为字符串
                String jsonString = EntityUtils.toString(responseEntity, "UTF-8");
                if (jsonString != null)
                {
                    if (jsonString != "")
                    {

                        业务处理
                    }

                }
            }
        }
        finally
        {
            if (response != null)
            {
                try
                {
                    response.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
            //需要拼接循环请求将注释下方部分代码进行外提。
            try
            {
                httpclient.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值