Java中post请求外部接口。其中有应对form-data参数方式处理
一、正常json参数的请求方式
代码片段如下:
String result = HttpUtil.post(URL_DEFAULT+"d38e4357cb96dce5", JSONUtil.parseObj(Dict.create()
.set("fileName", cityTransitMapParams.getFileName())
.set("appKey",cityTransitMapParams.getAppKey())
.set("signature",cityTransitMapParams.getSignature())
.set("timestamp",cityTransitMapParams.getTimesTampStr())
.set("servertype",cityTransitMapParams.getServertype())
.set("proxyToken",proxyToken)
).toString());
二、form-data请求参数的请求方式
代码片段如下:
HttpRequest post = HttpUtil.createPost(URL_DEFAULT + "/login");
Map<String, Object> paramMap = new HashMap<>();
paramMap.put("userName",cityTransitMapLoginParams.getUserName());
paramMap.put("password",cityTransitMapLoginParams.getPassword());
paramMap.put("clientId",cityTransitMapLoginParams.getClientId());
paramMap.put("serviceId",cityTransitMapLoginParams.getServiceId());
post.form(paramMap);
String result = post.execute().body();
想要全面了解相关请求可参考hutool文档:
Hutool参考文档