How To Get HTTP Request Header In Java
By:Roy.LiuLast updated:2019-08-17
This example shows you how to get the HTTP request headers in Java. To get the HTTP request headers, you need this class HttpServletRequest :
1. HttpServletRequest Examples
1.1 Loop over the request header’s name and print out its value.
WebUtils.java
package com.mkyong.web.utils;
import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
public class WebUtils {
private Map<String, String> getHeadersInfo(HttpServletRequest request) {
Map<String, String> map = new HashMap<String, String>();
Enumeration headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()) {
String key = (String) headerNames.nextElement();
String value = request.getHeader(key);
map.put(key, value);
return map;
Request Headers example :
"headers" : {
"Host" : "mkyong.com",
"Accept-Encoding" : "gzip,deflate",
"X-Forwarded-For" : "66.249.x.x",
"X-Forwarded-Proto" : "http",
"User-Agent" : "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)",
"X-Request-Start" : "1389158003923",
"Accept" : "*/*",
"Connection" : "close",
"X-Forwarded-Port" : "80",
"From" : "googlebot(at)googlebot.com"
1.2 Get the “user-agent” header only.
WebUtils.java
package com.mkyong.web.utils;
import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
public class WebUtils {
private String getUserAgent(HttpServletRequest request) {
return request.getHeader("user-agent");
User agent example :
Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)
2. Spring MVC Example
In Spring MVC, you can @Autowired the HttpServletRequest into any Spring managed bean directly.
SiteController.java
package com.mkyong.web.controller;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/site")
public class SiteController {
@Autowired
private HttpServletRequest request;
@RequestMapping(value = "/{input:.+}", method = RequestMethod.GET)
public ModelAndView getDomain(@PathVariable("input") String input) {
ModelAndView modelandView = new ModelAndView("result");
modelandView.addObject("user-agent", getUserAgent());
modelandView.addObject("headers", getHeadersInfo());
return modelandView;
//get user agent
private String getUserAgent() {
return request.getHeader("user-agent");
//get request headers
private Map<String, String> getHeadersInfo() {
Map<String, String> map = new HashMap<String, String>();
Enumeration headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()) {
String key = (String) headerNames.nextElement();
String value = request.getHeader(key);
map.put(key, value);
return map;
Declare this dependency in pom.xml, if HttpServletRequest is unable to find.
<dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency>
References
From:一号门

COMMENTS