JAVA8發(fā)送帶有Body的HTTP GET請(qǐng)求
正常來(lái)講,按照HTTP標(biāo)準(zhǔn),GET請(qǐng)求事不能帶有消息體BODY的。但是HTTP標(biāo)準(zhǔn)不是硬性規(guī)定,各個(gè)廠商可以根據(jù)自己的需求做成靈活的擴(kuò)展。比如ES的搜索接口就要求客戶端發(fā)送帶有BODY的HTTP GET請(qǐng)求。
發(fā)送請(qǐng)求的代碼分成兩個(gè)類,接收返回?cái)?shù)據(jù)的 StrResponse 和發(fā)起請(qǐng)求的工具欄 HttpUtils
StrResponse.java
import java.util.List; import java.util.Map; /** ?* 接收HTTP返回?cái)?shù)據(jù)的對(duì)象 ?* @author zhangchao ?*/ public class StrResponse { ? ? private int code = 200; ? ? private Map<String, List<String>> headers = null; ? ? private String body = null; ? ? public Map<String, List<String>> getHeaders() { ? ? ? ? return headers; ? ? } ? ? public String getBody() { ? ? ? ? return body; ? ? } ? ? public void setHeaders(Map<String, List<String>> headers) { ? ? ? ? this.headers = headers; ? ? } ? ? public void setBody(String body) { ? ? ? ? this.body = body; ? ? } ? ? public int getCode() { ? ? ? ? return code; ? ? } ? ? public void setCode(int code) { ? ? ? ? this.code = code; ? ? } ? ? public String getHeaderStr (String key) { ? ? ? ? List<String> list = this.headers.get(key); ? ? ? ? StringBuilder sb = new StringBuilder(); ? ? ? ? for (String str : list) { ? ? ? ? ? ? sb.append(str); ? ? ? ? } ? ? ? ? return sb.toString(); ? ? } ? ? public String getAllHeaderStr() { ? ? ? ? if (null == headers || headers.isEmpty()) { ? ? ? ? ? ? return ""; ? ? ? ? } ? ? ? ? StringBuilder sb = new StringBuilder(); ? ? ? ? for (String key : headers.keySet()) { ? ? ? ? ? ? List<String> list = headers.get(key); ? ? ? ? ? ? sb.append(key + ":\n"); ? ? ? ? ? ? for (String str : list) { ? ? ? ? ? ? ? ? sb.append(" ? ?" + str + "\n"); ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? return sb.toString(); ? ? } ? ? @Override ? ? public String toString() { ? ? ? ? final StringBuffer sb = new StringBuffer("StrResponse{"); ? ? ? ? sb.append("code=").append(code); ? ? ? ? sb.append(", headers=").append(headers); ? ? ? ? sb.append(", body='").append(body).append('\''); ? ? ? ? sb.append('}'); ? ? ? ? return sb.toString(); ? ? } }
HttpUtils.java
import java.util.Map; import java.util.List; import java.io.*; import java.net.*; /** ?* 通用http發(fā)送方法 ?* ?* @author zhangchao ?*/ public class HttpUtils { ?? ?public static StrResponse requestByte_responseStr(final String url, final String method,? ?? ??? ??? ?final byte[] requestBody,final Map<String, String> headerMap, String responseEncoding) { ? ? ? ? BufferedReader in = null; ? ? ? ? BufferedReader errorReader = null; ? ? ? ? HttpURLConnection connection = null; ? ? ? ? StrResponse strResponse = null; ? ? ? ? try { ? ? ? ? ? ? StringBuilder result = new StringBuilder(); ? ? ? ? ? ? URL realUrl = new URL(url); ? ? ? ? ? ? // 打開(kāi)和URL之間的連接 ? ? ? ? ? ? connection = (HttpURLConnection) realUrl.openConnection(); ? ? ? ? ? ? connection.setRequestMethod(method); ? ? ? ? ? ? // 請(qǐng)求內(nèi)容的長(zhǎng)度 ? ? ? ? ? ? if (null != requestBody && requestBody.length > 0) { ? ? ? ? ? ? ? ? connection.setRequestProperty("Content-Length", String.valueOf(requestBody.length)); ? ? ? ? ? ? } ? ? ? ? ? ? // 自定義請(qǐng)求頭 ? ? ? ? ? ? if (null != headerMap && false == headerMap.isEmpty()) { ? ? ? ? ? ? ? ? Set<String> keySet = headerMap.keySet(); ? ? ? ? ? ? ? ? for (String key : keySet) { ? ? ? ? ? ? ? ? ? ? connection.setRequestProperty(key, headerMap.get(key)); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? ? connection.setConnectTimeout(5000); ? ? ? ? ? ? connection.setReadTimeout(5000); ? ? ? ? ? ? // 把JSON作為字節(jié)流寫入post請(qǐng)求的body中 ? ? ? ? ? ? connection.setDoOutput(true); ? ? ? ? ? ? if (null != requestBody && requestBody.length > 0) { ? ? ? ? ? ? ? ? connection.getOutputStream().write(requestBody); ? ? ? ? ? ? } ? ? ? ? ? ? // 定義 BufferedReader輸入流來(lái)讀取URL的響應(yīng) ? ? ? ? ? ? in = new BufferedReader(new InputStreamReader( ? ? ? ? ? ? ? ? ? ? connection.getInputStream(), responseEncoding)); ? ? ? ? ? ? String line; ? ? ? ? ? ? while ((line = in.readLine()) != null) { ? ? ? ? ? ? ? ? result.append(line).append("\n"); ? ? ? ? ? ? } ? ? ? ? ? ? strResponse = new StrResponse(); ? ? ? ? ? ? strResponse.setCode(connection.getResponseCode()); ? ? ? ? ? ? // 返回的header ? ? ? ? ? ? Map<String, List<String>> map = connection.getHeaderFields(); ? ? ? ? ? ? strResponse.setHeaders(map); ? ? ? ? ? ? // 返回的body ? ? ? ? ? ? String responseBody = result.toString(); ? ? ? ? ? ? strResponse.setBody(responseBody); ? ? ? ? } catch (Exception e) { ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? ? ? try { ? ? ? ? ? ? ? ? if (null != connection) { ? ? ? ? ? ? ? ? ? ? StringBuilder result = new StringBuilder(); ? ? ? ? ? ? ? ? ? ? // 定義 BufferedReader輸入流來(lái)讀取URL的響應(yīng) ? ? ? ? ? ? ? ? ? ? errorReader = new BufferedReader(new InputStreamReader( ? ? ? ? ? ? ? ? ? ? ? ? ? ? connection.getErrorStream(), responseEncoding)); ? ? ? ? ? ? ? ? ? ? String line; ? ? ? ? ? ? ? ? ? ? while ((line = errorReader.readLine()) != null) { ? ? ? ? ? ? ? ? ? ? ? ? result.append(line).append("\n"); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? strResponse = new StrResponse(); ? ? ? ? ? ? ? ? ? ? strResponse.setCode(connection.getResponseCode()); ? ? ? ? ? ? ? ? ? ? // 返回的header ? ? ? ? ? ? ? ? ? ? Map<String, List<String>> map = connection.getHeaderFields(); ? ? ? ? ? ? ? ? ? ? strResponse.setHeaders(map); ? ? ? ? ? ? ? ? ? ? // 返回的body ? ? ? ? ? ? ? ? ? ? String responseBody = result.toString(); ? ? ? ? ? ? ? ? ? ? strResponse.setBody(responseBody); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } catch (Exception e2) { ? ? ? ? ? ? ? ? e2.printStackTrace(); ? ? ? ? ? ? } ? ? ? ? } finally { ? ? ? ? ? ? try { ? ? ? ? ? ? ? ? if (null != in) { ? ? ? ? ? ? ? ? ? ? in.close(); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? if (null != errorReader) { ? ? ? ? ? ? ? ? ? ? errorReader.close(); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } catch (IOException e) { ? ? ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? return strResponse; ? ? } ? ? public static StrResponse requestStr_responseStr(final String url, final String method, final String requestBody, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?final Map<String, String> headerMap, final String encoding) { ? ? ? ? // 字符串轉(zhuǎn)成字節(jié)流 ? ? ? ? byte[] bodyBytes = null; ? ? ? ? try { ? ? ? ? ? ? if (requestBody != null) { ? ? ? ? ? ? ? ? bodyBytes = requestBody.getBytes(encoding); ? ? ? ? ? ? } ? ? ? ? } catch (UnsupportedEncodingException e) { ? ? ? ? ? ? throw new RuntimeException(e); ? ? ? ? } ? ? ? ? return requestByte_responseStr(url, method, bodyBytes, headerMap, encoding); ? ? } }
使用方法
public class Main{ public static void main(String[] args) { String url = "http://192.168.19.11:9200/yourindex/_search"; String requestBody = "{" + "\"query\": {" + " \"bool\": {" + " \"must\": [" + " {" + " \"term\": {" + " \"areaName.keyword\": \"" + areaName + "\"" + " }" + " }," + " {" + " \"term\": {" + " \"date.keyword\": \"" + date+ "\"" + " }" + " }," + " {" + " \"term\": {\"rytype.keyword\": \"root\"}" + " }" + " ]" + " }" + " " + " }" + "}"; Map<String, String> headerMap = new HashMap<>(); headerMap.put("Content-Type", "application/json"); headerMap.put("Referer", url); String encoding = "UTF-8"; StrResponse strResponse = HttpUtils.requestStr_responseStr(url, "GET", requestBody, headerMap, encoding); String body = strResponse.getBody(); logger.info(body); } }
到此這篇關(guān)于JAVA8發(fā)送帶有Body的HTTP GET請(qǐng)求的文章就介紹到這了,更多相關(guān)JAVA8發(fā)送HTTP GET請(qǐng)求內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實(shí)現(xiàn)音頻添加自定義時(shí)長(zhǎng)靜音的示例代碼
這篇文章主要介紹了一個(gè)Java工具類,可以實(shí)現(xiàn)給一個(gè)wav音頻添加自定義時(shí)長(zhǎng)靜音。文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編學(xué)習(xí)一下2022-01-01SpringBoot之?dāng)r截器與過(guò)濾器解讀
這篇文章主要介紹了SpringBoot之?dāng)r截器與過(guò)濾器解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07Java 實(shí)戰(zhàn)項(xiàng)目之精品養(yǎng)老院管理系統(tǒng)的實(shí)現(xiàn)流程
讀萬(wàn)卷書不如行萬(wàn)里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+Springboot+Maven+mybatis+Vue+Mysql實(shí)現(xiàn)一個(gè)精品養(yǎng)老院管理系統(tǒng),大家可以在過(guò)程中查缺補(bǔ)漏,提升水平2021-11-11Springboot啟動(dòng)后執(zhí)行方法小結(jié)
本文主要介紹了Springboot啟動(dòng)后執(zhí)行方法小結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04深入分析Spring BeanDefinition的構(gòu)造元信息
Bean Definition是一個(gè)包含Bean元數(shù)據(jù)的對(duì)象,它描述了如何創(chuàng)建Bean實(shí)例、Bean屬性的值以及Bean之間的依賴關(guān)系,本文將帶大家深入分析Spring BeanDefinition的構(gòu)造元信息,需要的朋友可以參考下2024-01-01mybatis如何通過(guò)接口查找對(duì)應(yīng)的mapper.xml及方法執(zhí)行詳解
這篇文章主要給大家介紹了利用mybatis如何通過(guò)接口查找對(duì)應(yīng)的mapper.xml及方法執(zhí)行的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編一起來(lái)學(xué)習(xí)學(xué)習(xí)吧。2017-06-06Spring測(cè)試基本的控制器實(shí)戰(zhàn)示例
這篇文章主要為大家介紹了Spring測(cè)試基本的控制器實(shí)戰(zhàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10JAVA實(shí)現(xiàn)按時(shí)間段查詢數(shù)據(jù)操作
這篇文章主要介紹了JAVA實(shí)現(xiàn)按時(shí)間段查詢數(shù)據(jù)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08詳解mybatis通過(guò)mapper接口加載映射文件
本篇文章主要介紹了mybatis通過(guò)mapper接口加載映射文件 ,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08