导入依赖
<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}