HTTP协议-请求协议

java 复制代码
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController//表
public class request {

    @RequestMapping("/request")
    public String request(HttpServletRequest request) {
        // 1. 获取请求方式
        String method = request.getMethod(); // GET
        System.out.println("请求方式:" + method);

        // 2. 获取请求url地址
        String url = request.getRequestURL().toString(); // http://localhost:8080/request
        System.out.println("请求URL地址:" + url);

        String uri = request.getRequestURI(); // /request
        System.out.println("请求URI地址:" + uri);

        // 3. 获取请求协议
        String protocol = request.getProtocol(); // HTTP/1.1
        System.out.println("请求协议:" + protocol);

        // 4. 获取请求参数 - name, age
        String name = request.getParameter("name");
        String age = request.getParameter("age");
        System.out.println("name: " + name + ", age: " + age);

        // 5. 获取请求头 - Accept
        String accept = request.getHeader("Accept");
        System.out.println("Accept: " + accept);

        return "OK";
    }
}

网页端输出

输出端输出

2025-04-24T23:03:39.364+08:00 INFO 17836 --- [springboot-web-start] [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'

2025-04-24T23:03:39.366+08:00 INFO 17836 --- [springboot-web-start] [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'

2025-04-24T23:03:39.367+08:00 INFO 17836 --- [springboot-web-start] [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms

请求方式:GET

请求URL地址:http://localhost:8080/request

请求URI地址:/request

请求协议:HTTP/1.1

name: xaio, age: 18

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7

相关推荐
hgdlip4 分钟前
关闭IP属地显示会影响账号的正常使用吗
网络·网络协议·tcp/ip·ip属地
Zz_waiting.32 分钟前
网络原理 - 7(TCP - 4)
网络·网络协议·tcp/ip
zheshiyangyang43 分钟前
HTTP相关
网络·网络协议·http
我的golang之路果然有问题2 小时前
快速上手GO的net/http包,个人学习笔记
笔记·后端·学习·http·golang·go·net
爱编程的鱼2 小时前
Windows 各版本查找计算机 IP 地址指南
人工智能·windows·网络协议·tcp/ip·tensorflow
乘风破浪的咸鱼君2 小时前
一天学完Servlet!!!(万字总结)
servlet
xxy!4 小时前
OSI七层模型和TCP/IP四层模型
网络·网络协议·tcp/ip
异常君7 小时前
HTTP头中的Accept-Encoding与Content-Encoding深度剖析
后端·nginx·http
神的孩子都在歌唱9 小时前
网络IP冲突的成因与解决方案
网络·网络协议·tcp/ip