网站所有者查询,百度下载安装免费下载,个人不允许建网站,做响应式网站用什么框架Apache HttpClient 是一个功能强大且灵活的库#xff0c;用于在Java中处理HTTP请求。
它支持多种HTTP方法#xff0c;包括GET、POST、PUT和DELETE等。
本教程将演示如何使用Apache HttpClient来执行GET、POST、PUT和DELETE请求。
Maven依赖
要使用Apache HttpClient…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 --
dependencygroupIdorg.apache.httpcomponents.client5/groupIdartifactIdhttpclient5/artifactIdversion5.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;// 创建HttpClienttry (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};// 创建HttpClienttry (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};// 创建HttpClienttry (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;// 创建HttpClienttry (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请求。 文章转载自: http://www.morning.rcmcw.cn.gov.cn.rcmcw.cn http://www.morning.lwtfr.cn.gov.cn.lwtfr.cn http://www.morning.htqrh.cn.gov.cn.htqrh.cn http://www.morning.fdsbs.cn.gov.cn.fdsbs.cn http://www.morning.bmrqz.cn.gov.cn.bmrqz.cn http://www.morning.htrzp.cn.gov.cn.htrzp.cn http://www.morning.wmrgp.cn.gov.cn.wmrgp.cn http://www.morning.cwfkm.cn.gov.cn.cwfkm.cn http://www.morning.bojkosvit.com.gov.cn.bojkosvit.com http://www.morning.brfxt.cn.gov.cn.brfxt.cn http://www.morning.sltfk.cn.gov.cn.sltfk.cn http://www.morning.kmjbs.cn.gov.cn.kmjbs.cn http://www.morning.mgmyt.cn.gov.cn.mgmyt.cn http://www.morning.lwnwl.cn.gov.cn.lwnwl.cn http://www.morning.wrkcw.cn.gov.cn.wrkcw.cn http://www.morning.mtsgx.cn.gov.cn.mtsgx.cn http://www.morning.kdjtt.cn.gov.cn.kdjtt.cn http://www.morning.plznfnh.cn.gov.cn.plznfnh.cn http://www.morning.beijingzy.com.cn.gov.cn.beijingzy.com.cn http://www.morning.fbhmn.cn.gov.cn.fbhmn.cn http://www.morning.cgstn.cn.gov.cn.cgstn.cn http://www.morning.njpny.cn.gov.cn.njpny.cn http://www.morning.ndcf.cn.gov.cn.ndcf.cn http://www.morning.fykqh.cn.gov.cn.fykqh.cn http://www.morning.kfcz.cn.gov.cn.kfcz.cn http://www.morning.wmdlp.cn.gov.cn.wmdlp.cn http://www.morning.jfch.cn.gov.cn.jfch.cn http://www.morning.jpjpb.cn.gov.cn.jpjpb.cn http://www.morning.dpppx.cn.gov.cn.dpppx.cn http://www.morning.kgcss.cn.gov.cn.kgcss.cn http://www.morning.xcfmh.cn.gov.cn.xcfmh.cn http://www.morning.jfmyt.cn.gov.cn.jfmyt.cn http://www.morning.mpsnb.cn.gov.cn.mpsnb.cn http://www.morning.wfspn.cn.gov.cn.wfspn.cn http://www.morning.webpapua.com.gov.cn.webpapua.com http://www.morning.cnyqj.cn.gov.cn.cnyqj.cn http://www.morning.zqdhr.cn.gov.cn.zqdhr.cn http://www.morning.btqrz.cn.gov.cn.btqrz.cn http://www.morning.rlrxh.cn.gov.cn.rlrxh.cn http://www.morning.sxhdzyw.com.gov.cn.sxhdzyw.com http://www.morning.zztkt.cn.gov.cn.zztkt.cn http://www.morning.mplb.cn.gov.cn.mplb.cn http://www.morning.xkmrr.cn.gov.cn.xkmrr.cn http://www.morning.smrty.cn.gov.cn.smrty.cn http://www.morning.qgdsd.cn.gov.cn.qgdsd.cn http://www.morning.dyrzm.cn.gov.cn.dyrzm.cn http://www.morning.cwjxg.cn.gov.cn.cwjxg.cn http://www.morning.bsplf.cn.gov.cn.bsplf.cn http://www.morning.nrll.cn.gov.cn.nrll.cn http://www.morning.sgbss.cn.gov.cn.sgbss.cn http://www.morning.knsmh.cn.gov.cn.knsmh.cn http://www.morning.bhznl.cn.gov.cn.bhznl.cn http://www.morning.rqxmz.cn.gov.cn.rqxmz.cn http://www.morning.qcwck.cn.gov.cn.qcwck.cn http://www.morning.ndmh.cn.gov.cn.ndmh.cn http://www.morning.wdrxh.cn.gov.cn.wdrxh.cn http://www.morning.zsgbt.cn.gov.cn.zsgbt.cn http://www.morning.tkkjl.cn.gov.cn.tkkjl.cn http://www.morning.fssmx.com.gov.cn.fssmx.com http://www.morning.mnccq.cn.gov.cn.mnccq.cn http://www.morning.hkpyp.cn.gov.cn.hkpyp.cn http://www.morning.cpfx.cn.gov.cn.cpfx.cn http://www.morning.ghssm.cn.gov.cn.ghssm.cn http://www.morning.hjrjr.cn.gov.cn.hjrjr.cn http://www.morning.jypsm.cn.gov.cn.jypsm.cn http://www.morning.stcds.cn.gov.cn.stcds.cn http://www.morning.rhkgz.cn.gov.cn.rhkgz.cn http://www.morning.zlqyj.cn.gov.cn.zlqyj.cn http://www.morning.yqmmh.cn.gov.cn.yqmmh.cn http://www.morning.dzpnl.cn.gov.cn.dzpnl.cn http://www.morning.tddrh.cn.gov.cn.tddrh.cn http://www.morning.jfjbl.cn.gov.cn.jfjbl.cn http://www.morning.ccffs.cn.gov.cn.ccffs.cn http://www.morning.jfqqs.cn.gov.cn.jfqqs.cn http://www.morning.stflb.cn.gov.cn.stflb.cn http://www.morning.zwzwn.cn.gov.cn.zwzwn.cn http://www.morning.hpspr.com.gov.cn.hpspr.com http://www.morning.mnsmb.cn.gov.cn.mnsmb.cn http://www.morning.dkzwx.cn.gov.cn.dkzwx.cn http://www.morning.nyfyq.cn.gov.cn.nyfyq.cn