zoukankan      html  css  js  c++  java
  • HttpClient

    导入依赖

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

      编写测试类

    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.util.EntityUtils;
    import org.junit.Test;
    
    import java.io.IOException;
    
    public class HttpClientTest {
        @Test
        public void test(){
            HttpClient httpClient = HttpClients.createDefault ();
            HttpGet httpGet = new HttpGet ("http://115.28.108.130:8080/DemoController/getUserById?id=1");
            try {
                HttpResponse response = httpClient.execute (httpGet);
                HttpEntity entity = response.getEntity ();
                String s = EntityUtils.toString (entity);
                System.out.println (s);
            } catch (IOException e) {
                e.printStackTrace ();
            }
        }
    }
    

      

    运行测试结果

    15:39:52.888 [main] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager - Connection released: [id: 0][route: {}->http://115.28.108.130:8080][total kept alive: 1; route allocated: 1 of 2; total allocated: 1 of 20]
    {"code":200,"object":{"age":0,"email":"test@jd.com","id":0,"username":"小强"},"success":true}
    

      

    ------------------------------------------get方法入参通过URIBuilder------------------

    package com.longteng.lesson2;
    
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.client.utils.URIBuilder;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.util.EntityUtils;
    import org.junit.Test;
    
    import java.io.IOException;
    import java.net.URISyntaxException;
    
    public class HttpClientTest {
        @Test
        public void test() throws URISyntaxException {
            HttpClient httpClient = HttpClients.createDefault ();
            String url ="http://115.28.108.130:8080/DemoController/getUserById";
            try {
                URIBuilder uriBuilder =new URIBuilder (url);
                uriBuilder.addParameter ("id","1");
                HttpGet httpGet =new HttpGet (uriBuilder.build ());
                HttpResponse response = httpClient.execute (httpGet);
                HttpEntity entity = response.getEntity ();
                int statusCode = response.getStatusLine ().getStatusCode ();
                System.out.println (statusCode);
                String s = EntityUtils.toString (entity);
                System.out.println (s);
            } catch (IOException e) {
                e.printStackTrace ();
            }
        }
    }

    返回结果

    16:20:14.888 [main] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager - Connection released: [id: 0][route: {}->http://115.28.108.130:8080][total kept alive: 1; route allocated: 1 of 2; total allocated: 1 of 20]
    {"code":200,"object":{"age":0,"email":"test@jd.com","id":0,"username":"小强"},"success":true}
    

     ---------------------------POST方法

    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.client.utils.URIBuilder;
    import org.apache.http.entity.StringEntity;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.util.EntityUtils;
    import org.junit.Test;
    
    import javax.swing.text.html.parser.Entity;
    import java.io.IOException;
    import java.io.UnsupportedEncodingException;
    import java.net.URISyntaxException;
    
    public class HttpClientTest {
      
        @Test
        public void test2(){
            String url ="http://115.28.108.130:8080/DemoController/createUser";
            HttpClient httpClient = HttpClients.createDefault ();
            HttpPost httpPost=new HttpPost (url);
            String param ="{"username":"小强","age":10}";
            try {
                StringEntity stringEntity = new StringEntity (param);
                httpPost.setEntity (stringEntity);
                HttpResponse response = httpClient.execute (httpPost);
                System.out.println (response.getStatusLine ().getStatusCode ());
                System.out.println (EntityUtils.toString (response.getEntity ()).toString ());
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace ();
            } catch (ClientProtocolException e) {
                e.printStackTrace ();
            } catch (IOException e) {
                e.printStackTrace ();
            }
    
        }
    }
    

      运行结果

    16:42:20.727 [main] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager - Connection released: [id: 0][route: {}->http://115.28.108.130:8080][total kept alive: 1; route allocated: 1 of 2; total allocated: 1 of 20]
    {"code":200,"msg":"创建成功","success":true}
    

      

     

  • 相关阅读:
    Spring@Profile注解
    day 32 子进程的开启 及其用法
    day 31 udp 协议SOCK_DGRAM
    day 30 客户端获取cmd 命令的步骤
    day 29 socket 理论
    day 29 socket 初级版
    有关 组合 继承
    day 27 多态 接口 类方法 静态方法 hashlib 摘要算法模块
    新式类和经典类的区别
    day 28 hasattr getattr serattr delattr 和带__内置__ 类的内置方法
  • 原文地址:https://www.cnblogs.com/zhou-test/p/10229104.html
Copyright © 2011-2022 走看看