HttpComponents (http 客户端) 常用类简介


http://hc.apache.org/
阿帕奇的开源项目。用于Http通信。
<dependency>
	<groupId>org.apache.httpcomponents</groupId>
	<artifactId>httpclient</artifactId>
	<version>4.5.1</version>
</dependency>

1.client

org.apache.http.client. HttpClient
接口。
CloseableHttpClient org.apache.http.impl.client.HttpClients. createDefault()
此方法可以拿到HttpClient对象。它是线程安全的。

2.request

org.apache.http. HttpRequest
接口。实现类有HttpGet、HttpPost等。
org.apache.http.client.methods. HttpUriRequest
继承了HttpRequest接口的接口。
void org.apache.http.message.AbstractHttpMessage. addHeader(String name, String value)
添加请求的头部信息。
org.apache.http.client.methods. HttpGet
代表HttpGet请求。
org.apache.http.client.methods. HttpPost
代表HttpPost请求。

2.1 get请求

org.apache.http.client.methods.HttpGet. HttpGet(String uri)
HttpGet请求的构造函数。

2.2 post请求

org.apache.http.client.methods.HttpPost. HttpPost(String uri)
HttpPost请求的构造函数。
org.apache.http.entity.StringEntity. StringEntity(String string) 
http报文body的格式是字符串。用于构造json、xml类post请求。

3.设置参数

Builder org.apache.http.client.config.RequestConfig. custom()
拿到builder。
Builder org.apache.http.client.config.RequestConfig.Builder. setSocketTimeout(int socketTimeout)
设置socket链接超时。
Builder org.apache.http.client.config.RequestConfig.Builder. setConnectionTimeout(int connectionRequestTimeout)
设置http连接超时。socket超时是http超时的充分不必要条件。
Builder org.apache.http.client.config.RequestConfig.Builder. setConnectionRequestTimeout(int connectionRequestTimeout)
设置请求发出前的超时时间。适用于用连接池,连接池占满的情况。
RequestConfig org.apache.http.client.config.RequestConfig.Builder. build()
至此拿到了RequestConfig 对象。
void org.apache.http.client.methods.HttpRequestBase. setConfig( RequestConfigconfig)
设置连接超时等在内的参数。RequestConfig对象的生成见下行。
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(2000).build();

4.execute

CloseableHttpResponse org.apache.http.impl.client.CloseableHttpClient. execute(HttpUriRequest request) 
执行请求并返回结果,是同步函数,需要等待。

5.response

org.apache.http.client.methods. CloseableHttpResponse
接口。
StatusLine org.apache.http.HttpResponse. getStatusLine()
获取状态栏。
HttpEntity org.apache.http.HttpResponse. getEntity()
获取消息实体。
InputStream org.apache.http.HttpEntity. getContent()
获取内容。如果是文本数据,通常这样来拿。
Scanner scanner = new Scanner(instream, "utf-8");
void org.apache.http.util.EntityUtils. consume(HttpEntity entity) 
调用HttpEntity.getContent()就得到了InputStream,此方法用于把流消费完,然后关掉这个InputStream。

6.代码



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Http请求的工具有很多选择,其中比较常用的是Apache HttpClient和Spring的RestTemplate。Apache HttpClient是一个功能强大的HTTP客户端库,可以发送和接收HTTP请求。而Spring的RestTemplate是基于HttpClient封装的更高级的HTTP客户端,提供了更便捷的API和更全面的功能。 在使用Apache HttpClient时,你需要添加相应的依赖。在maven中,可以使用以下依赖添加HttpClient: ``` <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.12</version> </dependency> ``` 使用Apache HttpClient发送GET请求的示例代码如下: ```java CloseableHttpClient httpClient = HttpClientBuilder.create().build(); HttpGet request = new HttpGet("http://example.com/api/resource"); CloseableHttpResponse response = httpClient.execute(request); try { HttpEntity entity = response.getEntity(); if (entity != null) { // 处理响应结果 String result = EntityUtils.toString(entity); // ... } } finally { response.close(); } ``` 而使用Spring的RestTemplate时,你需要添加相应的依赖。在maven中,可以使用以下依赖添加RestTemplate: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> ``` 使用RestTemplate发送GET请求的示例代码如下: ```java RestTemplate restTemplate = new RestTemplate(); String result = restTemplate.getForObject("http://example.com/api/resource", String.class); ``` 以上是使用HttpClient和RestTemplate发送GET请求的简单示例,你可以根据实际需要进行进一步的封装和扩展。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值