導入依賴html
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> </dependency>
編寫測試類java
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 (); } } }
運行測試結果apache
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------------------less
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方法ui
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 (); } } }
運行結果url
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}