GithubHelp home page GithubHelp logo

gyfgaoyuefei / httputil Goto Github PK

View Code? Open in Web Editor NEW

This project forked from seanlwj/httputil

0.0 1.0 0.0 11 KB

Java 网络请求工具类,支持异步请求,支持传递 header ,支持表单和 json 类型的参数提交。

License: Apache License 2.0

Java 100.00%

httputil's Introduction

HttpUtil

 Java 网络请求工具类,支持异步请求,支持传递 header ,支持表单和 json 类型的参数提交。

包含两个工具类,一个HttpUtil一个HttpBaseUtil,一个依赖Spring,一个不依赖任何框架。

HttpUtil

  依赖于Spring框架,如果你的项目基于Spring或者SpringBoot,推荐使用此工具类。

使用示例:

public class Test{
    public void test(){
        // get请求
        String res = HttpUtil.get("https://baidu.com", null);
        System.out.println(res);
        
        // 带参数
        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
        params.add("name", "hahaha");
        String res = HttpUtil.get("https://baidu.com", params);
        System.out.println(res);
        
        // json参数提交
        User params = new User();
        String res = HttpUtil.request("https://baidu.com", params, null, HttpMethod.POST, MediaType.APPLICATION_JSON);
        System.out.println(res);
        
        // 更多使用方法看源码中的注释
    }
}

查看全部使用方法


HttpBaseUtil

  不依赖于任何框架,使用HttpURLConnection实现,支持异步请求方式。

使用示例:

public class Test{
    public void test(){
        // get请求
        String res = HttpBaseUtil.get("https://baidu.com", null);
        System.out.println(res);
        
        // 带参数
        Map<String, String> params = new HashMap<>();
        params.add("name", "hahaha");
        String res = HttpUtil.get("https://baidu.com", params);
        System.out.println(res);
        
        // 异步请求
        System.out.println("开始请求...." + System.currentTimeMillis());
        HttpBaseUtil.getAsyn("https://baidu.com", null, new OnHttpResult() {
            @Override
            public void onSuccess(String result) {
                System.out.println("请求结束...." + System.currentTimeMillis());
                System.out.println(result);
            }

            @Override
            public void onError(String message) {
                System.out.println(message);
            }
        });
        System.out.println("---我来证明请求是异步的---");
        
        // json参数提交
        User user = new User();
        String params = JSON.toJSONString(user);
        String res = HttpBaseUtil.request("https://baidu.com", params, null, "POST", "application/json");
        System.out.println(res);
        
        // 更多使用方法看源码中的注释
    }
}

查看全部使用方法

httputil's People

Contributors

ele-admin avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.