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

相关推荐
寻星探路34 分钟前
【深度长文】万字攻克网络原理:从 HTTP 报文解构到 HTTPS 终极加密逻辑
java·开发语言·网络·python·http·ai·https
王达舒199435 分钟前
HTTP vs HTTPS: 终极解析,保护你的数据究竟有多重要?
网络协议·http·https
朱皮皮呀36 分钟前
HTTPS的工作过程
网络协议·http·https
Binary-Jeff41 分钟前
一文读懂 HTTPS 协议及其工作流程
网络协议·web安全·http·https
那就回到过去8 小时前
MPLS多协议标签交换
网络·网络协议·hcip·mpls·ensp
胖咕噜的稞达鸭13 小时前
网络基础:初识TCP/IP协议
网络·网络协议·tcp/ip
全栈工程师修炼指南13 小时前
Nginx | stream content 阶段:UDP 协议四层反向代理浅析与实践
运维·网络·网络协议·nginx·udp
cur1es14 小时前
【UDP的报文结构】
网络·网络协议·udp·md5
闲人编程14 小时前
使用FastAPI和WebSocket构建高性能实时聊天系统
websocket·网络协议·网络编程·fastapi·持久化·实时聊天·codecapsule
惊讶的猫14 小时前
OpenFeign(声明式HTTP客户端)
网络·网络协议·http·微服务·openfeign