11package util ;
22
3+ import org .apache .http .HttpEntity ;
4+ import org .apache .http .HttpResponse ;
35import org .apache .http .NameValuePair ;
6+ import org .apache .http .client .ClientProtocolException ;
47import org .apache .http .client .entity .UrlEncodedFormEntity ;
58import org .apache .http .client .methods .CloseableHttpResponse ;
69import org .apache .http .client .methods .HttpGet ;
1215import org .apache .http .util .EntityUtils ;
1316
1417import java .io .IOException ;
18+ import java .io .UnsupportedEncodingException ;
1519import java .net .URI ;
1620import java .util .ArrayList ;
1721import java .util .List ;
@@ -24,7 +28,7 @@ public class HttpClientUtil {
2428 * @param param 请求参数
2529 * @return 结果
2630 */
27- public static String doGet (String url , Map <String , String > param ) {
31+ public static String doGet (String url , Map <String , String > param , Map < String , String > headers ) {
2832
2933 // 创建Httpclient对象
3034 CloseableHttpClient httpclient = HttpClients .createDefault ();
@@ -39,10 +43,18 @@ public static String doGet(String url, Map<String, String> param) {
3943 builder .addParameter (key , param .get (key ));
4044 }
4145 }
46+
4247 URI uri = builder .build ();
48+ String urlStr = uri .toString ();
49+ System .out .println ("urlStr:" + urlStr );
4350
4451 // 创建http GET请求
4552 HttpGet httpGet = new HttpGet (uri );
53+ if (headers !=null ){
54+ for (Map .Entry <String ,String > en :headers .entrySet ()){
55+ httpGet .setHeader (en .getKey (), en .getValue ());
56+ }
57+ }
4658
4759 // 执行请求
4860 response = httpclient .execute (httpGet );
@@ -65,13 +77,21 @@ public static String doGet(String url, Map<String, String> param) {
6577 return resultString ;
6678 }
6779
80+
81+
82+
83+
84+
85+
86+
87+
6888 /**
6989 * 无参数的get请求
7090 * @param url 请求地址
7191 * @return 返回结果
7292 */
7393 public static String doGet (String url ) {
74- return doGet (url , null );
94+ return doGet (url , null , null );
7595 }
7696
7797 /**
@@ -115,6 +135,49 @@ public static String doPost(String url, Map<String, String> param) {
115135 return resultString ;
116136 }
117137
138+
139+ public static String doPost (String url ,Map <String ,String > params ,Map <String ,String > headers ) {
140+ CloseableHttpClient client = HttpClients .createDefault ();
141+ HttpPost httpPost = new HttpPost (url );
142+ UrlEncodedFormEntity entity ;
143+ List <NameValuePair > paramPairs = new ArrayList <NameValuePair >();
144+ if (params !=null ){
145+ for (Map .Entry <String ,String > en :params .entrySet ()){
146+ paramPairs .add (new BasicNameValuePair (en .getKey (), en .getValue ()));
147+ }
148+ }
149+ if (headers !=null ){
150+ for (Map .Entry <String ,String > en :headers .entrySet ()){
151+ httpPost .setHeader (en .getKey (), en .getValue ());
152+ }
153+ }
154+ try {
155+ entity = new UrlEncodedFormEntity (paramPairs , "UTF-8" );
156+ httpPost .setEntity (entity );
157+ HttpResponse resp = client .execute (httpPost );
158+ HttpEntity respEntity = resp .getEntity ();
159+ if (null != respEntity ) {
160+ return EntityUtils .toString (respEntity , "UTF-8" );
161+ }
162+
163+ } catch (UnsupportedEncodingException e ) {
164+ e .printStackTrace ();
165+ } catch (ClientProtocolException e ) {
166+ e .printStackTrace ();
167+ } catch (IOException e ) {
168+ e .printStackTrace ();
169+ } finally {
170+ try {
171+ client .close ();
172+ } catch (Exception e ) {
173+ e .printStackTrace ();
174+ }
175+ }
176+ return null ;
177+ }
178+
179+
180+
118181 /**
119182 * 无参的post请求
120183 * @param url 请求url
@@ -123,4 +186,6 @@ public static String doPost(String url, Map<String, String> param) {
123186 public static String doPost (String url ) {
124187 return doPost (url , null );
125188 }
189+
190+
126191}
0 commit comments