http请求与Request常用方法

一、http请求

HTTP请求报文由3部分组成(请求行+请求头+请求体):

①是请求方法,GET和POST是最常见的HTTP方法,除此以外还包括DELETE、HEAD、OPTIONS、PUT、TRACE。不过,当前的大多数浏览器只支持GET和POST,Spring 3.0提供了一个HiddenHttpMethodFilter,允许你通过“_method”的表单参数指定这些特殊的HTTP方法(实际上还是通过POST提交表单)。服务端配置了HiddenHttpMethodFilter后,Spring会根据_method参数指定的值模拟出相应的HTTP方法,这样,就可以使用这些HTTP方法对处理方法进行映射了。 
②为请求对应的URL地址,它和报文头的Host属性组成完整的请求URL,③是协议名称及版本号。 
④是HTTP的报文头,报文头包含若干个属性,格式为“属性名:属性值”,服务端据此获取客户端的信息。 
⑤是报文体,它将一个页面表单中的组件值通过param1=value1&param2=value2的键值对形式编码成一个格式化串,它承载多个请求参数的数据。不但报文体可以传递请求参数,请求URL也可以通过类似于“/chapter15/user.html? param1=value1&param2=value2”的方式传递请求参数。 
对照上面的请求报文,我们把它进一步分解,你可以看到一幅更详细的结构图:  --------------------- 本文来自 咚浸暖的过去 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/u010256388/article/details/68491509?utm_source=copy

响应报文结构 

HTTP的响应报文也由三部分组成(响应行+响应头+响应体): 

①报文协议及版本; 
②状态码及状态描述; 
③响应报文头,也是由多个属性组成; 
④响应报文体,即我们真正要的“干货”。 

请求映射

http请求信息包含六部分信息:

扫描二维码关注公众号,回复: 3435107 查看本文章

①请求方法,如GET或POST,表示提交的方式;

②URL,请求的地址信息;

③协议及版本;

④请求头信息(包括Cookie信息);

⑤回车换行(CRLF);

⑥请求内容区(即请求的内容或数据),如表单提交时的参数数据、URL请求参数(?abc=123 ?后边的)等。

此处我们可以看到有①、②、④、⑥一般是可变的,因此我们可以这些信息进行请求到处理器的功能处理方法的映射,因此请求的映射分为如下几种:

URL路径映射:使用URL映射请求到处理器的功能处理方法;

请求方法映射限定:如限定功能处理方法只处理GET请求;

请求参数映射限定:如限定只处理包含“abc”请求参数的请求;

请求头映射限定:如限定只处理“Accept=application/json”的请求。

二、Request

  HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,通过这个对象提供的方法,可以获得客户端请求的所有信息。

获得客户机信息

  getRequestURL方法返回客户端发出请求时的完整URL。
  getRequestURI方法返回请求行中的资源名部分。
  getQueryString 方法返回请求行中的参数部分。
  getPathInfo方法返回请求URL中的额外路径信息。额外路径信息是请求URL中的位于Servlet的路径之后和查询参数之前的内容,它以“/”开头。
  getRemoteAddr方法返回发出请求的客户机的IP地址。
  getRemoteHost方法返回发出请求的客户机的完整主机名。
  getRemotePort方法返回客户机所使用的网络端口号。
  getLocalAddr方法返回WEB服务器的IP地址。
  getLocalName方法返回WEB服务器的主机名。

获得客户机请求头

  getHeader(string name)方法:String 
  getHeaders(String name)方法:Enumeration 
  getHeaderNames()方法

获得客户机请求参数(客户端提交的数据)

  • getParameter(String)方法(常用)
  • getParameterValues(String name)方法(常用)
  • getParameterNames()方法(不常用)
  • getParameterMap()方法(编写框架时常用)

在服务器端使用getParameter方法和getParameterValues方法接收表单参数

import java.io.BufferedReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.Principal;
import java.util.Enumeration;
import java.util.Locale;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletInputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


/**
 * 代理线程中的HttpServletRequest变量,使代码中可以通过静态方法访问request
 * @version $Revision: 9961 $
 */
public class Request {
	/**
	 * 输入参数
	 */
	public enum In {
		/** */
		RECORD {
			/** {@inheritDoc} */
			public String toString() {
				return "record";
			}
		}
	}

	/**
	 * 获取Request实例
	 *
	 * @return Request实例对象
	 */
	public static HttpServletRequest getInst() {
		return (HttpServletRequest) AppMgr.threadVar("request");
	}

	/**
	 * TODO$
	 *
	 * @param request
	 *            void
	 */
	public static void setInst(HttpServletRequest request) {
		AppMgr.setThreadVar("request", request);
	}

