Apache HttpClient实现访问RESTFUL接口

本文介绍如何使用Apache HttpClient 4.5.2发起HTTP GET请求并解析响应。具体包括依赖库的添加、发送GET请求的代码实现及如何处理响应信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、添加jar

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.2</version>
        </dependency>


2、代码

              CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpGet httpGet = new HttpGet("http://172.18.1.177:8080/declare-rest/restful/company/getPaging?currentPage=1&pageRecord=2");
            CloseableHttpResponse httpResponse = null;
            try {
                httpResponse = httpClient.execute(httpGet);
                System.out.print("Protocol Version :: ");
                System.out.println(httpResponse.getProtocolVersion());
                System.out.println("--------------------------------------------------");
                System.out.print("Status Code :: ");
                System.out.println(httpResponse.getStatusLine().getStatusCode());
                System.out.println("--------------------------------------------------");
                System.out.print("Reason Phrase :: ");
                System.out.println(httpResponse.getStatusLine().getReasonPhrase());
                System.out.println("--------------------------------------------------");
                System.out.print("Status Line :: ");
                System.out.println(httpResponse.getStatusLine().toString());
                System.out.println("--------------------------------------------------");
                HttpEntity httpEntity = httpResponse.getEntity();
                System.out.print("Content Length :: ");
                System.out.println(httpEntity.getContentLength());
                System.out.println("--------------------------------------------------");
                System.out.print("Content Type :: ");
                System.out.println(httpEntity.getContentType());
                System.out.println("--------------------------------------------------");
                System.out.print("Content :: ");
                System.out.println(httpEntity.getContent());
                System.out.println("--------------------------------------------------");
                System.out.print("Content Text :: ");
                System.out.println(EntityUtils.toString(httpEntity));
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    httpClient.close();
                    httpResponse.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值