栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

HttpClient、CloseableHttpClient请求接口

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

HttpClient、CloseableHttpClient请求接口

HttpClient

HttpClient Get方式请求接口

    public JSONObject getProducts(String token) {
        JSONObject jsonObject = null;
        //创建HttpClient对象
        HttpClient objClient = new HttpClient();
		
		//设置传递的编码格式,防止出现乱码错误
        objClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
		
		//创建GetMethod 对象,参数为请求的地址
        GetMethod method = new GetMethod("https://XXXXX.com/");
        
        //设置请求头
        method.setRequestHeader("token", token);

        try {
        	//执行方法请求接口
            objClient.executeMethod(method);
            //打印服务器返回的状态
            System.out.println(method.getStatusLine());
            //获取返回值信息
            String strResponseBody = new String(method.getResponseBody(),"UTF-8");
            System.out.println(strResponseBody);
            jsonObject = JSONObject.parseObject(strResponseBody);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //释放连接
        method.releaseConnection();
        return jsonObject;
    }

HttpClient Post方式请求接口

    public JSONObject getProducts(String token) {
        JSONObject jsonObject = null;
        //创建HttpClient对象
        HttpClient objClient = new HttpClient();
		
		//设置传递的编码格式,防止出现乱码错误
        objClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
		
		//创建PostMethod 对象,参数为请求的地址
        PostMethod method = new PostMethod("https://XXXXX.com/");
        
        //设置请求头
        method.setRequestHeader("token", token);

        try {
        	//执行方法请求接口
            objClient.executeMethod(method);
            //打印服务器返回的状态
            System.out.println(method.getStatusLine());
            //获取返回值信息
            String strResponseBody = new String(method.getResponseBody(),"UTF-8");
            System.out.println(strResponseBody);
            jsonObject = JSONObject.parseObject(strResponseBody);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //释放连接
        method.releaseConnection();
        return jsonObject;
    }
CloseableHttpClient

CloseableHttpClient Get方式请求接口

    public JSONObject getProducts(String token) {
        JSONObject jsonObject = null;
        //通过HttpClients.createDefault()获取到CloseableHttpClient对象
        CloseableHttpClient httpclient = HttpClients.createDefault();
		
		//创建HttpGet 对象,参数为请求的地址
      	HttpGet method = new HttpGet("https://XXXXX.com/");
        
        //设置请求头
       method.setHeader("token", token);

        try {
        	//执行方法请求接口得到响应
        	HttpResponse response = httpclient.execute(method);
            //解析响应数据
            String content = EntityUtils.toString(response.getEntity(), "UTF-8");
            //转为json格式
            jsonObject = JSON.parseObject(content);
            System.out.println(jsonObject.toString());
            EntityUtils.consume(response.getEntity());//完全消耗
        } finally {
            try {
                //释放连接
                method.releaseConnection();
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return jsonObject;
    }

CloseableHttpClient Post方式请求接口

    public JSONObject saveOrder(String token) {
        JSONObject jsonObject = null;
        //通过HttpClients.createDefault()获取到CloseableHttpClient对象
        CloseableHttpClient httpclient = HttpClients.createDefault();
        
        //设置传递的编码格式,防止出现乱码错误
        httpclient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
		
		//创建HttpPost 对象,参数为请求的地址
        HttpPost method = new HttpPost("https://XXXXX.com/");
        //设置请求头
        method.setHeader("token", token);

        JSONObject requestJson = new JSONObject();
        //日期
        requestJson.put("times", new Date());
        //单号,唯一
        requestJson.put("orderCode", "123");
        //长(厘米)
        requestJson.put("longs", "1");
        //宽(厘米)
        requestJson.put("width", "1");
        //高(厘米
        requestJson.put("high", "1");
        //转为StringEntity 
        StringEntity entity = new StringEntity(requestJson.toString(), Charset.forName("UTF-8"));
        //设置编码
        entity.setContentEncoding("UTF-8");
        // 发送Json格式的数据请求
        entity.setContentType("application/json");
        //设置参数
        method.setEntity(entity);

        try {
        	//执行方法请求接口得到响应
            HttpResponse response = httpclient.execute(method);
            //获取返回值信息
            String content = EntityUtils.toString(response.getEntity(), "UTF-8");
            //转为json
            jsonObject = JSON.parseObject(content);
            System.out.println(jsonObject.toString());
            EntityUtils.consume(response.getEntity());//完全消耗
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                //释放连接
                method.releaseConnection();
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return jsonObject;
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/283998.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号