Java http工具类

Http请求工具

一、添加依赖


    cn.gjing
    tools-httpclient
    1.1.0

二、使用说明

1、创建httpClient实例

请求地址支持https

public static void main(String[] args) {
    HttpClient httpClient = HttpClientFactory.builder("http://127.0.0.1:8080/test", HttpMethod.GET)
}

2、携带请求头

public static void main(String[] args) {
    Map header = new HashMap<>();
    header.put("token", "xxxx");
    HttpClient httpClient = HttpClientFactory.builder("http://127.0.0.1:8080/test", HttpMethod.GET)
            .withHeader(header);
}

3、携带参数

public static void main(String[] args) {
    Map param = new HashMap<>();
    param.put("id","1")
    HttpClient httpClient = HttpClientFactory.builder("http://127.0.0.1:8080/test", HttpMethod.GET)
            .withParam(param);
}

4、携带JSON

withBody()方法内参数可以是JSON字符串、Map、JSON对应实体对象

public static void main(String[] args) {
    Map map = new HashMap<>();
    map.put("key", "code");
    map.put("val", "200");
    HttpClient httpClient = HttpClientFactory.builder("http://127.0.0.1:8080/test_body", HttpMethod.POST)
            .withBody(map);
}

5、发起请求

需要传入响应类型, 最好与目标方法一致, 否则可能出现转换异常, 此方法为最终操作

public static void main(String[] args) {
    Map map = new HashMap<>();
    map.put("key", "code");
    map.put("val", "200");
    HttpClient httpClient = HttpClientFactory.builder("http://127.0.0.1:8080/test_body", HttpMethod.POST)
            .withBody(map);
    HttpVo httpVo = httpClient.execute(HttpVo.class);
    System.out.println(httpVo.toString());
}

三、 UrlUtil工具类

1、url拼接

Url拼接, 返回结果格式如: 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、参数排序

参数按照字段名的Unicode码从小到大排序(字典序), 得到的结果格式如: a=参数1&b=参数2

    public static void main(String[] args) {
        Map map = new HashMap<>(16);
        map.put("a", "参数1");
        map.put("b", "参数2");
        UrlUtil.paramUnicodeSort(map, false, false);
    }

参数说明

参数 描述
paramMap 参数
urlEncode 是否进行URL编码
keyToLower 转换后的参数的key值是否要转为小写

3、url参数转map

将URL地址后带的参数转成map

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

项目源代码可前往GitHUb查看:tools-httpclient

你可能感兴趣的:(java,http)