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;
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值