Introduction:
HttpClient is a subproject under Apache Jakarta Common, which is used to provide an efficient, up-to-date and feature rich client programming toolkit supporting HTTP protocol, and it supports the latest version and suggestions of HTTP protocol.
usage method:
1. Create a client CloseableHttpClient
2. Create an instance of the request method and specify the URL
If you need to send a GET request, create an HttpGet object; if you need to send a POST request, create an HttpPost object.
3. If you need to send the request parameters, you can call the common setParams(HttpParams params) method of HttpGet and HttpPost to add the request parameters. For HttpPost objects, you can also call the setentity (httpentity) method to set the request parameters. You can set the request parameter type to JSON through the setheader ("content type", "application / JSON; charset = UTF-8") method,
4. Send the request, call httpclient.execute (the object of the request instance), and return a Response object
5. Call getAllHeaders(), getHeaders(String name) and other methods of HttpResponse to get the response headers of the server;
Call getEntity() method of HttpResponse to get the HttpResponse object, which wraps the response content of the server. The program can get the response content of the server through this object
6. Release resources. The connection must be released no matter whether the execution method is successful or not
GET request instance
@Test public void testDemo01(){ try { //1 create Httpclient object, which is equivalent to opening browser CloseableHttpClient httpClient = HttpClients.createDefault(); //2 determine the request mode and request path, which is equivalent to entering the address in the browser HttpGet httpGet = new HttpGet("http://localhost:9090/user"); //3. Execute the request and get the response, which is equivalent to pressing enter after tapping the address. CloseableHttpResponse response = httpClient.execute(httpGet); // 4. Judge whether the status code is 200 if(response.getStatusLine().getStatusCode() == 200){ // 5 get response body content String str = EntityUtils.toString(response.getEntity()); System.out.println(str); } response.close(); httpClient.close(); } catch (IOException e) { e.printStackTrace(); }
POST request instance
public User Testlogin(User user){ CloseableHttpClient httpClient= null; CloseableHttpResponse response = null; try { //1. Create HttpClient httpClient = HttpClients.createDefault(); //2. Create a post request instance HttpPost httpPost = new HttpPost("http://localhost:9090/user/login"); //2.1 set the request type to json httpPost.setHeader("content-type","application/json;charset=utf-8"); //2.2 set request parameters (request body) String str = JSON.toJSONString(user); httpPost.setEntity(new StringEntity(str,"utf-8")); //3. Send request response = httpClient.execute(httpPost); //4. Judge the response status code as 200 if(response.getStatusLine().getStatusCode() == 200){ //5. Get the content of response body String string = EntityUtils.toString(response.getEntity()); User loginUser = JSON.parseObject(string,User.class); return loginUser; } } catch (Exception e) { e.printStackTrace(); } finally { try { //6. Release resources response.close(); httpClient.close(); } catch (IOException e) { e.printStackTrace(); } } return null; }