java调用rest接口如何HttpClient 使用put x-www-form-urlencoded数据?

2025-02-24 00:05:13
推荐回答(1个)
回答1:

把post的改成put试试
HttpPost httpPost = new HttpPost("http xxx");
List nvps = new ArrayList ();
nvps.add(new BasicNameValuePair("username", "vip"));
nvps.add(new BasicNameValuePair("password", "secret"));
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
CloseableHttpResponse response2 = httpclient.execute(httpPost);

try {
System.out.println(response2.getStatusLine());
HttpEntity entity2 = response2.getEntity();
// do something useful with the response body
// and ensure it is fully consumed
EntityUtils.consume(entity2);
} finally {
response2.close();
}