亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

JAVA8發(fā)送帶有Body的HTTP GET請求

 更新時間:2022年06月27日 10:47:30   作者:zhangchao19890805  
本文主要介紹了JAVA8發(fā)送帶有Body的HTTP GET請求,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

正常來講,按照HTTP標準,GET請求事不能帶有消息體BODY的。但是HTTP標準不是硬性規(guī)定,各個廠商可以根據(jù)自己的需求做成靈活的擴展。比如ES的搜索接口就要求客戶端發(fā)送帶有BODY的HTTP GET請求。

發(fā)送請求的代碼分成兩個類,接收返回數(shù)據(jù)的 StrResponse 和發(fā)起請求的工具欄 HttpUtils

StrResponse.java

import java.util.List;
import java.util.Map;

/**
?* 接收HTTP返回數(shù)據(jù)的對象
?* @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);
? ? ? ? ? ? // 打開和URL之間的連接
? ? ? ? ? ? connection = (HttpURLConnection) realUrl.openConnection();
? ? ? ? ? ? connection.setRequestMethod(method);
? ? ? ? ? ? // 請求內(nèi)容的長度
? ? ? ? ? ? if (null != requestBody && requestBody.length > 0) {
? ? ? ? ? ? ? ? connection.setRequestProperty("Content-Length", String.valueOf(requestBody.length));
? ? ? ? ? ? }
? ? ? ? ? ? // 自定義請求頭
? ? ? ? ? ? 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請求的body中
? ? ? ? ? ? connection.setDoOutput(true);
? ? ? ? ? ? if (null != requestBody && requestBody.length > 0) {
? ? ? ? ? ? ? ? connection.getOutputStream().write(requestBody);
? ? ? ? ? ? }
? ? ? ? ? ? // 定義 BufferedReader輸入流來讀取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輸入流來讀取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請求的文章就介紹到這了,更多相關(guān)JAVA8發(fā)送HTTP GET請求內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java實現(xiàn)音頻添加自定義時長靜音的示例代碼

    Java實現(xiàn)音頻添加自定義時長靜音的示例代碼

    這篇文章主要介紹了一個Java工具類,可以實現(xiàn)給一個wav音頻添加自定義時長靜音。文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編學習一下
    2022-01-01
  • SpringBoot之攔截器與過濾器解讀

    SpringBoot之攔截器與過濾器解讀

    這篇文章主要介紹了SpringBoot之攔截器與過濾器解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • Java 實戰(zhàn)項目之精品養(yǎng)老院管理系統(tǒng)的實現(xiàn)流程

    Java 實戰(zhàn)項目之精品養(yǎng)老院管理系統(tǒng)的實現(xiàn)流程

    讀萬卷書不如行萬里路,只學書上的理論是遠遠不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+Springboot+Maven+mybatis+Vue+Mysql實現(xiàn)一個精品養(yǎng)老院管理系統(tǒng),大家可以在過程中查缺補漏,提升水平
    2021-11-11
  • Springboot啟動后執(zhí)行方法小結(jié)

    Springboot啟動后執(zhí)行方法小結(jié)

    本文主要介紹了Springboot啟動后執(zhí)行方法小結(jié),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-04-04
  • Kafka簡單客戶端編程實例

    Kafka簡單客戶端編程實例

    這篇文章主要為大家詳細介紹了Kafka簡單客戶端編程實例,利用Kafka的API進行客戶端編程,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • 深入分析Spring BeanDefinition的構(gòu)造元信息

    深入分析Spring BeanDefinition的構(gòu)造元信息

    Bean Definition是一個包含Bean元數(shù)據(jù)的對象,它描述了如何創(chuàng)建Bean實例、Bean屬性的值以及Bean之間的依賴關(guān)系,本文將帶大家深入分析Spring BeanDefinition的構(gòu)造元信息,需要的朋友可以參考下
    2024-01-01
  • mybatis如何通過接口查找對應(yīng)的mapper.xml及方法執(zhí)行詳解

    mybatis如何通過接口查找對應(yīng)的mapper.xml及方法執(zhí)行詳解

    這篇文章主要給大家介紹了利用mybatis如何通過接口查找對應(yīng)的mapper.xml及方法執(zhí)行的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家具有一定的參考學習價值,需要的朋友們下面跟著小編一起來學習學習吧。
    2017-06-06
  • Spring測試基本的控制器實戰(zhàn)示例

    Spring測試基本的控制器實戰(zhàn)示例

    這篇文章主要為大家介紹了Spring測試基本的控制器實戰(zhàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-10-10
  • JAVA實現(xiàn)按時間段查詢數(shù)據(jù)操作

    JAVA實現(xiàn)按時間段查詢數(shù)據(jù)操作

    這篇文章主要介紹了JAVA實現(xiàn)按時間段查詢數(shù)據(jù)操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • 詳解mybatis通過mapper接口加載映射文件

    詳解mybatis通過mapper接口加載映射文件

    本篇文章主要介紹了mybatis通過mapper接口加載映射文件 ,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08

最新評論