apache httpclient 是一个功能强大且灵活的库,用于在java中处理http请求。
它支持多种http方法,包括get、post、put和delete等。
本教程将演示如何使用apache httpclient来执行get、post、put和delete请求。
maven依赖
要使用apache httpclient,您需要在pom.xml文件中添加以下依赖项:
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents.client5/httpclient5 -->
<dependency>
<groupid>org.apache.httpcomponents.client5</groupid>
<artifactid>httpclient5</artifactid>
<version>5.3</version>
</dependency>示例场景
我们将创建简单的java类,这些类将向指定的url发送get、post、put和delete请求,并打印响应。
jsonplaceholder api
为了演示目的,我们将使用jsonplaceholder api,该api提供了一个虚拟的在线restful端点,用于测试和原型设计。
get请求
发送get请求的java类
创建一个名为httpclientgetexample的类,代码如下:
import org.apache.hc.client5.http.classic.methods.httpget;
import org.apache.hc.client5.http.classic.methods.closeablehttpresponse;
import org.apache.hc.client5.http.impl.classic.closeablehttpclient;
import org.apache.hc.client5.http.impl.classic.httpclients;
import org.apache.hc.core5.http.io.entity.entityutils;
public class httpclientgetexample {
public static void main(string[] args) {
string url = "https://jsonplaceholder.typicode.com/posts/1";
// 创建httpclient
try (closeablehttpclient httpclient = httpclients.createdefault()) {
// 创建httpget请求
httpget request = new httpget(url);
// 执行请求
try (closeablehttpresponse response = httpclient.execute(request)) {
// 获取http响应状态
system.out.println("response code: " + response.getcode());
// 获取http响应内容
string content = entityutils.tostring(response.getentity());
system.out.println("response content: \n" + content);
}
} catch (exception e) {
e.printstacktrace();
}
}
}示例输出
response code: 200
response content:
{
"userid": 1,
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
}
post请求
发送post请求的java类
创建一个名为httpclientpostexample的类,代码如下:
import org.apache.hc.client5.http.classic.methods.httppost;
import org.apache.hc.client5.http.classic.methods.closeablehttpresponse;
import org.apache.hc.client5.http.impl.classic.closeablehttpclient;
import org.apache.hc.client5.http.impl.classic.httpclients;
import org.apache.hc.core5.http.io.entity.stringentity;
import org.apache.hc.core5.http.contenttype;
import org.apache.hc.core5.http.io.entity.entityutils;
public class httpclientpostexample {
public static void main(string[] args) {
string url = "https://jsonplaceholder.typicode.com/posts";
string json = "{\"title\":\"foo\",\"body\":\"bar\",\"userid\":1}";
// 创建httpclient
try (closeablehttpclient httpclient = httpclients.createdefault()) {
// 创建httppost请求
httppost request = new httppost(url);
// 设置json负载
stringentity entity = new stringentity(json, contenttype.application_json);
request.setentity(entity);
// 设置头部
request.setheader("accept", "application/json");
request.setheader("content-type", "application/json");
// 执行请求
try (closeablehttpresponse response = httpclient.execute(request)) {
// 获取http响应状态
system.out.println("response code: " + response.getcode());
// 获取http响应内容
string content = entityutils.tostring(response.getentity());
system.out.println("response content: \n" + content);
}
} catch (exception e) {
e.printstacktrace();
}
}
}示例输出
response code: 201
response content:
{
"title": "foo",
"body": "bar",
"userid": 1,
"id": 101
}
put请求
发送put请求的java类
创建一个名为httpclientputexample的类,代码如下:
import org.apache.hc.client5.http.classic.methods.httpput;
import org.apache.hc.client5.http.classic.methods.closeablehttpresponse;
import org.apache.hc.client5.http.impl.classic.closeablehttpclient;
import org.apache.hc.client5.http.impl.classic.httpclients;
import org.apache.hc.core5.http.io.entity.stringentity;
import org.apache.hc.core5.http.contenttype;
import org.apache.hc.core5.http.io.entity.entityutils;
public class httpclientputexample {
public static void main(string[] args) {
string url = "https://jsonplaceholder.typicode.com/posts/1";
string json = "{\"id\":1,\"title\":\"foo\",\"body\":\"bar\",\"userid\":1}";
// 创建httpclient
try (closeablehttpclient httpclient = httpclients.createdefault()) {
// 创建httpput请求
httpput request = new httpput(url);
// 设置json负载
stringentity entity = new stringentity(json, contenttype.application_json);
request.setentity(entity);
// 设置头部
request.setheader("accept", "application/json");
request.setheader("content-type", "application/json");
// 执行请求
try (closeablehttpresponse response = httpclient.execute(request)) {
// 获取http响应状态
system.out.println("response code: " + response.getcode());
// 获取http响应内容
string content = entityutils.tostring(response.getentity());
system.out.println("response content: \n" + content);
}
} catch (exception e) {
e.printstacktrace();
}
}
}示例输出
response code: 200
response content:
{
"id": 1,
"title": "foo",
"body": "bar",
"userid": 1
}
delete请求
发送delete请求的java类
创建一个名为httpclientdeleteexample的类,代码如下:
import org.apache.hc.client5.http.classic.methods.httpdelete;
import org.apache.hc.client5.http.classic.methods.closeablehttpresponse;
import org.apache.hc.client5.http.impl.classic.closeablehttpclient;
import org.apache.hc.client5.http.impl.classic.httpclients;
import org.apache.hc.core5.http.io.entity.entityutils;
public class httpclientdeleteexample {
public static void main(string[] args) {
string url = "https://jsonplaceholder.typicode.com/posts/1";
// 创建httpclient
try (closeablehttpclient httpclient = httpclients.createdefault()) {
// 创建httpdelete请求
httpdelete request = new httpdelete(url);
// 执行请求
try (closeablehttpresponse response = httpclient.execute(request)) {
// 获取http响应状态
system.out.println("response code: " + response.getcode());
// 获取http响应内容
string content = entityutils.tostring(response.getentity());
system.out.println("response content: \n" + content);
}
} catch (exception e) {
e.printstacktrace();
}
}
}示例输出
response code: 200
response content:
{}
额外配置
- 设置自定义头部:可以通过调用请求对象(如httpget、httppost、httpput、httpdelete)上的
setheader方法来设置自定义头部。 - 处理重定向:默认情况下,apache httpclient会自动处理重定向。您可以使用自定义的
httpclientbuilder来自定义这种行为。 - 设置超时:可以使用
requestconfig来设置连接和套接字超时。
结论
使用apache httpclient来执行get、post、put和delete http请求非常方便。
通过遵循本教程,您现在应该能够创建并执行这些类型的请求,处理响应,并定制http请求和响应过程。
apache httpclient提供了一整套功能,使其成为处理java应用程序中http操作的优秀选择。
jsonplaceholder api作为一个实用且方便的来源,适合用来测试和原型化您的http请求。
到此这篇关于如何使用apache httpclient来执行get、post、put和delete请求的文章就介绍到这了,更多相关apache httpclient执行get、post、put和delete请求内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论