在项目中快速发起http请求
<dependency>
<groupId>cn.gjing</groupId>
<artifactId>tools-httpclient</artifactId>
<version>1.0.2</version>
</dependency>
public static void main(String[] args) {
HttpClient client = new HttpClient();
Map<String, String> map = new HashMap<>(16);
map.put("a", "参数a");
map.put("b", "参数b");
//发送http请求
String result = client.get("http://127.0.0.1:8080/test", map, String.class);
System.out.println(result);
}
public static void main(String[] args) {
HttpClient client = new HttpClient();
Map<String, String> map = new HashMap<>(16);
map.put("a", "参数a");
map.put("b", "参数b");
//发送https请求
Map result = client.post("https://127.0.0.1:8080/test", map, Map.class);
System.out.println(result.toString());
}
public static void main(String[] args) {
HttpClient client = new HttpClient();
ResultBean result = client.delete("http://127.0.0.1:8080/test/1", ResultBean.class);
System.out.println(result.toString());
}
public static void main(String[] args) {
HttpClient client = new HttpClient();
Integer result = client.put("https://127.0.0.1:8080/test/2", Integer.class);
System.out.println(result);
}
| 参数 | 描述 |
|---|---|
| url |
请求地址, 必填
|
| queryMap | 请求参数 |
| headers | 请求头 |
| connectTimeout |
请求超时时间, 默认5s
|
| readTimeout |
读超时时间, 默认10秒
|
| responseType |
响应结果返回类型, 必填
|
| jsonEntity | Json对象, Json字符串对应的对象或者map |
| jsonStr | Json字符串 |
响应结果 返回类型最好设置与目标方法一致
,否则可能会出现转换异常,如下:
@GetMapping("/test")
public Integer test() {
return 1111;
}
public static void main(String[] args) {
HttpClient client = new HttpClient();
Map result = client.get("http://127.0.0.1:8080/test", Map.class);
System.out.println(result.toString());
}
由于目标方法返回的为Integer类型,不是key,value类型,故调用方使用Map接收会出现 转换异常
。
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值是否要转为小写 |
public static void main(String[] args) {
String url = "http://127.0.0.1:8080?a=2&b=2";
UrlUtil.urlParamToMap(url);
}