在使用RESTFUL时有时需要httpclient
下面是基于apache实现GET/POST/PUT/DELETE
1.添加依赖
<dependency> <groupId>org.apache.httpcomponentsgroupId> <artifactId>httpclientartifactId> <version>4.5version> dependency> <dependency> <groupId>org.apache.httpcomponentsgroupId> <artifactId>httpclient-cacheartifactId> <version>4.5version> dependency> <dependency> <groupId>org.apache.httpcomponentsgroupId> <artifactId>httpmimeartifactId> <version>4.5version> dependency>2.编写和httpclient客户端
下面是博主自己的实现:
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Map; import org.apache.http.HttpEntity; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPut; import org.apache.http.client.params.ClientPNames; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.params.BasicHttpParams; import org.apache.http.params.CoreConnectionPNames; import org.apache.http.params.HttpParams; public class HttpClient { public static StringBuffer sentURL(String path, String bearer, String method,String jsonData) { CloseableHttpClient httpclient = HttpClients.createDefault(); HttpParams params = new BasicHttpParams(); Integer CONNECTION_TIMEOUT = 2 * 1000; Integer SO_TIMEOUT = 2 * 1000; Long CONN_MANAGER_TIMEOUT = 500L; params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, CONNECTION_TIMEOUT); params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, SO_TIMEOUT); params.setLongParameter(ClientPNames.CONN_MANAGER_TIMEOUT, CONN_MANAGER_TIMEOUT); CloseableHttpResponse response=null; if(method.trim().equalsIgnoreCase("GET")) response=sendGet(httpclient,path,bearer); else if(method.trim().equalsIgnoreCase("DELETE")) response=sendDelete(httpclient,path,bearer); else if(method.trim().equalsIgnoreCase("POST")) response=sendPost(httpclient,path,jsonData,bearer); else if(method.trim().equalsIgnoreCase("PUT")) response=sendPut(httpclient,path,jsonData,bearer); InputStream instream=null; StringBuffer sbuffer=null; BufferedReader reader = null; try { HttpEntity entityRes = response.getEntity(); if (entityRes != null) { instream = entityRes.getContent(); InputStreamReader inputStream = new InputStreamReader(instream); reader = new BufferedReader(inputStream); String lines; sbuffer = new StringBuffer(""); while ((lines = reader.readLine()) != null) { lines = new String(lines.getBytes(), "utf-8"); sbuffer.append(lines); } return sbuffer; } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ try { instream.close(); response.close(); httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } return sbuffer; } public static CloseableHttpResponse sendGet(CloseableHttpClient httpclient,String url, String bearer) { HttpGet httpGet=new HttpGet(url); httpGet.setHeader("Authorization", bearer);
//这里是公司用的访问认证,header中添加一个token,若没有可以省去这一步 CloseableHttpResponse response=null; try { response= httpclient.execute(httpGet); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return response; } public static CloseableHttpResponse sendPost(CloseableHttpClient httpclient,String url,String jsonData, String bearer) { HttpPost httpPost=new HttpPost(url); httpPost.setHeader("Authorization", bearer); StringEntity stringEntity =null; if(jsonData!=null) { stringEntity = new StringEntity(jsonData,"utf8"); stringEntity.setContentType("application/json"); httpPost.setEntity(stringEntity); } CloseableHttpResponse response=null; try { response= httpclient.execute(httpPost); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return response; } public static CloseableHttpResponse sendPut(CloseableHttpClient httpclient,String url,String jsonData,String bearer) { HttpPut httpPut=new HttpPut(url); httpPut.setHeader("Authorization", bearer); StringEntity stringEntity =null; if(jsonData!=null) { stringEntity = new StringEntity(jsonData,"utf8"); stringEntity.setContentType("application/json"); httpPut.setEntity(stringEntity); } CloseableHttpResponse response=null; try { response= httpclient.execute(httpPut); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return response; } public static CloseableHttpResponse sendDelete(CloseableHttpClient httpclient,String url,String bearer) { HttpDelete httpDelete=new HttpDelete(url); httpDelete.setHeader("Authorization", bearer); CloseableHttpResponse response=null; try { response= httpclient.execute(httpDelete); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return response; } }