使用HttpClient请求方式JSON和JSON(FROM)

使用HttpClient请求方式JSON和JSON(FROM)

请求方式 : JSON(FROM)

	public Map httpResponseFrom(String url, Map<String, String> params){
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        HttpPost httpPost = null;
        HttpGet httpGet = null;
        BufferedReader reader = null;
        CloseableHttpResponse httpResponse = null;
        String result = "";
        Map<String, String> resultMap = null;
        try {
                MultipartEntityBuilder builder = MultipartEntityBuilder.create();
                if (params != null) {
                    for (String key : params.keySet()) {
                        builder.addPart(key,new StringBody(params.get(key), 		ContentType.create("text/plain", Consts.UTF_8)));
                    }
                HttpEntity reqEntity = builder.build();
                httpPost = new HttpPost(url);
                httpPost.setEntity(reqEntity);
                httpPost.setConfig(RequestConfig.custom().setConnectTimeout(90000).setSocketTimeout(90000).setSocketTimeout(30000).build());
                //POST调用接口
                httpResponse = httpClient.execute(httpPost);
            }
            reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(), "UTF-8"));
            String inputLine;

            while ((inputLine = reader.readLine()) != null) {
                result += inputLine;
            }
            if (!StringUtils.isBlank(result)) {
                //将json转成Map返回
                return resultMap = (Map) JSON.parse(result);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if(httpResponse != null)
                    httpResponse.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

请求方式 : JSON

public Map httpResponse(String url, String body){
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        HttpPost httpPost = null;
        HttpGet httpGet = null;
        BufferedReader reader = null;
        CloseableHttpResponse httpResponse = null;
        String result = "";
        Map<String, String> resultMap = null;
        try {
                StringEntity urlEntity = new StringEntity(new String(body.getBytes(), "ISO-8859-1"));//解决有中文返回“认证失败”
                httpPost = new HttpPost(url);
                httpPost.addHeader("Content-type", "application/json; charset=utf-8");
                httpPost.setEntity(urlEntity);
                httpPost.setConfig(RequestConfig.custom().setConnectTimeout(90000).setSocketTimeout(90000).build());
                //POST调用接口
                httpResponse = httpClient.execute(httpPost);
            reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(), "UTF-8"));
            String inputLine;
            while ((inputLine = reader.readLine()) != null) {
                result += inputLine;
            }
            if (!StringUtils.isBlank(result)) {
                //将json转成Map返回
                return resultMap = (Map) JSON.parse(result);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if(httpResponse != null)
                    httpResponse.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值