Java HTTP tool class

Time:2019-12-8

HTTP request tool

I. add dependency

<dependency>
    <groupId>cn.gjing</groupId>
    <artifactId>tools-httpclient</artifactId>
    <version>1.2.1</version>
</dependency>

II. Instructions

The return value type should preferably be the same as the target methodOtherwise, conversion exceptions may occur, which is best used when the return type is not confirmedStringTo receive. At the end of the request, you can use theget()Method can also be used to get the returned contentlistener()Method specifies the listener to listen to the processing logic after the result is returned

1. No parameter request

public class Test{
    public static void main(String[] args) {
        String result = HttpClient.builder("http://127.0.0.1:8080/test", HttpMethod.GET, String.class)
                        .execute()
                        .get();
        System.out.println(result);
    }
}

2. Request with request header

public class Test{
    public static void main(String[] args) {
            Map<String, String> map = new HashMap<>(16);
            map.put("head", "111");
            HttpClient.builder("http://127.0.0.1:8080/test6", HttpMethod.GET, String.class)
                    .header(map)
                    .execute()
                    .listener(System.out::println);
    }
}

3. Carrying parameters

public class Test{
    public static void main(String[] args) {
        Map<String, Object> param = new HashMap<>();
        param.put("id","1");
        HttpClient.builder("http://127.0.0.1:8080/test6", HttpMethod.GET, String.class)
                .param(param)
                .listener(System.out::println);
    }
}

4. Carry JSON

It can be JSON string, map, and JSON corresponding entity object. Here, it is demonstrated to pass through map

public class Test{
    public static void main(String[] args) {
        Map<String, String> map = new HashMap<>();
        map.put("key", "code");
        map.put("val", "200");
        Map resultMap = HttpClient.builder("http://127.0.0.1:8080/test6", HttpMethod.POST, Map.class)
                .body(map)
                .get();
    }
}

III. urlutils tool class

1. URL splicing

URL splicing, and the return result format is as follows: http: / / xxx / Param1 / param2

    public static void main(String[] args) {
        String url = "http://127.0.0.1:8080/";
        Object[] param = {1, 2, 3, 4};
        UrlUtil.urlAppend(url, param);
    }

2. Parameter sorting

Parameters are sorted according to the Unicode code of the field name from small to large (dictionary order). The result format is as follows: a = Parameter1 & B = parameter2

public static void main(String[] args) {
        Map<String, Object> map = new HashMap<>(16);
        Map. Put ("a", "parameter 1");
        Map. Put ("B", "parameter 2");
        UrlUtil.paramUnicodeSort(map, false, false);
    }

Parameter description

parameter describe
paramMap parameter
urlEncode URL encoding or not
keyToLower Whether the key value of the converted parameter should be converted to lowercase

3. Transfer URL parameter to map

Convert the parameters after the URL address to map

    public static void main(String[] args) {
        String url = "http://127.0.0.1:8080?a=2&b=2";
        UrlUtil.toMap(url);
    }

The project source code can be viewed in GitHub: tools httpclient

Recommended Today

SQL practical skills: how to split strings

create function f_split(@c varchar(2000),@split varchar(2)) returns @t table(col varchar(20)) as begin while(charindex(@split,@c)<>0) begin insert @t(col) values (substring(@c,1,charindex(@split,@c)-1)) set @c = stuff(@c,1,charindex(@split,@c),”) end insert @t(col) values (@c) return end go select * from dbo.f_split(‘dfkd,dfdkdf,dfdkf,dffjk’,’,’) drop function f_split