http的post请求

private String valueOf(String url, String param,String token) throws IOException {
        String result;
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response;
        HttpPost httpPost = new HttpPost(url);
        if(null != token && token.length() > 0){
            httpPost.setHeader("token", token);
        }
        //配置ResquestConfig
        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(15000)
                .setConnectTimeout(15000)
                .setConnectionRequestTimeout(15000)
                .build();
        //配置StringEntity
        StringEntity stringEntity = new StringEntity(String.valueOf(param), "UTF-8");
        stringEntity.setContentType("application/json");

        //将Entity和Config设置到HttpPost
        httpPost.setEntity(stringEntity);
        httpPost.setConfig(requestConfig);
        //获取页面
        response = httpClient.execute(httpPost);
        result = EntityUtils.toString(response.getEntity(), "UTF-8");
        return result;
    }
String data = this.getAllDevice("这里是url",token);

private String getAllDevice(String url, String param) throws IOException {
        String result;
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response;
        HttpPost httpPost = new HttpPost(url);
        httpPost.setHeader("token", param);
        //配置ResquestConfig
        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(15000)
                .setConnectTimeout(15000)
                .setConnectionRequestTimeout(15000)
                .build();
        //配置StringEntity
        StringEntity stringEntity = new StringEntity(String.valueOf(param), "UTF-8");
        stringEntity.setContentType("application/json");

        //将Entity和Config设置到HttpPost
        httpPost.setEntity(stringEntity);
        httpPost.setConfig(requestConfig);
        //获取页面
        response = httpClient.execute(httpPost);
        result = EntityUtils.toString(response.getEntity(), "UTF-8");
        JSONObject jsonobject = JSONObject.parseObject(result);
        String data = jsonobject.getString("data");
        return data;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值