HTTP之request请求
Posted hejh
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HTTP之request请求相关的知识,希望对你有一定的参考价值。
HTTP之request请求
request:请求 作用:获取浏览器发送过来的数据 组成部分: 请求行 请求头 请求体 操作请求行 格式: 请求方式 请求资源 协议/版本 常用方法:HttpServletRequest 掌握 String getMethod():获取请求方式 String getRemoteAddr():获取ip地址 String getContextPath() :在java中获取项目名称 (/day10) 了解: getRequestURI():获取的是 从项目名到参数之前的内容 /day10/regist getRequestURL():获取的带协议的完整路径 http://localhost/day10/regist String getQueryString():get请求的所有参数 username=tom&password=123 String getProtocol():获取协议和版本 例如:请求行 GET /day10/row?username=tom&password=123 HTTP/1.1
案例:请求头方法
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <servlet> <servlet-name>RowServlet</servlet-name> <servlet-class>com.hjh.servlet.request.RowServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>RowServlet</servlet-name> <url-pattern>/row</url-pattern> </servlet-mapping> </web-app>
package com.hjh.servlet.request; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class RowServlet extends HttpServlet private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException //获取请求方式 String method = request.getMethod(); System.out.println("请求方式:"+method); //获取请求资源 String uri = request.getRequestURI(); String url = request.getRequestURL().toString(); System.out.println("uri:"+uri+" "+" url:"+url); //获取请求参数的字符串 String query = request.getQueryString(); System.out.println("请求参数字符串:"+query); //获取协议版本 String protocol = request.getProtocol(); System.out.println("协议版本:"+protocol); System.out.println("---------------------------以下为重要方法----------------------------"); //获取请求IP String addr = request.getRemoteAddr(); System.out.println("请求IP:"+addr); //获取项目名 String contextPath = request.getContextPath(); System.out.println("项目名:"+contextPath); protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException doGet(request, response);
启动项目,浏览器输入url“http://localhost:8080/Servlet/row?username=hjh&password=12345”,回车,eclipse中console输出为:
请求方式:GET uri:/Servlet/row url:http://localhost:8080/Servlet/row 请求参数字符串:username=hjh&password=12345 协议版本:HTTP/1.1 ---------------------------以下为重要方法---------------------------- 请求IP:0:0:0:0:0:0:0:1 项目名:/Servlet
操作请求头 格式:key/value(value可以是多个值) 常用方法: ★String getHeader(String key):通过key获取指定的value (一个) 了解: Enumeration getHeaders(String name) :通过key获取指定的value(多个) Enumeration getHeaderNames() :获取所有的请求头的名称 int getIntHeader(String key):获取整型的请求头 long getDateHeader(String key):获取时间的请求头 重要的请求头: user-agent:浏览器内核 msie firefox chrome referer:页面从那里来 防盗链
案例:请求头
<servlet> <servlet-name>HeaderServlet</servlet-name> <servlet-class>com.hjh.servlet.request.HeaderServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>HeaderServlet</servlet-name> <url-pattern>/header</url-pattern> </servlet-mapping>
package com.hjh.servlet.request; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class HeaderServlet extends HttpServlet private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException //获取浏览器内核 String user_agent = request.getHeader("user-agent"); System.out.println("浏览器内核:"+user_agent); //获取referer String referer = request.getHeader("referer"); if(referer==null) System.out.println("直接在地址栏输入的"); else if(referer.contains("localhost")) System.out.println("本机输入"); else if(referer.contains("192.167.23.156")) System.out.println("其他人的"); else System.out.println("兄弟,盗链可耻"); protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException doGet(request, response);
url http://localhost:8080/Servlet/header
console
浏览器内核:Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/58.0.3029.96 Safari/537.36 直接在地址栏输入的
以上是关于HTTP之request请求的主要内容,如果未能解决你的问题,请参考以下文章