	/**
	 * 获取当前用户
	 *
	 * @return 用户信息
	 */
	public static User curUser() {
		try {
			return (User) getSession(true).getAttribute(User.KEY_SESSION_USER);
		} catch (Exception e) {
			// 没有用户用guest处理
			IBean guest = new Bean();
			guest.set("USER_CODE", "guest");
			guest.set("USER_NAME", "guest");
			return new User(guest);
		}
	}

	/**
	 * 返回这个请求使用的HTTP方法(例如:GET、POST、PUT)。
	 *
	 * @return 方法名称
	 */
	public static String getMethod() {
		return getInst().getMethod();
	}

	/**
	 * 返回请求URL中访问路径。 如果有查询字符串存在,将不包括在返回值当中。 例如:
	 * 请求URL为http://localhost:8080/web/main/list.do?id=1,将返回/web/main/list.do
	 *
	 * @return URI
	 */
	public static String getRequestURI() {
		return getInst().getRequestURI();
	}

	/**
	 * 返回请求的URL。包含协议(例如http和https)和端口,但不包含查询字符串。
	 *
	 * @return 包含URL的StringBuffer
	 */
	public static StringBuffer getRequestURL() {
		return getInst().getRequestURL();
	}

	/**
	 * 返回web应用名称路径,如为根目录,则返回""。 例如:
	 * web应用名称是web,请求URL为http://localhost:8080/web/main/list.do?id=1,将返回/web
	 *
	 * @return Context Path
	 */
	public static String getContextPath() {
		return getInst().getContextPath();
	}

	/**
	 * 返回servlet路径。 例如:
	 * web应用名称是web,请求URL为http://localhost:8080/web/main/list.do?
	 * id=1,将返回/main/list.do
	 *
	 * @return Servlet Path
	 */
	public static String getServletPath() {
		return getInst().getServletPath();
	}

	/**
	 * 返回这个URL请求的Servlet路径之后的额外路径信息。 例如:
	 * 请求URL为http://localhost:8080/web/main/list/product1/,
	 * 其中Servlet的url-pattern为/main/list/* 则返回/product1/
	 *
	 * @return Path Info
	 */
	public static String getPathInfo() {
		return getInst().getPathInfo();
	}

	/**
	 * 这个方法获得这个请求的URL的Servlet路径之后的额外的路径信息,并将它转换成一个真实的路径。
	 *
	 * @return path
	 */
	public static String getPathTranslated() {
		return getInst().getPathTranslated();
	}

	/**
	 * 返回请求URL所包含的查询字符串。
	 *
	 * @return 查询字符串
	 */
	public static String getQueryString() {
		return getInst().getQueryString();
	}

	/**
	 * 返回一个请求头域的值。 如果这个请求头域不存在,这个方法返回null。
	 *
	 * @param name
	 *            头域名称
	 * @return 头域值
	 */
	public static String getHeader(String name) {
		return getInst().getHeader(name);
	}

	/**
	 * 返回一个请求头域的值列表。
	 *
	 * @param name
	 *            头域名
	 * @return 包含头域值的Enumeration对象
	 */
	public static Enumeration getHeaders(String name) {
		return getInst().getHeaders(name);
	}

	/**
	 * 返回请求的所有头域名。
	 *
	 * @return 包含所有头域名的Enumeration对象
	 */
	public static Enumeration getHeaderNames() {
		return getInst().getHeaderNames();
	}

	/**
	 * 返回指定的请求头域的值,这个值被转换成一个整数。
	 *
	 * @param name
	 *            头域名
	 * @return 值为整数的头域值
	 */
	public static int getIntHeader(String name) {
		return getInst().getIntHeader(name);
	}

	/**
	 * 返回指定的请求头域的值,这个值被转换成一个自1970-1-1日(GMT)以来的精确到毫秒的长整数。
	 *
	 * @param name
	 *            头域名
	 * @return 值为长整数的头域值
	 */
	public static long getDateHeader(String name) {
		return getInst().getDateHeader(name);
	}

	/**
	 * 返回一个Cookie数组,该数组包含这个请求中当前的所有Cookie。 如果这个请求中没有Cookie,返回一个空数组。
	 *
	 * @return Cookie数组
	 */
	public static Cookie[] getCookies() {
		return getInst().getCookies();
	}

	/**
	 * 返回当前请求的Session
	 *
	 * @param create
	 *            没有有效的Session时,是否创建新Session,不创建返回null
	 * @return Session
	 */
	public static HttpSession getSession(boolean create) {
		return getInst().getSession(create);
	}

	/**
	 * 返回当前请求的Session
	 *
	 * @return Session
	 */
	public static HttpSession getSession() {
		return getInst().getSession();
	}

