要使用Apache HttpClient库来发送HTTP请求和接收响应,你需要按照以下步骤进行操作:
- 导入HttpClient库: 首先,在你的Java项目中添加Apache HttpClient库的依赖。如果你使用Maven作为项目构建工具,可以在
pom.xml
文件中添加以下依赖项:
<dependencies> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.10</version> </dependency> </dependencies>
xml复制代码
<dependencies> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.13</version> </dependency> </dependencies>
- 创建HttpClient实例: 创建HttpClient对象以执行HTTP请求。
java复制代码
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; CloseableHttpClient httpClient = HttpClientBuilder.create().build();
- 创建HTTP请求: 创建一个HttpUriRequest对象,以指定请求类型和URL。
java复制代码
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpUriRequest; // GET请求示例 HttpUriRequest request = new HttpGet("http://example.com/api"); // POST请求示例 HttpUriRequest request = new HttpPost("http://example.com/api");
- 设置请求参数: 如果需要,你可以设置请求头、请求体或其他请求参数。
java复制代码
// 设置请求头信息 request.addHeader("Content-Type", "application/json"); // 设置请求体 String requestBody = "{ \"key\": \"value\" }"; ((HttpPost) request).setEntity(new StringEntity(requestBody));
- 发送请求并获取响应: 使用HttpClient对象发送请求,并获得服务器的响应。
java复制代码
import org.apache.http.client.ResponseHandler; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.impl.client.BasicResponseHandler; CloseableHttpResponse response = httpClient.execute(request); // 使用BasicResponseHandler将响应内容转换为字符串 ResponseHandler<String> responseHandler = new BasicResponseHandler(); String responseBody = responseHandler.handleResponse(response);
- 处理响应: 对获得的响应进行处理,包括获取状态码、响应头和
这是一个示例
public static void main(String[] args) throws IOException {
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
// GET请求示例
HttpUriRequest requestGET = new HttpGet("http://localhost:8082/base/personbychineseName?unitchineseName=第三舰队");
System.out.println("requestGET = " + requestGET);
// POST请求示例
// HttpUriRequest requestPOST = new HttpPost("http://localhost:8082/base/listb");
HttpUriRequest requestPOST = new HttpPost("http://localhost:8082/unit/queryunitbybase");
// 设置请求头信息
requestPOST.addHeader("Content-Type", "application/json");
// 设置请求体
//String requestBody = "{ \"chineseName\": \"横须贺港\" }";
String requestBody = "{\"chineseName\":\"横须贺港\"}";
//String requestBody = "{\"chName\":\"高雄海軍基地\",\"type\":\"海基\",\"country\":\"台湾\",\"catagory\":\"蓝方\"}";
((HttpPost) requestPOST).setEntity(new StringEntity(requestBody,"UTF-8"));
System.out.println("requestPOST = " + requestPOST);
CloseableHttpResponse response = httpClient.execute(requestPOST);
//CloseableHttpResponse response = httpClient.execute(requestGET);
// 使用BasicResponseHandler将响应内容转换为字符串
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = responseHandler.handleResponse(response);
System.out.println("responseBody = " + responseBody);
}