1 Star 0 Fork 0

Edsuns / HttpRequest

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

HttpRequest

A small-size but efficient library makes http request easier for java.

Demo

Normal Usage

public class Demo {
    public static void main(String[] args) throws IOException {
        HttpRequest request = new HttpRequest(url)
                // set request headers
                .headers(HttpRequest.data("Cache-Control", "no-cache")
                        .data("Accept-Encoding", "deflate")
                )
                // set cookies
                .cookies(cookiesIn)
                // other configurations
                .timeout(2000)
                .followRedirects(true)
                // get request with url params
                .get(HttpRequest.data("name", "value")
                        // support for type conversion
                        .data("integer", 1)
                );
        int status = request.getStatus();// response status
        String body = request.getBody();// body content
        byte[] bodyBytes = request.getBodyBytes();// body bytes
        List<String> redirects = request.getRedirects();// redirect url list
        Map<String, List<String>> responseHeaders =
                request.getResponseHeaders();// response headers
        Map<String, String> cookies = request.getCookies();// all the cookies
    }
}

Request Methods

public class Demo {
    public static void main(String[] args) throws IOException {
        HttpRequest request = new HttpRequest(url)
                // post request with form data
                .exec(HttpRequest.Method.POST,// also support other request methods
                        HttpRequest.data("name", "value")
                                .data("integer", 1)
                );
        String body = request.getBody();
        Map<String, String> cookies = request.getCookies();
    }
}

Use Proxy

public class Demo {
    public static void main(String[] args) throws IOException {
        Proxy proxy = new Proxy(Proxy.Type.HTTP,
                new InetSocketAddress(proxyHost, proxyPort));
        HttpRequest request = new HttpRequest(url, proxy)
                .timeout(timeout).exec(HttpRequest.Method.HEAD);
        int status = request.getStatus();
    }
}

Async Request

public class Demo {
    public static void main(String[] args) {
        HttpRequest.async(new HttpRequest(URL_NEED_PROXY).timeout(timeout),
                HttpRequest.Method.HEAD)
                .observe(request -> {// when request success
                    int status = request.getStatus();
                }, exception -> {// when an exception is caught
                    String msg = exception.getMessage();
                });
    }
}

How To

Step 1. Add the JitPack repository to your build file

Add it in your root build.gradle at the end of repositories:

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

Step 2. Add the dependency

dependencies {
    implementation 'com.github.Edsuns:HttpRequest:Tag'
}
MIT License Copyright (c) 2020 Edsuns Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

轻量级Http请求库,可靠、高效、易用。 展开 收起
Java
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/Edsuns/HttpRequest.git
git@gitee.com:Edsuns/HttpRequest.git
Edsuns
HttpRequest
HttpRequest
master

搜索帮助