导入依赖
<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
原文地址:https://www.cnblogs.com/zhou-test/p/10229104.html