JAVA后端《HttpClient》

1.Apache HttpClien总结

在这里插入图片描述

2.Apache HttpClien 测试

 public static void testHttpClientGet(){
        CloseableHttpClient httpClient =null;
        CloseableHttpResponse response =null;
        HttpEntity entity = null;
        try {
                 httpClient = HttpClients.createDefault();
                 HttpGet httpGet = new HttpGet("http://127.0.0.1:9005/user_all?id=2");
                 httpGet.setHeader("Connection","keep-alive");

                 response = httpClient.execute(httpGet);
                 System.out.println("response = " + response.getStatusLine());
                 
                 entity = response.getEntity();
                 String result = EntityUtils.toString(entity);
                 System.out.println("result = " + result);
                
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                //关闭流
                EntityUtils.consume(entity);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (httpClient!=null){
                try {
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (response!=null){
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //设置连接超时时间 单位毫秒。
    private static final int CONNECT_TIMEOUT = 5000;
    //获取数据的超时时间 单位毫秒。
    private static final int SOCKET_TIMEOUT = 5000;

    public static void testHttpClientPost() throws UnsupportedEncodingException {

        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost post = new HttpPost("http://127.0.0.1:9005/user_form");
       // post.setHeader("connection","keep-alive");
        RequestConfig requestConfig =  RequestConfig.custom()
                                      .setConnectTimeout(CONNECT_TIMEOUT)
                                      .setSocketTimeout(SOCKET_TIMEOUT).build();
        post.setConfig(requestConfig);

        //发送 json {"id":5,"username":"fuck","password":"123456"}
       /* StringEntity stringEntity = new StringEntity("{\"id\":5,\"username\":\"fuck\",\"password\":\"123456\"}");
          stringEntity.setContentType("application/json;charset=UTF-8");
          post.setEntity(stringEntity);*/

         //发送模拟from表单
        List<BasicNameValuePair> params = new ArrayList<>();
        params.add(new BasicNameValuePair("start","1"));
        params.add(new BasicNameValuePair("end","3"));
        post.setEntity(new UrlEncodedFormEntity(params, StandardCharsets.UTF_8));

        CloseableHttpResponse response = null;
        try {
                response = httpClient.execute(post);
                String string = EntityUtils.toString(response.getEntity());
                System.out.println("string = " + string);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (httpClient!=null){
                try {
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (response!=null){
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值