HttpPost使用教程

Post请求示例代码
1、json方式提交

HttpPost httpPost = new HttpPost(param);
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000).build();
httpPost.setConfig(requestConfig);
httpPost.setHeader("Content-type", "application/json");
CloseableHttpClient httpClient = null;
try{
    //设置post请求参数
    Map<String, String> paramMap = new HashMap<String, String>();
    paramMap.put("component_appid", component_appid);
    paramMap.put("component_appsecret", component_appsecret);
    paramMap.put("component_verify_ticket", component_verify_ticket);
    httpPost.setEntity(new StringEntity(JSONObject.toJSONString(paramMap), ContentType.create("application/json", "utf-8")));
    httpClient = HttpClients.custom().disableAutomaticRetries().build();
	// parse response
	HttpResponse response = httpClient.execute(httpPost);
	EntityUtils.consume(entity);
	return SUCCESS;
} catch (Exception e) {
} finally{
	if (httpPost!=null) {
              httpPost.releaseConnection();
	}
	if (httpClient!=null) {
		try {
			httpClient.close();
		} catch (IOException e) {
		}
	}
}
2、url方式提交
HttpPost httpPost = new HttpPost(param);
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000).build();
httpPost.setConfig(requestConfig);
CloseableHttpClient httpClient = null;
try{
    //设置post请求参数
    List list = new ArrayList();
    list.add(new BasicNameValuePair("component_appid", component_appid));  
    list.add(new BasicNameValuePair("component_appsecret", component_appsecret)); 
    list.add(new BasicNameValuePair("component_verify_ticket", component_verify_ticket)); 
    UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(list,"UTF-8");
    httpPost.setEntity(urlEncodedFormEntity);
    httpClient = HttpClients.custom().disableAutomaticRetries().build();
	// parse response
	HttpResponse response = httpClient.execute(httpPost);
	EntityUtils.consume(entity);
	return SUCCESS;
} catch (Exception e) {
} finally{
	if (httpPost!=null) {
              httpPost.releaseConnection();
	}
	if (httpClient!=null) {
		try {
			httpClient.close();
		} catch (IOException e) {
		}
	}
}
博客链接:https://blog.csdn.net/u012869196/article/details/84076705