request.setCharacterEncoding("UTF-8");
Enumeration<String> enu = request.getHeaderNames();
while(enu.hasMoreElements()){
String headerName = enu.nextElement();
System.out.println(headerName+": "+ request.getHeader(headerName));
}
控制台会显示:
accept: application/x-shockwave-flash, image/gif, image/jpeg, image/pjpeg, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, */*
referer: http://localhost:8888/1201test/index.jsp
accept-language: zh-cn,en-GB;q=0.5
user-agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET4.0C; .NET4.0E)
content-type: application/x-www-form-urlencoded
accept-encoding: gzip, deflate
host: localhost:8888
content-length: 50
connection: Keep-Alive
cache-control: no-cache
cookie: JSESSIONID=1025111EA7268F96AF27DFCEC7DC136F
Enumeration<String> enu = request.getHeaderNames();
while(enu.hasMoreElements()){
String headerName = enu.nextElement();
System.out.println(headerName+": "+ request.getHeader(headerName));
}
控制台会显示:
accept: application/x-shockwave-flash, image/gif, image/jpeg, image/pjpeg, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, */*
referer: http://localhost:8888/1201test/index.jsp
accept-language: zh-cn,en-GB;q=0.5
user-agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET4.0C; .NET4.0E)
content-type: application/x-www-form-urlencoded
accept-encoding: gzip, deflate
host: localhost:8888
content-length: 50
connection: Keep-Alive
cache-control: no-cache
cookie: JSESSIONID=1025111EA7268F96AF27DFCEC7DC136F
本文通过一个具体的示例展示了如何使用Java代码解析HTTP请求头信息。包括设置字符编码为UTF-8,并遍历打印所有请求头及其对应的值。涉及到的请求头包括accept、referer、accept-language等。
1041

被折叠的 条评论
为什么被折叠?



