这个例子使用的是Apache HttpClient 4.5.7版本

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.7</version>
</dependency>
XML
import com.alibaba.fastjson.JSON;import org.apache.commons.beanutils.BeanMap;import org.apache.commons.beanutils.BeanUtils;import org.apache.http.HttpEntity;import org.apache.http.HttpStatus;import org.apache.http.NameValuePair;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.message.BasicNameValuePair;import org.apache.http.util.EntityUtils;import java.util.ArrayList;import java.util.List;import java.util.Map; /** * @author Jack * @date 2019年3月12日 下午4:56:14 * @version 1.0.0 */public class HttpClientUtils {     /**     *     * @param protocol  协议     * @param hostname  域名或者IP地址     * @param port  端口     * @param path  路径 如   /aa/query     * @param request   入参     * @param resCla   返回结果对象的类字面量     * @param <Y>     * @param <T>     * @return  返回对应的结果对象     * @throws Exception     */    public static  <Y,T> Y alaPost(String protocol, String hostname, String port, String path, T request,Class<Y> resCla) throws Exception{        String result=post( protocol,  hostname,  port,  path, request);        Y response=resCla.newInstance() ;        try {            Map<String, Object> resMap = JSON.parse(result, Map.class);            //map转bean            BeanUtils.populate(response,resMap );        } catch (Exception e) {            throw new Exception("error");        }        return response;    }     /**     *     * @param protocol  协议     * @param hostname  域名或者IP地址     * @param port  端口     * @param path  路径 如   /aa/query     * @param request   入参     * @param <T>     * @return   字符串形式的返回结果     * @throws Exception     */    public static <T> String post(String protocol, String hostname, String port, String path, T request)            throws Exception {        CloseableHttpClient httpclient = HttpClients.createDefault();        // url路径        HttpPost httpPost = new HttpPost(                protocol + "://" + hostname + ":" + port + path);        List<NameValuePair> nvps = new ArrayList<>();         // 设置参数        //bean转map        BeanMap map=new BeanMap(request);        map.forEach((k, v) -> nvps.add(new BasicNameValuePair( k.toString(), v.toString())));        // 设置参数编码        httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));        CloseableHttpResponse response = httpclient.execute(httpPost);        if (HttpStatus.SC_OK != response.getStatusLine().getStatusCode()) {            throw new Exception("error");        }        String result ;        try {            HttpEntity entity = response.getEntity();            // 获取返回结果            result = EntityUtils.toString(entity);            //关闭流            EntityUtils.consume(entity);        } finally {            response.close();        }        return result;    } }
Java