Home
  • 计算机网络
  • 操作系统
  • 数据结构与算法
  • 设计模式
  • JavaSE
  • JVM
  • JUC
  • Netty
  • CPP
  • QT
  • UE
  • Go
  • Gin
  • Gorm
  • HTML
  • CSS
  • JavaScript
  • vue2
  • TypeScript
  • vue3
  • react
  • Spring
  • SpringMVC
  • Mybatis
  • SpringBoot
  • SpringSecurity
  • SpringCloud
  • Mysql
  • Redis
  • 消息中间件
  • RPC
  • 分布式锁
  • 分布式事务
  • 个人博客
  • 弹幕视频平台
  • API网关
  • 售票系统
  • 消息推送平台
  • SaaS短链接系统
  • Linux
  • Docker
  • Git
GitHub (opens new window)
Home
  • 计算机网络
  • 操作系统
  • 数据结构与算法
  • 设计模式
  • JavaSE
  • JVM
  • JUC
  • Netty
  • CPP
  • QT
  • UE
  • Go
  • Gin
  • Gorm
  • HTML
  • CSS
  • JavaScript
  • vue2
  • TypeScript
  • vue3
  • react
  • Spring
  • SpringMVC
  • Mybatis
  • SpringBoot
  • SpringSecurity
  • SpringCloud
  • Mysql
  • Redis
  • 消息中间件
  • RPC
  • 分布式锁
  • 分布式事务
  • 个人博客
  • 弹幕视频平台
  • API网关
  • 售票系统
  • 消息推送平台
  • SaaS短链接系统
  • Linux
  • Docker
  • Git
GitHub (opens new window)
  • HTTP

    • Get 与 Post
    • 状态码&字段
    • 输入URL到返回页面全过程
    • HTTP
    • HTTP演进
    • HTTPS
    • WebSocket
    • RPC
    • 粘包问题
    • Java中的网络请求
      • Java中的网络请求怎么表示?
    • 请求报文与响应报文
    • HTTP如何保存用户状态
    • 长连接与短连接
    • SSL TLS
  • TCP

  • UDP

  • IP

  • 网络安全

  • 计算机网络
  • HTTP
Nreal
2024-03-03
目录

Java中的网络请求

# Java中的网络请求怎么表示?

客户端请求由ServletRequest类型的request对象表示,在HTTP请求场景下,容器提供的请求对象的具体类型为HttpServletRequest;

HTTP的请求消息分为三部分:请求行、请求头、请求正文;

请求行方法:

// 获取请求行中的协议名和版本
public String getProtocol();

// 获取请求方式
public String getMethod();

// url中的额外路径信息
public String getPathInfo();

// url中的额外路径信息多对应的资源的真是路径
public String getPathTranslated();

// 获取请求URL所属的WEB应用程序路径,以/开头,表示整个web站点的根目录
public String getContextPath();

// 请求行中的参数
public String getQueryString();

// 获取请求行中的资源名,主机端口之后,参数之前的部分
public String getRequestURI();

// 获取Servlet所映射的路径
public String getServletPath();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

网络连接信息方法:

// 客户端的ip
public String getRemoteAddr();
//客户端的主机
public String getRemoteHost();
//客户端的端口
public int getRemotePort();
// 服务器接收当前请求的网络接口的ip对应的主机名
public String getLocalName();
// 服务器接收当前请求的网络接口的ip
public String getLocalAddr();
// 服务器接收当前请求的网络接口的端口
public int getLocalPort();
// 获取URL
public StringBuffer getRequestURL();
// 当前请求所指向的主机名
public String getServerName();
// 当前请求所连接的服务器端口号
public int getServerPort();
// 协议名
public String getScheme();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

请求头信息:

// 获取请求头
public long getDateHeader(String name);
public String getHeader(String name); 
public Enumeration<String> getHeaders(String name); 
public Enumeration<String> getHeaderNames();
public int getIntHeader(String name);
// 获取Content-Length头字段信息
public int getContentLength();
// 返回Content-Type头字段信息
public String getContentType();
// 返回请求消息的字符集编码,Content-Type头字段信息
public String getCharacterEncoding();


public String getAuthType();
    
public Cookie[] getCookies();

public String getRemoteUser();

public boolean isUserInRole(String role);

public java.security.Principal getUserPrincipal();

public String getRequestedSessionId();

public HttpSession getSession(boolean create);

public HttpSession getSession();

public String changeSessionId();

public boolean isRequestedSessionIdValid();

public boolean isRequestedSessionIdFromCookie();

public boolean isRequestedSessionIdFromURL();

public boolean isRequestedSessionIdFromUrl();

public boolean authenticate(HttpServletResponse response) 
  throws IOException,ServletException;

public void login(String username, String password) 
  throws ServletException;

public void logout() throws ServletException;

public Collection<Part> getParts() throws IOException, ServletException;

public Part getPart(String name) throws IOException, ServletException;

public <T extends HttpUpgradeHandler> T  upgrade(Class<T> handlerClass)
  throws IOException, ServletException;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

请求参数:

// 读取url地址后的参数或者POST请求中application/x-www-form-urlencoded编码的实体
// 可以对编码内容自动完成参数的分解、提取以及解码
public String getParameter(String name);
public Enumeration<String> getParameterNames();
public String[] getParameterValues(String name);
public Map<String, String[]> getParameterMap();

// 获取流对象
public ServletInputStream getInputStream() throws IOException; 
public BufferedReader getReader() throws IOException;
1
2
3
4
5
6
7
8
9
10
粘包问题
请求报文与响应报文

← 粘包问题 请求报文与响应报文→

Theme by Vdoing | Copyright © 2021-2024
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式