JavaでHTTPリクエストヘッダを取得する方法

JavaでHTTP要求ヘッダーを取得する方法

この例は、JavaでHTTP要求ヘッダーを取得する方法を示しています。 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 Map getHeadersInfo(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では、HttpServletRequestを任意のSpring管理対象Beanに直接@Autowiredすることができます。

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 Map getHeadersInfo() {

        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;
    }

}

HttpServletRequestが見つからない場合は、この依存関係をpom.xmlで宣言します。

    
        javax.servlet
        servlet-api
        2.5