2021与蓝度共同重构项目,服务端
liuhaonan
2022-02-09 66e0f592475cebf0511be54f71217be5ba4245c8
ximon-admin/src/main/java/com/sandu/ximon/admin/utils/VnnoxRequestUtil.java
@@ -2,7 +2,9 @@
import com.sandu.ximon.admin.utils.request.CommonHeader;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
@@ -12,6 +14,10 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;
public class VnnoxRequestUtil {
@@ -35,29 +41,53 @@
        return result;
    }
    public static String GetWithHeader (String url, String params, CommonHeader header) {
        CloseableHttpClient client = HttpClients.createDefault();
        HttpGet get = new HttpGet(url);
        for(String key: header.keySet()) {
            get.setHeader(key, header.get(key));
        }
        String result = null;
        try {
            StringEntity s = new StringEntity(params, UTF8);
            s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
                    "application/json"));
            HttpResponse httpResponse = client.execute(get);
            InputStream inStream = httpResponse.getEntity().getContent();
//    public static String GetWithHeader (String url, String params, CommonHeader header) {
//        CloseableHttpClient client = HttpClients.createDefault();
//        HttpGet get = new HttpGet(url);
//        for(String key: header.keySet()) {
//            get.setHeader(key, header.get(key));
//        }
//        String result = null;
//        try {
//            StringEntity s = new StringEntity(params, UTF8);
//            s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
//                    "application/json"));
//            HttpResponse httpResponse = client.execute(get);
//            InputStream inStream = httpResponse.getEntity().getContent();
//
//            result = streamToString(inStream, UTF8);
//
//            inStream.close();
//        } catch (Exception e) {
//            return null;
//        }
//        return result;
//    }
public static String GetWithHeader (String url, List<NameValuePair> urlParameters, Map<String, String> header) throws URISyntaxException {
            result = streamToString(inStream, UTF8);
            inStream.close();
        } catch (Exception e) {
            return null;
        }
        return result;
    URI finalURI = new URIBuilder(url).setParameters(urlParameters).build();
    CloseableHttpClient client = HttpClients.createDefault();
    HttpGet get = new HttpGet(finalURI);
    for(String key: header.keySet()) {
        get.setHeader(key, header.get(key));
    }
    String result = null;
    try {
//            StringEntity s = new StringEntity(params, UTF8);
//            s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
//                    "application/json"));
        HttpResponse httpResponse = client.execute(get);
        InputStream inStream = httpResponse.getEntity().getContent();
        result = streamToString(inStream, UTF8);
        inStream.close();
    } catch (Exception e) {
        return null;
    }
    return result;
}