码迷,mamicode.com
首页 > Web开发 > 详细

HttpClient

时间:2019-01-06 16:30:04      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:art   code   test   locate   ota   nts   client   public   ali   

导入依赖

 <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}

  

HttpClient

标签:art   code   test   locate   ota   nts   client   public   ali   

原文地址:https://www.cnblogs.com/zhou-test/p/10229104.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!