gpt4 book ai didi

java - 不透明 URI 中是否允许查询参数?

转载 作者:行者123 更新时间:2023-11-30 02:20:58 26 4
gpt4 key购买 nike

Java 的 URI类定义不透明 URI 如下(强调我的)。

A URI is opaque if, and only if, it is absolute and its scheme-specific part does not begin with a slash character ('/'). An opaque URI has a scheme, a scheme-specific part, and possibly a fragment; all other components are undefined.

正如文档所述,不透明的 URI 对于查询参数返回 null

URI uri = URI.create("stackoverflow:foo?key=value#frag");
uri.isOpaque() == true
uri.getScheme() == stackoverflow
uri.getSchemeSpecificPart() == foo?key=value
uri.getQuery() == null
uri.getFragment() == frag

此行为是否特定于 Java 的 URI 实现,或者 URI 规范是否不允许在不透明 URI 中使用查询参数?

最佳答案

Java 的 URI类(class)是 documented符合RFC 2396RFC 2732 :

Aside from some minor deviations noted below, an instance of this class represents a URI reference as defined by RFC 2396: Uniform Resource Identifiers (URI): Generic Syntax, amended by RFC 2732: Format for Literal IPv6 Addresses in URLs.

第 3 节:URI 语法组件并没有明确禁止不透明 URI 中使用查询组件,而只是不这样做为它们定义语法。根据定义,不透明的 URI 需要特定于方案的知识来了解是否存在类似于查询组件的东西以及如何准确地解析它。拥有它是完全合法的,但如果没有这种特殊知识,就无法在一般意义上(根据本 RFC)支持它。相关的 RFC 文本是:

The URI syntax is dependent upon the scheme. In general, absoluteURI are written as follows:

<scheme>:<scheme-specific-part>

An absolute URI contains the name of the scheme being used (<scheme>)followed by a colon (":") and then a string (the <scheme-specific-part>)whose interpretation depends on the scheme.

只有通用语法提供了 RFC 定义的查询组件,并且该语法需要至少一个斜杠 ( / ) 紧跟在方案后的第一个冒号 ( : ) 后面。

The URI syntax does not require that the scheme-specific-part haveany general structure or set of semantics which is common among allURI. However, a subset of URI do share a common syntax forrepresenting hierarchical relationships within the namespace. This"generic URI" syntax consists of a sequence of four main components:

<scheme>://<authority><path>?<query>

each of which, except <scheme>, may be absent from a particular URI.For example, some URI schemes do not allow an <authority> component,and others do not use a <query> component.

absoluteURI   = scheme ":" ( hier_part | opaque_part )

...

hier_part = ( net_path | abs_path ) [ "?" query ]
net_path = "//" authority [ abs_path ]>
abs_path = "/" path_segments

上述两个 RFC 均已 obsoleted by RFC 3986 ,但是对于 backwards-compatibility Java 现有的 API 和行为的原因是 unlikely to be changed .

URI 实现与较新的 RFC 的不同之处不仅在于它如何定义 URI 的有效组件,而且还在于行为。例如,请参见:

快速搜索至少找到一个开源 library尝试提供与 RFC 3986 兼容的实现,但之前的链接既不是认可也不是推荐。与现有的兼容性java.net.URI基于 -的 API 可能会受到限制。

关于java - 不透明 URI 中是否允许查询参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46833320/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com