requests

Sonatype helps open source projects to set up Maven repositories on https://oss.sonatype.org/

License

License

Categories

Categories

Net
GroupId

GroupId

cn.networklab
ArtifactId

ArtifactId

requests
Last Version

Last Version

1.0.1
Release Date

Release Date

Type

Type

jar
Description

Description

requests
Sonatype helps open source projects to set up Maven repositories on https://oss.sonatype.org/
Project URL

Project URL

http://maven.apache.org
Source Code Management

Source Code Management

https://github.com/smartning/requests.git

Download requests

How to add to project

<!-- https://jarcasting.com/artifacts/cn.networklab/requests/ -->
<dependency>
    <groupId>cn.networklab</groupId>
    <artifactId>requests</artifactId>
    <version>1.0.1</version>
</dependency>
// https://jarcasting.com/artifacts/cn.networklab/requests/
implementation 'cn.networklab:requests:1.0.1'
// https://jarcasting.com/artifacts/cn.networklab/requests/
implementation ("cn.networklab:requests:1.0.1")
'cn.networklab:requests:jar:1.0.1'
<dependency org="cn.networklab" name="requests" rev="1.0.1">
  <artifact name="requests" type="jar" />
</dependency>
@Grapes(
@Grab(group='cn.networklab', module='requests', version='1.0.1')
)
libraryDependencies += "cn.networklab" % "requests" % "1.0.1"
[cn.networklab/requests "1.0.1"]

Dependencies

compile (5)

Group / Artifact Type Version
org.apache.httpcomponents : httpclient jar 4.4.1
org.apache.httpcomponents : httpcore jar 4.4.1
com.google.code.gson : gson jar 2.2.4
org.slf4j : slf4j-api jar 1.7.5
ch.qos.logback : logback-classic jar 1.1.7

test (2)

Group / Artifact Type Version
junit : junit jar 4.12
org.hamcrest : hamcrest-library jar 1.3

Project Modules

There are no modules declared in this project.

Requests

==作者: 徐宁==

==Email: [email protected]==

[TOC]

简介

本项目为java版本的requests实现,为Http请求客户端实现,支持rest,内部实现连接池,支持多样化配置,对项目无依赖,即插即拔,能够像python的requests那样快速开发而又不失灵活且无侵入。

1.快速实践

1-1.maven依赖

<dependency>
    <groupId>cn.networklab</groupId>
    <artifactId>requests</artifactId>
    <version>${version}</version>
</dependency>

1-2.最佳实践

public class QuickStartTest {
    public static Requests client;
    private static final Logger LOGGER = LoggerFactory.getLogger(QuickStartTest.class);
    /**
     * 第一步: 实例化client对象,建议设为单例
     */
    @Before
    public void before() {
        client = new RequestImpl();
    }
    /**
     * 第二步:发送请求,获取
     */
    @Test
    public void sendSimpleRequest() {
        String res = client.get("http://www.baidu.com").text();
        LOGGER.info(res);
    }
    @After
    public void destory(){
        LOGGER.info("client request end");
    }
}

2.请求客户端Client

客户端以借口Requests引用,框架已经实现了客户端,就是RequestsImpl,当然您可以自己去重写,您可以直接代码中引用或者使用spring注入的方式,因为request客户端消耗一定的资源,建议客户端构建为单例。调用如下代码所示:
Requests requestClient = new RequestImpl();

3.Requests支持的请求方法

1.GET:       RequestClient.get({url}, {params}, {headers})
2.POST:	  RequestClient.post({url}, {params}, {bosy}, {headers})
3.PUT:       RequestClient.post({url}, {params}, {bosy}, {headers})
4.DELETE:	RequestClient.post({url}, {params}, {bosy}, {headers})

4.结果类型

1.text(文本):	RequestClient.get({url}, {params}, {headers}).text();
2.json(json):	RequestClient.get({url}, {params}, {headers}).json();
3.inputstream(流): RequestClient.get({url}, {params}, {headers}).inputStream();

5.框架流程

Request->RequestClient->HttpConnectionManager->HttpMethod->HttpResponse

Versions

Version
1.0.1
1.0.0