HttpClient官网
HttpClient - HttpClient Home
每个对应版本都有 快速开始的示例
maven项目
pom依赖
<dependencies> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.12</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>RELEASE</version> <scope>compile</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency> </dependencies>
在测试类里测试
package com.example;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* @author hrui
* @date 2023/11/17 12:35
*/
public class HttpClientTestTest{
@Test
public void testGet(){
//发起get请求,编程方式处理http请求
String url="https://restapi.amap.com/v3/ip?key=0113a13c88697dcea6a445584d535837&ip=60.25.188.64";
//1.创建HttpClient对象
CloseableHttpClient client= HttpClients.createDefault();
//2.创建HttpGet对象
HttpGet httpGet=new HttpGet(url);
//httpGet.setHeader("token", "xxxxxx");可能对方网站需要某些校验的时候,在请求头设置值
try{
//3.执行请求,使用client对象的方法,执行请求后获取返回结果
CloseableHttpResponse response = client.execute(httpGet);
// 处理响应 200成功
if(response.getStatusLine().getStatusCode()== HttpStatus.SC_OK){
// 如果需要处理响应内容,可以在这里获取响应实体
// HttpEntity responseEntity = response.getEntity();
// InputStream content = responseEntity.getContent();//返回流原因是不只字符串 还有视频 音频 图片等等
//如果确定是字符串也封装了对应方法
String json = EntityUtils.toString(response.getEntity());
System.out.println(json);
}
}catch (Exception e){
e.printStackTrace();
}finally {
if(client!=null){
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void test02(){
//1.创建HttpClient对象
CloseableHttpClient client= HttpClients.createDefault();
//2.创建HttpGet对象
String url="https://restapi.amap.com/v3/ip";
HttpPost httpPost=new HttpPost(url);
//httpPost.setHeader("token", "xxxxxx");可能对方网站需要某些校验的时候,在请求头设置值
//3.准备参数
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("key", "0113a13c88697dcea6a445584d535837"));
nvps.add(new BasicNameValuePair("ip", "60.25.188.64"));
try {
//4.设置使用参数
httpPost.setEntity(new UrlEncodedFormEntity(nvps));//构建表单 name=xxx&password=xxx形式
// httpPost.setHeader("Content-Type", "application/json");
// // 设置请求体(如果有)
// String requestBody = "{\"key\": \"value\"}";
// httpPost.setEntity(new StringEntity(requestBody));//如果是JSON
//5.执行请求
CloseableHttpResponse response = client.execute(httpPost);
if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
String s = EntityUtils.toString(response.getEntity());
System.out.println(s);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if(client!=null){
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}