HttpUtil.java 4.6 KB
Newer Older
苗卫卫 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
package com.boco.nbd.wios.manage.util;

import cn.hutool.core.codec.Base64;
import com.alibaba.fastjson.JSON;
import com.boco.nbd.cams.core.constant.CamsConstant;
import com.boco.nbd.wios.manage.contants.WiosConstant;
import org.apache.http.Consts;
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.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.URI;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * sap http请求工具类
 *
 * @author xfy
 * @date 2022/2/11
 */
public class HttpUtil {
    public static String doGet(String url, Map<String, Object> param, Map<String, String> headers) {
        String resultString = "";
        CloseableHttpResponse response = null;
        CloseableHttpClient httpclient = null;
        try {
            // 创建Httpclient对象
            httpclient = HttpClients.createDefault();
            // 创建uri
            URIBuilder builder = new URIBuilder(url);
            if (param != null) {
                for (String key : param.keySet()) {
                    builder.addParameter(key, param.get(key).toString());
                }
            }
            URI uri = builder.build();

            // 创建http GET请求
            HttpGet httpGet = new HttpGet(uri);
            List<NameValuePair> params = new ArrayList<>();
            if (param == null) {
                params = null;
            }
            RequestBuilder requestBuilder = RequestBuilder.get().setUri(new URI(url));
            requestBuilder.setEntity(new UrlEncodedFormEntity(params, Consts.UTF_8));
            httpGet.setHeader(
                    new BasicHeader("Content-Type", headers.get("Content-Type")));
            httpGet.setHeader(new BasicHeader("Accept", "text/plain;charset=utf-8"));
            httpGet.setHeader("Authorization", headers.get("Authorization"));

            response = httpclient.execute(httpGet);
            // 判断返回状态是否为200
            if (WiosConstant.SUCCESS_CODE == response.getStatusLine().getStatusCode()) {
                resultString = EntityUtils.toString(response.getEntity(), CamsConstant.UTF_8);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
                if (httpclient != null) {
                    httpclient.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return resultString;
    }

    public static String doPost(String url, Map<String, Object> param, Map<String, String> headers) {
        CloseableHttpResponse response = null;
        String resultString = "";
        try {
            // 创建Httpclient对象
            CloseableHttpClient httpClient = HttpClients.createDefault();
            // 创建Http Post请求
            HttpPost httpPost = new HttpPost(url);
            if (headers != null) {
                httpPost.setHeader(
                        new BasicHeader("Content-Type", headers.get("Content-Type")));
                httpPost.setHeader(new BasicHeader("Accept", "text/plain;charset=utf-8"));
                httpPost.setHeader("Authorization", headers.get("Authorization"));
            }
            // 创建参数列表
            if (param != null) {
                StringEntity entity = new StringEntity(JSON.toJSONString(param), "utf-8");
                httpPost.setEntity(entity);
            }
            // 执行http请求
            response = httpClient.execute(httpPost);
            resultString = EntityUtils.toString(response.getEntity(), "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return resultString;
    }


    public static String buildBasicAuth(String username, String password, Charset charset) {
        String data = username.concat(":").concat(password);
        return "Basic " + Base64.encode(data, charset);
    }
}