Commons-HttpClient原来是Apache Commons项目下的一个组件,现已被HttpComponents项目所取代;作为调用Http接口的一种选择,本文介绍下其使用方法。文中所使用到的软件版本:Java 1.8.0_191、Commons-HttpClient 3.1。
1、服务端
2、调用
2.1、GET请求
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返回结果:" + get.getResponseBodyAsString()); } else { System.out.println("GET返回状态码:" + status); } } catch (Exception e) { e.printStackTrace(); } }
2.2、POST请求(发送键值对数据)
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("POST返回结果:" + post.getResponseBodyAsString()); } else { System.out.println("POST返回状态码:" + status); } } catch (Exception e) { e.printStackTrace(); } }
2.3、POST请求(发送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("POST返回结果:" + post.getResponseBodyAsString()); } else { System.out.println("POST返回状态码:" + status); } } catch (Exception e) { e.printStackTrace(); } }
2.4、上传文件及发送键值对数据
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返回结果:" + post.getResponseBodyAsString()); } else { System.out.println("multi返回状态码:" + status); } } catch (Exception e) { e.printStackTrace(); } }
2.5、完整例子
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; /** * * 通过Commons-HttpClient调用Http接口 * */ public class CommonsHttpClientCase { /** * GET请求 */ 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返回结果:" + get.getResponseBodyAsString()); } else { System.out.println("GET返回状态码:" + status); } } catch (Exception e) { e.printStackTrace(); } } /** * POST请求(发送键值对数据) */ 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("POST返回结果:" + post.getResponseBodyAsString()); } else { System.out.println("POST返回状态码:" + status); } } catch (Exception e) { e.printStackTrace(); } } /** * POST请求(发送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("POST返回结果:" + post.getResponseBodyAsString()); } else { System.out.println("POST返回状态码:" + status); } } catch (Exception e) { e.printStackTrace(); } } /** * 上传文件及发送键值对数据 */ 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返回结果:" + post.getResponseBodyAsString()); } else { System.out.println("multi返回状态码:" + status); } } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { get(); post(); post2(); multi(); } }