http请求模拟表单提交:
postman示例:
代码实现:
// /**
// * 方法名
// * @param requestUrl 请求地址
// * @param param1 请求头参数1
// * @param param2 请求头参数2
// * */
public static String postWithParamsForString(String url, String param1,String param2) throws ClientProtocolException, IOException{
String jsonStr = "";
CloseableHttpClient httpClient = HttpClients.createDefault();
String requestBody = createRequestBody(param1, param2);
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
StringEntity entity = new StringEntity(requestBody);
httpPost.setEntity(entity);
CloseableHttpResponse response = httpClient.execute(httpPost);
jsonStr = EntityUtils.toString(response.getEntity());
return jsonStr;
}
private static String createRequestBody(String param1, String param2) {
return MessageFormat.format("grant=client&client={0}&clientid={1}", param1, param2);
}