HttpClient的基本使用

1.导包

 <!--http客户端-->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.3.5</version>
        </dependency>

2.不带参数的GET请求

@Test
    public void test1() throws IOException {

        //1.创建HttpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();

        //2.创建get请求
        HttpGet httpGet=new HttpGet("http://localhost:8080/test/find?bannerId=1");

        //3.发起请求获取响应
        CloseableHttpResponse response = httpClient.execute(httpGet);

        //获取响应体
        HttpEntity responseEntity = response.getEntity();

        System.out.println(EntityUtils.toString(responseEntity));

    }

添加请求头

//添加请求头
httpGet.addHeader("api-key","1234466");

3.带参数的GET请求

@Test
    public void test2() throws Exception {

        //1.创建HttpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();

        //2.创建get请求
        URI uri = new URIBuilder("http://localhost:8080/test/find").setParameter("bannerId", "1").build();

        HttpGet httpGet=new HttpGet(uri);

        //3.发起请求获取响应
        CloseableHttpResponse response = httpClient.execute(httpGet);

        //获取响应体
        HttpEntity responseEntity = response.getEntity();

        System.out.println(EntityUtils.toString(responseEntity));

    }

4.POST请求
@Test
    public void test3() throws Exception {

        //1.创建HttpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();

        //2.创建post请求
        HttpPost httpPost=new HttpPost("http://localhost:8080/test/add");


        //封装post请求的参数
        List<NameValuePair> pairList=new ArrayList<NameValuePair>();
        pairList.add(new BasicNameValuePair("name","aa"));
        pairList.add(new BasicNameValuePair("position","1"));

        UrlEncodedFormEntity entity=new UrlEncodedFormEntity(pairList);

        //设置请求体
        httpPost.setEntity(entity);

        //3.发起请求获取响应
        CloseableHttpResponse response = httpClient.execute(httpPost);

        //获取响应体
        HttpEntity responseEntity = response.getEntity();

        System.out.println(EntityUtils.toString(responseEntity));

    }

5.POST发送json

String body="{\"username\":\"ntxz\",\"password\":\"123456\"}";
try {
            HttpClient httpClient = HttpClients.createDefault();
            HttpPost httpPost = new HttpPost("http://127.0.0.1:6676/user/localLogin");
            httpPost.addHeader("Content-Type", "application/json;charset=utf8");
            
            httpPost.setEntity(new StringEntity(body, StandardCharsets.UTF_8));
            //发送请求获取响应
            HttpResponse response = httpClient.execute(httpPost);
            //获取所有响应头
            Header[] tokens = response.getAllHeaders();
            for (Header header : tokens) {
                if (header.getName().equals("token")){
                    System.out.println(header.getValue());
                }
            }
            System.out.println(EntityUtils.toString(response.getEntity()));

        }catch (Exception e){
            System.out.println("请求异常, 错误信息为: "+e.getMessage());
        }
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值