Using tools-httpclient can quickly send HTTP / HTTPS request in the project
Project use:
- Add dependence:
<dependency>
<groupId>cn.gjing</groupId>
<artifactId>tools-httpclient</artifactId>
<version>1.0.1</version>
</dependency>
- Interface requires the use of direct use:
- HTTP request (GET case)
public static void main(String[] args) {
Map<String, String> map = new HashMap<>(16);
map.put("a", "参数a");
map.put("b", "参数b");
//发送http请求
String result = HttpClient.get("http://127.0.0.1:8080/test", map, String.class);
System.out.println(result);
}
- HTTPS requests (POST Case)
public static void main(String[] args) {
Map<String, String> map = new HashMap<>(16);
map.put("a", "参数a");
map.put("b", "参数b");
//发送https请求
Map result = HttpClient.post("https://127.0.0.1:8080/test", map, Map.class);
System.out.println(result.toString());
}
- DELETE Case
public static void main(String[] args) {
ResultBean result = HttpClient.delete("http://127.0.0.1:8080/test/1", ResultBean.class);
System.out.println(result.toString());
}
- PUT Case
public static void main(String[] args) {
Integer result = HttpClient.put("https://127.0.0.1:8080/test/2", Integer.class);
System.out.println(result);
}
Parameter Description HttpClient (except for any parameter required parameters need not be directly transmitted null):
- url : Request address (required)
- QueryMap : Request parameter
- headers : request header
- connectTimeout : Request timeout, default 5s
- ReadTimeout : reading timeout, default 10 seconds
- responseType : response result return type (required)
- jsonEntity : Json object may be a character string corresponding to json object map may be used
- jsonStr : Json string
tip: The best type of response returned results consistent with the targets set method, this may appear abnormal conversion
Target method
@GetMapping("/test")
public Integer test() {
return 1111;
}
The caller
public static void main(String[] args) {
Map result = HttpClient.get("http://127.0.0.1:8080/test", Map.class);
System.out.println(result.toString());
}
Since the target method returns an Integer type, not a key, value type, so the caller will be converted using the Map receiving abnormal.
-
- -
- UrlUtil Tools
- urlAppend (for direct parameterized URL address)
public static void main(String[] args) {
String url = "http://127.0.0.1:8080/";
Object[] param = {1, 2, 3, 4};
System.out.println(UrlUtil.urlAppend(url, param));
}
输出结果为:http://127.0.0.1:8080/1/2/3/4/
- paramUnicodeSort (small according to Unicode code parameter field name to large order (lexicographical))
public static void main(String[] args) {
Map<String, Object> map = new HashMap<>(16);
map.put("a", "参数1");
map.put("b", "参数2");
/*
该方法带三个参数,分别为:
paramMap: 参数
urlEncode: 是否进行URL encode
keyToLower: 转换后的参数的key值是否转为小写
*/
System.out.println(UrlUtil.paramUnicodeSort(map, false, false));
}
输出结果为:a=参数1&b=参数2
- urlParamToMap (URL address with the parameter turn into Map)
public static void main(String[] args) {
String url = "http://127.0.0.1:8080?a=2&b=2";
System.out.println(UrlUtil.urlParamToMap(url));
}
输出结果为:{a=2, b=2}
-
- -
Project source code can go GitHUb View: Tools-HttpClient
- -