	/**
	 * 返回当前请求的Session Id
	 *
	 * @return Session Id
	 */
	public static String getRequestedSessionId() {
		return getInst().getRequestedSessionId();
	}

	/**
	 * 检查当前请求的Session是否可用。
	 *
	 * @return boolean值,是否可用
	 */
	public static boolean isRequestedSessionIdValid() {
		return getInst().isRequestedSessionIdValid();
	}

	/**
	 * 判断请求的Session Id是否是通过客户端的一个cookie提供的。
	 *
	 * @return boolean值,是否是cookie提供
	 */
	public static boolean isRequestedSessionIdFromCookie() {
		return getInst().isRequestedSessionIdFromCookie();
	}

	/**
	 * 如果这个请求的Session Id是通过客户端的URL的一部分提供的,该方法返回真,否则返回假。
	 *
	 * @return boolean值,Session Id是否来自URL
	 */
	public static boolean isRequestedSessionIdFromURL() {
		return getInst().isRequestedSessionIdFromURL();
	}

	/**
	 * 返回这个请求的身份验证模式。
	 *
	 * @return 如BASIC_AUTH, FORM_AUTH, CLIENT_CERT_AUTH, DIGEST_AUTH(suitable
	 *         for == comparison) 或者其他模式 如果请求没有验证, 则返回null
	 */
	public static String getAuthType() {
		return getInst().getAuthType();
	}

	/**
	 * 返回作了验证请求的用户名。 如果在请求中没有用户名信息,这个方法返回null。
	 *
	 * @return 用户名
	 */
	public static String getRemoteUser() {
		return getInst().getRemoteUser();
	}

	/**
	 * 判断验证用户是否包含在一个角色中。
	 *
	 * @param role
	 *            角色名称
	 * @return 是否包含在角色中
	 */
	public static boolean isUserInRole(String role) {
		return getInst().isUserInRole(role);
	}

	/**
	 * 返回一个java.security.Principal对象,此对象包含了验证用的名称。 如果没有验证用户,则返回null。
	 *
	 * @return Principal对象
	 */
	public static Principal getUserPrincipal() {
		return getInst().getUserPrincipal();
	}

	/**
	 * 返回这个请求所用的协议,其形式是协议/主版本号.次版本号。例如对于一个HTTP1.0的请求,该方法返回HTTP/1.0。
	 *
	 * @return 请求协议
	 */
	public static String getProtocol() {
		return getInst().getProtocol();
	}

	/**
	 * 返回请求URL所使用的Scheme。 例如: http、https或者ftp等。
	 *
	 * @return Scheme名称
	 */
	public static String getScheme() {
		return getInst().getScheme();
	}

	/**
	 * 返回接收请求的服务器的主机名。
	 *
	 * @return 主机名
	 */
	public static String getServerName() {
		return getInst().getServerName();
	}

	/**
	 * 返回接收请求的端口号。
	 *
	 * @return 端口号
	 */
	public static int getServerPort() {
		return getInst().getServerPort();
	}

	/**
	 * 返回请求者的IP地址。
	 *
	 * @return IP地址
	 */
	public static String getRemoteAddr() {
		return getInst().getRemoteAddr();
	}

	/**
	 * 返回请求者的主机名称。不能或者选择不解析主机名,将会直接返回IP地址。
	 *
	 * @return 主机名称
	 */
	public static String getRemoteHost() {
		return getInst().getRemoteHost();
	}

	/**
	 * 返回请求者的端口号。
	 *
	 * @return 端口号
	 */
	public static int getRemotePort() {
		return getInst().getRemotePort();
	}

	/**
	 * 获得本地IP。
	 *
	 * @return IP地址
	 */
	public static String getLocalAddr() {
		return getInst().getLocalAddr();
	}

	/**
	 * 获得本地主机名。
	 *
	 * @return 主机名
	 */
	public static String getLocalName() {
		return getInst().getLocalName();
	}

	/**
	 * 获得本地端口号
	 *
	 * @return 端口号
	 */
	public static int getLocalPort() {
		return getInst().getLocalPort();
	}

	/**
	 * 重新设置请求的字符编码。 这个方法必需在读取请求参数或流之前调用。
	 *
	 * @param env
	 *            字符编码名称
	 * @throws UnsupportedEncodingException
	 *             如果字符编码不可用
	 */
	public static void setCharacterEncoding(String env)
			throws UnsupportedEncodingException {
		getInst().setCharacterEncoding(env);
	}

	/**
	 * 返回指定请求参数的值,如果这个参数不存在返回null。
	 *
	 * @param name
	 *            参数名称
	 * @return 参数值
	 */
	public static String getParameter(String name) {
		return getInst().getParameter(name);
	}

