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}
    

      

     

  • 相关阅读:
    x86 hook跳转内存地址转换计算框架
    win10180317134配合VS2017搭建WDK驱动开发环境
    C/C++字节特征码转换自动格式化文本工具算法源码
    jar包直接拷贝到WEBINF/lib下和以userLibrary引入的区别
    java.io.IOException: Cannot rename original file to %TOMCAT_HOME%\conf\tomcatusers.xml.old
    Tomcat5 和 Tomcat6 类加载器架构
    Tomcat version * only supports J2EE * Web modules
    tomcat启动时报错:org.apache.catalina.core.AprLifecycleListener init
    tomcat 不能正常启动,双击 startup.bat 一闪而过
    java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
  • 原文地址:https://www.cnblogs.com/zhou-test/p/10229104.html
Copyright © 2011-2022 走看看