How To Get HTTP Request Header In Java

摘要: This example shows you how to get the HTTP request headers in Java. To get the HTTP request headers, you need this class HttpServletRequest :

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

  1. Wikipedia – List of HTTP header fields
  2. How To Get Http Response Header In Java

上一篇: Java Check if web request is from Google crawler
下一篇: Java Write directly to memory
 评论 ( What Do You Think )
名称
邮箱
网址
评论
验证
   
 

 


  • 微信公众号

  • 我的微信

站点声明:

1、一号门博客CMS,由Python, MySQL, Nginx, Wsgi 强力驱动

2、部分文章或者资源来源于互联网, 有时候很难判断是否侵权, 若有侵权, 请联系邮箱:summer@yihaomen.com, 同时欢迎大家注册用户,主动发布无版权争议的 文章/资源.

3、鄂ICP备14001754号-3, 鄂公网安备 42280202422812号