java http delete_HttpClient HTTP DELETE请求方法示例

本教程演示如何使用Apache HttpClient 4.5创建Http DELETE请求。 HTTP DELETE请求方法请求删除由URI指定的资源。

Maven依赖关系

我们使用maven来管理依赖关系,并使用Apache HttpClient 4.5版本。 将以下依赖项添加到您的项目中,以便创建HTTP DELETE请求方法。

pom.xml 文件的内容如下 -

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0

http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.yiibai.httpclient.httmethods

http-get

1.0.0-SNAPSHOT

https://memorynotfound.com

httpclient - ${project.artifactId}

org.apache.httpcomponents

httpclient

4.5.2

maven-compiler-plugin

3.5.1

1.8

1.8

HTTP Delete请求方法示例

在以下示例中,我们将数据发布到资源URL:http://httpbin.org/delete 。 该资源确认数据并返回一个JSON对象,我们只需将其打印到控制台。 注意:使用Java7的try-with-resources来自动处理关闭ClosableHttpClient。 接下来使用Java 8的lambda作为ResponseHandler。 在这里,根据Http状态代码判断返回状态,当一切正常时,我们会将解析的响应正文返回给String。 当状态码不是所期望的时候,将抛出一个ClientProtocolException,表明Http DELETE请求方法失败。 最后,我们将响应主体打印到控制台。

文件:HttpDeleteRequestMethodExample.java -

package com.yiibai.httpdemo;

import org.apache.http.HttpEntity;

import org.apache.http.client.ClientProtocolException;

import org.apache.http.client.ResponseHandler;

import org.apache.http.client.methods.HttpDelete;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClients;

import org.apache.http.util.EntityUtils;

import java.io.IOException;

/**

* This example demonstrates the use of {@link HttpDelete} request method.

*/

public class HttpDeleteRequestMethodExample {

public static void main(String... args) throws IOException {

try (CloseableHttpClient httpclient = HttpClients.createDefault()) {

HttpDelete httpDelete = new HttpDelete("http://httpbin.org/delete");

System.out.println("Executing request " + httpDelete.getRequestLine());

// Create a custom response handler

ResponseHandler responseHandler = response -> {

int status = response.getStatusLine().getStatusCode();

if (status >= 200 && status < 300) {

HttpEntity entity = response.getEntity();

return entity != null ? EntityUtils.toString(entity) : null;

} else {

throw new ClientProtocolException("Unexpected response status: " + status);

}

};

String responseBody = httpclient.execute(httpDelete, responseHandler);

System.out.println("----------------------------------------");

System.out.println(responseBody);

}

}

}

执行上面示例代码,得到以下结果 -

Executing request DELETE http://httpbin.org/delete HTTP/1.1

----------------------------------------

{

"args": {},

"data": "",

"files": {},

"form": {},

"headers": {

"Accept-Encoding": "gzip,deflate",

"Connection": "close",

"Content-Length": "0",

"Host": "httpbin.org",

"User-Agent": "Apache-HttpClient/4.5.5 (Java/1.8.0_65)"

},

"json": null,

"origin": "112.67.166.104",

"url": "http://httpbin.org/delete"

}

¥ 我要打赏

纠错/补充

收藏

加QQ群啦,易百教程官方技术学习群

注意:建议每个人选自己的技术方向加群,同一个QQ最多限加 3 个群。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值