diff --git a/pallas-core/src/main/java/com/vip/pallas/utils/HttpUtil.java b/pallas-core/src/main/java/com/vip/pallas/utils/HttpUtil.java index 0c2993d5..7681fbdb 100644 --- a/pallas-core/src/main/java/com/vip/pallas/utils/HttpUtil.java +++ b/pallas-core/src/main/java/com/vip/pallas/utils/HttpUtil.java @@ -22,6 +22,7 @@ import java.util.Map; import java.util.Set; +import org.apache.http.Header; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.HttpClient; @@ -38,7 +39,7 @@ public class HttpUtil { private static Logger logger = LoggerFactory.getLogger(HttpUtil.class); @SuppressWarnings("rawtypes") - private ResponseHandler urlCallback = (HttpResponse response) -> { + private static ResponseHandler urlCallback = (HttpResponse response) -> { Map ret = null; if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { String result = EntityUtils.toString(response.getEntity(), "utf-8"); @@ -63,7 +64,7 @@ public class HttpUtil { }; @SuppressWarnings("rawtypes") - private ResponseHandler jsonCallback = (HttpResponse response) -> { + private static ResponseHandler jsonCallback = (HttpResponse response) -> { Map ret = null; if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { String result = EntityUtils.toString(response.getEntity(), "utf-8"); @@ -83,25 +84,35 @@ public class HttpUtil { return ret; }; - public Map httpGet(String url, Map params, boolean json) throws Exception { - Set keySet = params.keySet(); + public static Map httpGet(String url, Map params, boolean json) throws Exception { + return httpGet(url, params, json, null); + } + public static Map httpGet(String url, Map params, boolean json, Header[] headers) + throws Exception { String getUrl = url; - if (url.lastIndexOf('?') <= -1) { - getUrl += "?"; - } + if (params != null) { + Set keySet = params.keySet(); - boolean flag = false; - for (String key : keySet) { - if (flag) { - getUrl += "&"; + if (url.lastIndexOf('?') <= -1) { + getUrl += "?"; + } + + boolean flag = false; + for (String key : keySet) { + if (flag) { + getUrl += "&"; + } + getUrl += key + "="; + getUrl += ObjectMapTool.getString(params, key); + flag = true; } - getUrl += key + "="; - getUrl += ObjectMapTool.getString(params, key); - flag = true; } HttpGet request = new HttpGet(new URI(getUrl)); HttpClient httpClient = HttpClientUtil.getHttpClient(); + if (headers != null) { + request.setHeaders(headers); + } return httpClient.execute(request, json ? jsonCallback : urlCallback); } } \ No newline at end of file diff --git a/pallas-it/src/test/java/com/vip/pallas/test/core/HttpUtilTest.java b/pallas-it/src/test/java/com/vip/pallas/test/core/HttpUtilTest.java index 4103ba48..468cf8fe 100644 --- a/pallas-it/src/test/java/com/vip/pallas/test/core/HttpUtilTest.java +++ b/pallas-it/src/test/java/com/vip/pallas/test/core/HttpUtilTest.java @@ -17,13 +17,14 @@ package com.vip.pallas.test.core; -import com.vip.pallas.test.base.BaseEsTest; -import com.vip.pallas.utils.HttpUtil; +import java.util.HashMap; +import java.util.Map; + import org.junit.Assert; import org.junit.Test; -import java.util.HashMap; -import java.util.Map; +import com.vip.pallas.test.base.BaseEsTest; +import com.vip.pallas.utils.HttpUtil; /** * Created by owen on 12/7/2017. @@ -32,13 +33,12 @@ public class HttpUtilTest extends BaseEsTest { @Test public void testHttpGet() throws Exception { - HttpUtil util = new HttpUtil(); Map params = new HashMap(); - Map res = util.httpGet("http://127.0.0.1:9200/_cat/indices", params, false); + Map res = HttpUtil.httpGet("http://127.0.0.1:9200/_cat/indices", params, false); Assert.assertEquals("{}", res.toString()); - res = util.httpGet("http://127.0.0.1:9200/_cat/indices", params, true); + res = HttpUtil.httpGet("http://127.0.0.1:9200/_cat/indices", params, true); Assert.assertTrue(res.size() > 0); } } \ No newline at end of file