Как получить заголовок HTTP-запроса в Java
В этом примере показано, как получить заголовки HTTP-запроса в Java. Чтобы получить заголовки HTTP-запроса, вам понадобится этот классHttpServletRequest
:
1. Примеры HttpServletRequest
1.1 Loop over the request header’s name and print out its value.
WebUtils.java
package com.example.web.utils; import javax.servlet.http.HttpServletRequest; import java.util.Enumeration; import java.util.HashMap; import java.util.Map; public class WebUtils { private MapgetHeadersInfo(HttpServletRequest request) { Map map = new HashMap (); Enumeration headerNames = request.getHeaderNames(); while (headerNames.hasMoreElements()) { String key = (String) headerNames.nextElement(); String value = request.getHeader(key); map.put(key, value); } return map; } }
Пример запроса заголовков:
"headers" : { "Host" : "example.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.example.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"); } }
Пример пользовательского агента:
Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)
2. Spring MVC Пример
В Spring MVC вы можете@Autowired
HttpServletRequest
напрямую в любой управляемый компонент Spring.
SiteController.java
package com.example.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 MapgetHeadersInfo() { Map map = new HashMap (); Enumeration headerNames = request.getHeaderNames(); while (headerNames.hasMoreElements()) { String key = (String) headerNames.nextElement(); String value = request.getHeader(key); map.put(key, value); } return map; } }
Объявите эту зависимость вpom.xml
, еслиHttpServletRequest
не может найти.
javax.servlet servlet-api 2.5