<dependency> <groupId>cn.gjing</groupId> <artifactId>tools-httpclient</artifactId> <version>1.2.1</version> </dependency> 复制代码
返回值类型最好与目标方法一致
,否则可能会出现转换异常,在不确认返回类型时最好使用 String
去接收。在请求结束后,可以通过 get()
方法获取返回的内容,也可以通过 listener()
方法指定监听者去监听结果返回后的处理逻辑 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); } } 复制代码
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); } } 复制代码
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); } } 复制代码
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(); } } 复制代码
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); } 复制代码
public static void main(String[] args) { Map<String, Object> map = new HashMap<>(16); map.put("a", "参数1"); map.put("b", "参数2"); UrlUtil.paramUnicodeSort(map, false, false); } 复制代码
参数 | 描述 |
---|---|
paramMap | 参数 |
urlEncode | 是否进行URL编码 |
keyToLower | 转换后的参数的key值是否要转为小写 |