java獲取http請求的Header和Body的簡單方法
在http請求中,有Header和Body之分,讀取header使用request.getHeader("...");
讀取Body使用request.getReader(),但getReader獲取的是BufferedReader,需要把它轉(zhuǎn)換成字符串,下面是轉(zhuǎn)換的方法。
public class TestController {
@RequestMapping("/a")
protected void doPost(HttpServletRequest request,
HttpServletResponse response, BufferedReader br)
throws ServletException, IOException {
//Header部分
System.out.print(request.getHeaderNames());
Enumeration<?> enum1 = request.getHeaderNames();
while (enum1.hasMoreElements()) {
String key = (String) enum1.nextElement();
String value = request.getHeader(key);
System.out.println(key + "\t" + value);
}
//body部分
String inputLine;
String str = "";
try {
while ((inputLine = br.readLine()) != null) {
str += inputLine;
}
br.close();
} catch (IOException e) {
System.out.println("IOException: " + e);
}
System.out.println("str:" + str);
}
以上就是小編為大家?guī)淼膉ava獲取http請求的Header和Body的簡單方法全部內(nèi)容了,希望大家多多支持腳本之家~
相關(guān)文章
SpringBoot?整合RabbitMq?自定義消息監(jiān)聽容器來實現(xiàn)消息批量處理
Spring Boot中提供了默認(rèn)的監(jiān)聽器容器,但是有時候我們需要自定義監(jiān)聽器容器,來滿足一些特殊的需求,比如批量獲取數(shù)據(jù),這篇文章主要介紹了SpringBoot?整合RabbitMq?自定義消息監(jiān)聽容器來實現(xiàn)消息批量處理,需要的朋友可以參考下2023-04-04
Spring Bean實例的創(chuàng)建及構(gòu)造器的挑選
這篇文章主要介紹了Spring Bean實例的創(chuàng)建及構(gòu)造器的挑選,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)java的小伙伴們有很好的幫助,需要的朋友可以參考下2021-04-04
SpringBoot如何訪問不同的數(shù)據(jù)庫的方法實現(xiàn)
本文主要介紹了在SpringBoot應(yīng)用中配置和管理多個數(shù)據(jù)源的方法,包括使用SpringBoot官方支持的配置方式和第三方庫實現(xiàn)多數(shù)據(jù)源配置,感興趣的可以了解一下2024-11-11
springboot 定時任務(wù)@Scheduled實現(xiàn)解析
這篇文章主要介紹了springboot 定時任務(wù)@Scheduled實現(xiàn)解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-09-09

