package com.tcm.web.controller.request.demo;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
import java.util.Map.Entry;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* 测试请求头哪些内容
* 参考资料:
* 1. HTTP Content-Type的对照:http://tool.oschina.net/commons
* 2. HTTP状态码详解:http://tool.oschina.net/commons?type=5
* 3. HTTP请求头和响应头的对照:http://tools.jb51.net/table/http_header
* @author TCM
* @create 2017年3月28日下午2:30:17
*/
@Controller
public class HeaderController {
@RequestMapping("/req/header")
public String getHeader(HttpServletRequest request, HttpServletResponse response, ModelMap map){
map.addAttribute("method",request.getMethod());
/*******************************************************
* 获取客户端信息 *
*******************************************************/
//获取请求路径
System.out.println("getServletPath():"+request.getServletPath());
//获取完整URL
System.out.println("getRequestURL():"+request.getRequestURL());
//获取资源URI
System.out.println("getRequestURI():"+request.getRequestURI());
//获取服务器id
System.out.println("getLocalAddr():"+request.getLocalAddr());
//获取服务器名
System.out.println("getLocalName():"+request.getLocalName());
//获取客户机ip
System.out.println("getRemoteAddr():"+request.getRemoteAddr());
//获取客户主机名
System.out.println("getRemoteHost():"+request.getRemoteHost());
//获取服务器的项目名
System.out.println("ContextPath():"+request.getContextPath());
//获取HttpSessionId
System.out.println("getRequestedSessionId():"+request.getRequestedSessionId());
//获取HttpSession对象
System.out.println("getSession():"+request.getSession().getId());
//请求方式
System.out.println("getMethod():"+request.getMethod());
//获取查询字符串(?ni=123)
System.out.println("getQueryString():"+request.getQueryString());
//获取当前登录用户
System.out.println("getRemoteUser():"+request.getRemoteUser());
//获取当前登录用户的授权类型
System.out.println("getAuthType():"+request.getAuthType());
//获取Cookie[]
System.out.println("getCookies():"+request.getCookies().length);
/*******************************************************
* 获取请求头信息
*参考资料:
*http://tools.jb51.net/table/http_header
*http://www.cnblogs.com/wangyang108/p/5755525.html
*******************************************************/
System.out.println("\n"+"----------------获取请求头信息--------------------");
//客户端能够接收的内容类型
System.out.println("Accept:"+request.getHeader("Accept"));
//客户端可以支持的web服务器返回内容压缩编码类型
System.out.println("Accept-Encoding:"+request.getHeader("Accept-Encoding"));
//请求体对应的MIME信息
System.out.println("Content-Type:"+request.getHeader("Content-Type"));
//服务器的域名和端口号
System.out.println("Host:"+request.getHeader("Host"));
//请求的内容长度
System.out.println("Content-Length:"+request.getHeader("Content-Length"));
//请求用户的信息
System.out.println("User-Agent:"+request.getHeader("User-Agent"));
//请求域名下的所有cookie值一起发送给web服务器
System.out.println("Cookie:"+request.getHeader("Cookie"));
//是否持久连接
//Keep-Alive:不会断开,当客户端发送另外一个请求时,就使用这条已经建立的连接
System.out.println("Connection:"+request.getHeader("Connection"));
//发出请求的时间
System.out.println("Date:"+request.getHeader("Date"));
//指定请求和响应遵循的缓存机制
//max-age>0 时直接从游览器缓存中提取 ;max-age<=0 时 向server发送http请求确认 ,该资源是否有修改
System.out.println("Cache-Control:"+request.getHeader("Cache-Control"));
//HTTP授权的授权证书
System.out.println("Authorization:"+request.getHeader("Authorization"));
//发出请求的客户的email
System.out.println("From:"+request.getHeader("From"));
/*******************************************************
* 获取请求参数
*******************************************************/
System.out.println("\n"+"----------------获取请求参数--------------------");
//获取指定的请求参数
System.out.println("getParameter():"+request.getParameter("ni"));
//获取所有请求参数
Map<String, String[]> params = request.getParameterMap();
Map<String, String[]> sorted = new TreeMap<String, String[]>();
sorted.putAll(params);
Iterator<Entry<String, String[]>> pairs = sorted.entrySet().iterator();
while (pairs.hasNext()) {
Map.Entry<String, String[]> pair = pairs.next();
if (pair.getValue() != null) {
System.out.println(pair.getKey() + "=" + (pair.getValue())[0]);
} else {
System.out.println(pair.getKey() + "=");
}
}
/*******************************************************
* 响应部分
*******************************************************/
System.out.println("\n"+"----------------响应参数--------------------");
//url编码重定向
System.out.println("encodeRedirectURL():"+response.encodeRedirectURL(request.getRequestURL().toString()));
//设置响应体类型
response.setContentType("application/vnd.ms-excel;charset=utf-8");
//响应体的MIME类型
System.out.println("getContentType():"+response.getContentType());
//返回编码集
System.out.println("getCharacterEncoding():"+response.getCharacterEncoding());
//响应状态
System.out.println("getStatus():"+response.getStatus());
//缓存大小
System.out.println("getBufferSize():"+response.getBufferSize());
//清空缓存内容,不清空headers和状态码
response.resetBuffer();
//清空缓存内容、headers、状态码
response.reset();
System.out.println("getStatus():"+response.getStatus());
System.out.println("\n"+"----------------响应头--------------------");
Collection<String> list = response.getHeaderNames();
Iterator<String> ite = list.iterator();
while (ite.hasNext()) {
String head = ite.next();
System.out.println(head);
}
System.out.println("\n"+"----------------响应头--------------------");
//设置响应头
// response.setHeader("Content-Encoding", "gzip");
//web服务器支持的返回内容压缩编码类型
System.out.println("Content-Encoding:"+response.getHeader("Content-Encoding"));
//返回资源的MD5校验值
System.out.println("Content-MD5:"+response.getHeader("Content-MD5"));
//返回内容的MIME类型
System.out.println("Content-Type:"+response.getHeader("Content-Type"));
//原始服务器消息发出的时间
System.out.println("Date:"+response.getHeader("Date"));
//web服务器软件名称
System.out.println("Server:"+response.getHeader("Server"));
//重定向URL的位置
System.out.println("Location:"+response.getHeader("Location"));
//web服务器软件名称
System.out.println("Server:"+response.getHeader("Server"));
return "header/requestInfo";
}
}
Http请求头和响应头
最新推荐文章于 2023-09-07 10:15:00 发布