我尝试发送POST带有这样的正文的请求
{
"method": "getAreas",
"methodProperties": {
"prop1" : "value1",
"prop2" : "value2",
}
}
这是我的代码
static final String HOST = "https://somehost.com";
public String sendPost(String method,
Map<String, String> methodProperties) throws ClientProtocolException, IOException {
HttpPost post = new HttpPost(HOST);
List<NameValuePair> urlParameters = new ArrayList<>();
urlParameters.add(new BasicNameValuePair("method", method));
List<NameValuePair> methodPropertiesList = methodProperties.entrySet().stream()
.map(entry -> new BasicNameValuePair(entry.getKey(), entry.getValue()))
.collect(Collectors.toList());
// ??? urlParameters.add(new BasicNameValuePair("methodProperties", methodPropertiesList));
post.setEntity(new UrlEncodedFormEntity(urlParameters));
try (CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = httpClient.execute(post)) {
return EntityUtils.toString(response.getEntity());
}
}
但 的构造函数BasicNameValuePair适用(String, String)。所以我需要另一堂课。
有什么办法可以添加methodPropertiesList吗urlParameters?
长风秋雁
慕无忌1623718
慕妹3146593
相关分类