Apache的HttpClient,封装工具类
引入依赖
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.13</version> </dependency>
1、GET方法
public static String doGet(String url, Map<String, String> params) { CloseableHttpClient client = HttpClients.createDefault(); URIBuilder uriBuilder; CloseableHttpResponse httpResponse = null; try { uriBuilder = new URIBuilder(url); if(params != null) { params.forEach((k, v) -> { uriBuilder.addParameter(k, v); }); } URI uri = uriBuilder.build(); HttpGet httpGet = new HttpGet(uri); httpResponse = client.execute(httpGet); return EntityUtils.toString(httpResponse.getEntity(), Charset.defaultCharset()); } catch (Exception e) { e.printStackTrace(); } finally { try { if(httpResponse != null) { httpResponse.close(); } client.close(); } catch (IOException e) { e.printStackTrace(); } } return null; }
2、 POST方法
public static String doPost(String url, Map<String, String> params) { CloseableHttpClient client = HttpClients.createDefault(); CloseableHttpResponse httpResponse = null; try { HttpPost httpPost = new HttpPost(url); if(params != null) { List<NameValuePair> nvpList = new ArrayList<>(); params.forEach((k, v) -> { nvpList.add(new BasicNameValuePair(k, v)); }); UrlEncodedFormEntity entity = new UrlEncodedFormEntity(nvpList, Charset.defaultCharset()); httpPost.setEntity(entity); } httpResponse = client.execute(httpPost); return EntityUtils.toString(httpResponse.getEntity(), Charset.defaultCharset()); } catch (Exception e) { e.printStackTrace(); } finally { try { if(httpResponse != null) { httpResponse.close(); } client.close(); } catch (IOException e) { e.printStackTrace(); } } return null; }
3、application/json方式传参
public static String doPostJson(String url, Map<String, String> params) { CloseableHttpClient client = HttpClients.createDefault(); CloseableHttpResponse httpResponse = null; try { HttpPost httpPost = new HttpPost(url); if(params != null) { StringEntity entity = new StringEntity(JSON.toJSONString(params), ContentType.APPLICATION_JSON); httpPost.setEntity(entity); } httpResponse = client.execute(httpPost); return EntityUtils.toString(httpResponse.getEntity(), Charset.defaultCharset()); } catch (Exception e) { e.printStackTrace(); } finally { try { if(httpResponse != null) { httpResponse.close(); } client.close(); } catch (IOException e) { e.printStackTrace(); } } return null; }
贴上完整工具类
import com.alibaba.fastjson.JSON; import net.jcip.annotations.ThreadSafe; 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.utils.URIBuilder; import org.apache.http.entity.ContentType; 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.BasicNameValuePair; 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.HashMap; import java.util.List; import java.util.Map; @ThreadSafe public class HttpClient { public static String doGet(String url) { return doGet(url, null); } public static String doGet(String url, Map<String, String> params) { CloseableHttpClient client = HttpClients.createDefault(); URIBuilder uriBuilder; CloseableHttpResponse httpResponse = null; try { uriBuilder = new URIBuilder(url); if(params != null) { params.forEach((k, v) -> { uriBuilder.addParameter(k, v); }); } URI uri = uriBuilder.build(); HttpGet httpGet = new HttpGet(uri); httpResponse = client.execute(httpGet); return EntityUtils.toString(httpResponse.getEntity(), Charset.defaultCharset()); } catch (Exception e) { e.printStackTrace(); } finally { try { if(httpResponse != null) { httpResponse.close(); } client.close(); } catch (IOException e) { e.printStackTrace(); } } return null; } public static String doPost(String url) { return doPost(url, null); } public static String doPost(String url, Map<String, String> params) { CloseableHttpClient client = HttpClients.createDefault(); CloseableHttpResponse httpResponse = null; try { HttpPost httpPost = new HttpPost(url); if(params != null) { List<NameValuePair> nvpList = new ArrayList<>(); params.forEach((k, v) -> { nvpList.add(new BasicNameValuePair(k, v)); }); UrlEncodedFormEntity entity = new UrlEncodedFormEntity(nvpList, Charset.defaultCharset()); httpPost.setEntity(entity); } httpResponse = client.execute(httpPost); return EntityUtils.toString(httpResponse.getEntity(), Charset.defaultCharset()); } catch (Exception e) { e.printStackTrace(); } finally { try { if(httpResponse != null) { httpResponse.close(); } client.close(); } catch (IOException e) { e.printStackTrace(); } } return null; } public static String doPostJson(String url, Map<String, String> params) { CloseableHttpClient client = HttpClients.createDefault(); CloseableHttpResponse httpResponse = null; try { HttpPost httpPost = new HttpPost(url); if(params != null) { StringEntity entity = new StringEntity(JSON.toJSONString(params), ContentType.APPLICATION_JSON); httpPost.setEntity(entity); } httpResponse = client.execute(httpPost); return EntityUtils.toString(httpResponse.getEntity(), Charset.defaultCharset()); } catch (Exception e) { e.printStackTrace(); } finally { try { if(httpResponse != null) { httpResponse.close(); } client.close(); } catch (IOException e) { e.printStackTrace(); } } return null; } }