Commons-HttpClient is a component in the original Apache Commons project, the project has now been replaced HttpComponents; Http invoked as an alternative interface, the article describes its use. Used herein to software version: Java 1.8.0_191, Commons-HttpClient 3.1.
1, the server
See Java calls Http Interface (1) - write server
2, call
2.1, GET requests
public static void get() { try { String requestPath = "http://localhost:8080/webframe/demo/test/getUser?userId=1000&userName=" + URLEncoder.encode("李白", "utf-8"); HttpClient httpClient = new HttpClient(); GetMethod get = new GetMethod(requestPath); int status = httpClient.executeMethod(get); if (status == HttpStatus.SC_OK) { System.out.println ( "GET returns the result:" + get.getResponseBodyAsString ()); } else { System.out.println ( "the GET return a status code:" + Status); } } catch (Exception e) { e.printStackTrace (); } }
2.2, POST request (key-value pairs of data transmission)
public static void post() { try { String requestPath = "http://localhost:8080/webframe/demo/test/getUser"; HttpClient httpClient = new HttpClient(); PostMethod post = new PostMethod(requestPath); post.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8"); String param = "userId=1000&userName=李白"; post.setRequestEntity(new StringRequestEntity(param)); int status = httpClient.executeMethod(post); if (status == HttpStatus.SC_OK) { System.out.println ( "the POST returns the result:" + post.getResponseBodyAsString ()); } else { System.out.println ( "the POST return a status code:" + Status); } } catch (Exception e) { e.printStackTrace (); } }
2.3, POST request (JSON data transmission)
public static void post2() { try { String requestPath = "http://localhost:8080/webframe/demo/test/addUser"; HttpClient httpClient = new HttpClient(); PostMethod post = new PostMethod(requestPath); post.addRequestHeader("Content-type", "application/json"); String param = "{\"userId\": \"1001\",\"userName\":\"杜甫\"}"; post.setRequestEntity(new StringRequestEntity(param)); int status = httpClient.executeMethod(post); if (status == HttpStatus.SC_OK) { System.out.println ( "the POST returns the result:" + post.getResponseBodyAsString ()); } else { System.out.println ( "the POST return a status code:" + Status); } } catch (Exception e) { e.printStackTrace (); } }
2.4, upload a file and send the data key
public static void multi() { try { String requestPath = "http://localhost:8080/webframe/demo/test/multi"; HttpClient httpClient = new HttpClient(); PostMethod post = new PostMethod(requestPath); File file = new File("d:/a.jpg"); Part[] parts = {new FilePart("file", file), new StringPart("param1", "参数1", "utf-8"), new StringPart("param2", "参数2", "utf-8")}; post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams())); int status = httpClient.executeMethod(post); if (status == HttpStatus.SC_OK) { System.out.println ( "Multi returns the result:" + post.getResponseBodyAsString ()); } else { System.out.println ( "Multi return a status code:" + Status); } } catch (Exception e) { e.printStackTrace (); } }
2.5, complete example
package com.inspur.http; import java.io.File; import java.net.URLEncoder; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.StringRequestEntity; import org.apache.commons.httpclient.methods.multipart.FilePart; import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity; import org.apache.commons.httpclient.methods.multipart.Part; import org.apache.commons.httpclient.methods.multipart.StringPart; /** * * Call Http interfaces Commons-HttpClient * */ public class CommonsHttpClientCase { /** * GET request */ public static void get() { try { String requestPath = "http://localhost:8080/webframe/demo/test/getUser?userId=1000&userName=" + URLEncoder.encode("李白", "utf-8"); HttpClient httpClient = new HttpClient(); GetMethod get = new GetMethod(requestPath); int status = httpClient.executeMethod(get); if (status == HttpStatus.SC_OK) { System.out.println ( "GET returns the result:" + get.getResponseBodyAsString ()); } else { System.out.println ( "the GET return a status code:" + Status); } } catch (Exception e) { e.printStackTrace (); } } /** * POST request (key-value pairs of data transmission) */ public static void post() { try { String requestPath = "http://localhost:8080/webframe/demo/test/getUser"; HttpClient httpClient = new HttpClient(); PostMethod post = new PostMethod(requestPath); post.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8"); String param = "userId=1000&userName=李白"; post.setRequestEntity(new StringRequestEntity(param)); int status = httpClient.executeMethod(post); if (status == HttpStatus.SC_OK) { System.out.println ( "the POST returns the result:" + post.getResponseBodyAsString ()); } else { System.out.println ( "the POST return a status code:" + Status); } } catch (Exception e) { e.printStackTrace (); } } /** * POST request (data transmission json) */ public static void post2() { try { String requestPath = "http://localhost:8080/webframe/demo/test/addUser"; HttpClient httpClient = new HttpClient(); PostMethod post = new PostMethod(requestPath); post.addRequestHeader("Content-type", "application/json"); String param = "{\"userId\": \"1001\",\"userName\":\"杜甫\"}"; post.setRequestEntity(new StringRequestEntity(param)); int status = httpClient.executeMethod(post); if (status == HttpStatus.SC_OK) { System.out.println ( "the POST returns the result:" + post.getResponseBodyAsString ()); } else { System.out.println ( "the POST return a status code:" + Status); } } catch (Exception e) { e.printStackTrace (); } } /** * Upload the file and send the data key */ public static void multi() { try { String requestPath = "http://localhost:8080/webframe/demo/test/multi"; HttpClient httpClient = new HttpClient(); PostMethod post = new PostMethod(requestPath); File file = new File("d:/a.jpg"); Part[] parts = {new FilePart("file", file), new StringPart("param1", "参数1", "utf-8"), new StringPart("param2", "参数2", "utf-8")}; post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams())); int status = httpClient.executeMethod(post); if (status == HttpStatus.SC_OK) { System.out.println ( "Multi returns the result:" + post.getResponseBodyAsString ()); } else { System.out.println ( "Multi return a status code:" + Status); } } catch (Exception e) { e.printStackTrace (); } } public static void main(String[] args) { get(); post(); Post2 (); multi(); } }