HttpUtils工具類和post請求json引數傳輸
阿新 • • 發佈:2018-11-30
import lombok.extern.slf4j.Slf4j; import org.apache.http.HttpEntity; import org.apache.http.NameValuePair; import org.apache.http.client.ResponseHandler; 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.entity.StringEntity; import org.apache.http.impl.client.BasicResponseHandler; 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.URLEncoder; import java.util.ArrayList; import java.util.List; import java.util.Map; @Slf4j public class HttpUtils { private static final String ENCODING = "UTF-8"; public static String post(String url, Map<String, String> paramsMap) { CloseableHttpClient client = HttpClients.createDefault(); String responseText = ""; CloseableHttpResponse response = null; try { HttpPost method = new HttpPost(url); if (paramsMap != null) { List<NameValuePair> paramList = new ArrayList<NameValuePair>(); for (Map.Entry<String, String> param : paramsMap.entrySet()) { NameValuePair pair = new BasicNameValuePair(param.getKey(), param.getValue()); paramList.add(pair); } method.setEntity(new UrlEncodedFormEntity(paramList, ENCODING)); } response = client.execute(method); HttpEntity entity = response.getEntity(); if (entity != null) { responseText = EntityUtils.toString(entity); } } catch (Exception e) { log.error("http request failed",e); } finally { try { response.close(); } catch (Exception e) { log.error("",e); } } return responseText; } public static String get(String url, Map<String, String> paramsMap) { CloseableHttpClient client = HttpClients.createDefault(); String responseText = ""; CloseableHttpResponse response = null; try { String getUrl = url+"?"; if (paramsMap != null) { for (Map.Entry<String, String> param : paramsMap.entrySet()) { getUrl += param.getKey() + "=" + URLEncoder.encode(param.getValue(), ENCODING)+"&"; } } HttpGet method = new HttpGet(getUrl); response = client.execute(method); HttpEntity entity = response.getEntity(); if (entity != null) { responseText = EntityUtils.toString(entity); } } catch (Exception e) { log.error("http request failed",e); } finally { try { response.close(); } catch (Exception e) { log.error("",e); } } return responseText; } //post請求引數為json格式 public static String HttpPostWithJson(String url, String json) { String returnValue = "這是預設返回值,介面呼叫失敗"; CloseableHttpClient httpClient = HttpClients.createDefault(); ResponseHandler<String> responseHandler = new BasicResponseHandler(); try{ //第一步:建立HttpClient物件 httpClient = HttpClients.createDefault(); //第二步:建立httpPost物件 HttpPost httpPost = new HttpPost(url); //第三步:給httpPost設定JSON格式的引數 StringEntity requestEntity = new StringEntity(json,"utf-8"); requestEntity.setContentEncoding("UTF-8"); httpPost.setHeader("Content-type", "application/json"); httpPost.setEntity(requestEntity); //第四步:傳送HttpPost請求,獲取返回值 returnValue = httpClient.execute(httpPost,responseHandler); //調介面獲取返回值時,必須用此方法 } catch(Exception e) { e.printStackTrace(); } finally { try { httpClient.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //第五步:處理返回值 return returnValue; } }
呼叫方式都很簡單前面兩個是url+map
map的key是引數,value是值。無參的話傳null就好了
post傳入json引數的話注意傳入的json要轉義。可以在postman上轉為json格式進行復制就好了
獲取到了json資料,如何json轉list並傳到資料庫的話可以看看我上一篇