	/**
	 * 返回指定请求参数的值(String数组),如果这个参数不存在返回null。
	 *
	 * @param name
	 *            参数名称
	 * @return 参数值数组
	 */
	public static String[] getParameterValues(String name) {
		return getInst().getParameterValues(name);
	}

	/**
	 * 返回所有参数名的列表。
	 *
	 * @return 包含所有参数名的Enumeration对象
	 */
	public static Enumeration getParameterNames() {
		return getInst().getParameterNames();
	}

	/**
	 * 返回请求参数的Map对象
	 *
	 * @return Map对象
	 */
	public static Map getParameterMap() {
		return getInst().getParameterMap();
	}

	/**
	 * 返回一个输入流。
	 *
	 * @return 输入流对象ServletInputStream
	 * @throws IOException
	 *             IO异常
	 */
	public static ServletInputStream getInputStream() throws IOException {
		return getInst().getInputStream();
	}

	/**
	 * 返回读取请求的BufferedReader。
	 *
	 * @return BufferedReader
	 * @throws IOException
	 *             IO异常
	 * @throws IllegalStateException
	 *             如果这个请求的输入流已经被getInputStream调用获得
	 */
	public static BufferedReader getReader() throws IOException,
			IllegalStateException {
		return getInst().getReader();
	}

	/**
	 * 返回请求的字符编码。
	 *
	 * @return 字符编码
	 */
	public static String getCharacterEncoding() {
		return getInst().getCharacterEncoding();
	}

	/**
	 * 请求内容的长度,如果长度未知就返回-1。
	 *
	 * @return 长度整数
	 */
	public static int getContentLength() {
		return getInst().getContentLength();
	}

	/**
	 * 返回请求的MIME类型,如果类型未知返回null。
	 *
	 * @return MIME类型
	 */
	public static String getContentType() {
		return getInst().getContentType();
	}

	/**
	 * 得到Locale对象。
	 *
	 * @return Locale对象
	 */
	public static Locale getLocale() {
		return getInst().getLocale();
	}

	/**
	 * 得到Locale对象列表。
	 *
	 * @return 包含所有Locale对象的Enumeration
	 */
	public static Enumeration getLocales() {
		return getInst().getLocales();
	}

	/**
	 * 请求是否使用了安全通道,如https
	 *
	 * @return boolean值
	 */
	public static boolean isSecure() {
		return getInst().isSecure();
	}

	/**
	 * 返回指定属性的值(Object),如不存在则返回null。
	 *
	 * @param name
	 *            属性名
	 * @return 值对象
	 */
	public static Object getAttribute(String name) {
		return getInst().getAttribute(name);
	}

	/**
	 * 设置一个属性
	 *
	 * @param name
	 *            属性名称
	 * @param o
	 *            值对象
	 */
	public static void setAttribute(String name, Object o) {
		getInst().setAttribute(name, o);
	}

	/**
	 * 得到所有request中的属性名称列表。
	 *
	 * @return Enumeration对象,包含了所有request属性名称。
	 */
	public static Enumeration getAttributeNames() {
		return getInst().getAttributeNames();
	}

	/**
	 * 移除一个request属性。
	 *
	 * @param name
	 *            属性名称
	 */
	public static void removeAttribute(String name) {
		getInst().removeAttribute(name);
	}

	/**
	 * 返回一个指定路径的RequestDispatcher对象,如果过路径无效返回null。
	 *
	 * @param path
	 *            路径
	 * @return RequestDispatcher对象
	 */
	public static RequestDispatcher getRequestDispatcher(String path) {
		return getInst().getRequestDispatcher(path);
	}

	/**
	 * 获取DATA标签内参数值
	 *
	 * @param name
	 *            DATA标签内的编码
	 * @return 编码值
	 */
	public static String getParamBean(String name) {
		String rtnVal = "";
		IBean paramBean = (IBean) AppMgr.threadVar("paramBean");
		if (paramBean != null) {
			rtnVal = String.valueOf(paramBean.get(name));
		}
		return rtnVal != null && rtnVal.length() > 0 ? rtnVal : getInst()
				.getParameter(name);
	}

	public static String getWebAddress() {
		StringBuffer webAddress = new StringBuffer(getInst().getScheme());
		webAddress.append("://").append(getInst().getServerName());
		if (80!=getInst().getServerPort()) {
			webAddress.append(":").append(getInst().getServerPort());
		}
		webAddress.append(getInst().getContextPath());
		return webAddress.toString()+"/";
	}
}

猜你喜欢

转载自blog.csdn.net/qq_35029061/article/details/82915076