JAVA利用HttpClient进行POST请求

目前,要为另一个项目提供接口,接口是用HTTP URL实现的,最初的想法是另一个项目用JQuery post进行请求。

但是,很可能另一个项目是部署在别的机器上,那么就存在跨域问题,而JQuery的post请求是不允许跨域的。

这时,就只能够用HttpClient包进行请求了,同时由于请求的URL是HTTPS的,为了避免需要证书,所以用一个类继承DefaultHttpClient类,忽略校验过程。

package com.hellowin.yl.admin.util.httpUtil;

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 org.apache.rocketmq.common.utils.HttpTinyClient;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

1.写一个利用HttpClient发送post请求的类
public class RequestClientInterface {
    private  CloseableHttpClient httpClient;

    public  RequestClientInterface() {
        // 1 创建HttpClinet,相当于打开浏览器
        this.httpClient = HttpClients.createDefault();
    }

    /**
     * 带参数的post请求
     *
     * @param url
     * @param map
     * @return
     * @throws Exception
     */
    public  HttpTinyClient.HttpResult doPost(String url, Map map) throws Exception {
        // 声明httpPost请求
         HttpPost httpPost = new HttpPost(url);

        // 判断map不为空
        if (map != null) {
            // 声明存放参数的List集合
            List params = new ArrayList();

            // 遍历map,设置参数到list中
            for (Map.Entry entry : map.entrySet()) {
                params.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
            }

            // 创建form表单对象
            UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(params, "utf-8");
            formEntity.setContentType("Content-Type:application/json");

            // 把表单对象设置到httpPost中
            httpPost.setEntity(formEntity);
        }

        // 使用HttpClient发起请求,返回response
        CloseableHttpResponse response = this.httpClient.execute(httpPost);

        // 解析response封装返回对象httpResult
        HttpTinyClient.HttpResult httpResult = null;
        if (response.getEntity() != null) {
            httpResult = new HttpTinyClient.HttpResult(response.getStatusLine().getStatusCode(),
                    EntityUtils.toString(response.getEntity(), "UTF-8"));
        } else {
            httpResult = new HttpTinyClient.HttpResult(response.getStatusLine().getStatusCode(), "");
        }
        // 返回结果
        return httpResult;
    }
}


2.调用post请求的测试代码
import java.util.HashMap;
import java.util.Map;
    //对接口进行测试
    public class TestMain {
        private String url = "https://192.168.1.101/";
        private String charset = "utf-8";
        private HttpClientUtil httpClientUtil = null;

        public TestMain(){
            httpClientUtil = new HttpClientUtil();
        }

        public void test(){
            String httpOrgCreateTest = url + "httpOrg/create";
            Map createMap = new HashMap();
            RequestClientInterface clientInterface = new RequestClientInterface();
            createMap.put("orgname","****");
            createMap.put("functionId", "****");
            HttpTinyClient.HttpResult httpResult = clientInterface.doPost(httpOrgCreateTest, createMap);
            System.out.println("result:"+httpResult);
        }

        public static void main(String[] args){
            TestMain main = new TestMain();
            main.test();
        }
    }

参考:https://blog.csdn.net/rongyongfeikai2/article/details/41659353

你可能感兴趣的:(SpringBoot)