HttpClient隶属于Apache的HttpComponents项目,它提供了更加丰富且高效的HTTP访问方式,具备几个特色纯Java实现、支持HTTPS、支持通过CONNECT方法建立管道链接、支持Basic、Digest和NTLM等鉴权方式,开源等等
配置Maven
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.12</version>
</dependency>
GET方法测试代码
GET方法可用于获取指定资源,,GET方法的URL中可以携带参数
package org.davieyang.httpinterfacetest;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class TestGetInterface {
/**
* 发送GET请求
* @param args: 入口参数
*/
public static void main(String[] args){
CloseableHttpClient client = HttpClients.createDefault();
// 通过调用HttpClients的静态方法构建一个HTTP客户端
HttpGet httpGet1 = new HttpGet("http://localhost:8080/mobilePhone?model=iPhone+6S");
// GET请求通过实例化HttpGet类来实现,其他HTTP请求也有相应的类,即HttpDelete/HttpHead/HttpOptions/HttpPatch/HttpPost/HttpPut
// HttpTrace
// 使用String的方式构建Get请求,语法简洁,但结构不清晰
URI uri = null;
try{
uri = new URIBuilder().setScheme("http").setHost("localhost").setPort(8080).setPath("/mobilePhone")
.setParameter("model", "iPhone 6S").build();
} catch (URISyntaxException e){
e.printStackTrace();
}
// 使用URI构建,结构清晰,但过程复杂,且构建过程可能造成URI语法异常
// 当遇到空格的时候,第一种方式需要手动将空格替换为+号,第二种方式会自动编码
HttpGet httpGet2 = new HttpGet(uri);
CloseableHttpResponse response = null;
try {
response = client.execute(httpGet1);
System.out.println(EntityUtils.toString(response.getEntity()));
response = client.execute(httpGet2);
System.out.println(EntityUtils.toString(response.getEntity()));
} catch (Exception e){
e.printStackTrace();
}
// 通过HTTP客户端执行请求得到服务器响应,并将结果打印到控制台
}
}
POST方法测试代码
POST方法用于创建或修改指定资源,例如表单提交文件上传等,该方法既可以在URL中携带参数,也可以在请求体中携带参数
package org.davieyang.httpinterfacetest;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class TestPostInterface {
public static void main(String[] args){
CloseableHttpClient client = HttpClients.createDefault();
// 构建并发送RESTful POST请求
HttpPost restPost = new HttpPost("http://localhost:8080/mobilePhone");
// 使用StringEntity设置请求体和Content-Type
StringEntity stringEntity = new StringEntity(
"{\"brand\":\"Motorola\", \"model\":\"motoZPlay\", \"os\":\"ANDROID\"}", ContentType.APPLICATION_JSON);
restPost.setEntity(stringEntity);
CloseableHttpResponse response = null;
try{
response = client.execute(restPost);
System.out.println(EntityUtils.toString(response.getEntity()));
} catch (Exception e){
e.printStackTrace();
}
// 构建和发送SOAP请求
HttpPost soapPost = new HttpPost("http://localhost:8080/MobilePhones");
// 使用String方式构建请求
String soapString = "<soapenv:Envelope "
+ "xmlns:soapenv=\"http://achemas.xmlsoap.org/soap/envelope/\""
+ "xmlns:hi=\" http://www.davieyang.com\">\r\n"
+ "<soapenv:Header/>\r\n"
+ "<soapenv:Body>\r\n"
+ "<hi:getMobilePhoneRequest>\r\n"
+ "<hi:model>iPhone 6S</hi:model>\r\n"
+ "</hi:getMobilePhoneRequest>\r\n"
+ "</soapenv:Body>\r\n"
+ "</soapenv:Envelope>";
// 使用StringEntity设置请求体和Content-Type
stringEntity = new StringEntity(soapString, ContentType.TEXT_XML);
soapPost.setEntity(stringEntity);
response = null;
try{
response = client.execute(soapPost);
System.out.println(EntityUtils.toString(response.getEntity()));
} catch (Exception e){
e.printStackTrace();
}
}
}