java中的setRequestProperty

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25005883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 23:25:46  来源:igfitidea点击:

setRequestProperty in java

javahttpurlconnectionurlconnection

提问by John

The documentation heresays that setRequestPropertysets the general request property.

这里的文档说setRequestProperty设置一般请求属性。

So suppose I have the following code snippet showing the usage of setRequestPropertyas follows:

所以假设我有以下代码片段显示setRequestProperty如下用法:

        URL url = new URL(requestUrl);
        URLConnection urlConn = url.openConnection();
        urlConn.setRequestProperty("accept", "application/json");
        urlConn.setRequestProperty("datetime", dateTimeString);
        urlConn.setRequestProperty("authorization", authorization);
        urlConn.setUseCaches(false);
        urlConn.setDoInput(true); // Triggers POST

Q1: Does acceptneed to have an uppercase Ahere? Similarly, for authorization, does it need to have an uppercase Aas well? The reason I am asking this is because I have seen many post where people have been using authorizationas say for example conn.setRequestProperty ("Authorization", "Basic " + encodedString);. This is shown here.

Q1:这里accept需要大写A吗?同样,对于authorization,它是否也需要大写A?我问这个的原因是因为我看过很多帖子,人们一直在使用authorizationas say conn.setRequestProperty ("Authorization", "Basic " + encodedString);。这在此处显示。

Q2. Since I have lot of setRequestPropertyproperty defined above, does this means that, a URL contains all of these properties? Are there any other properties that exists besides the one I have used above?

Q2。由于我在setRequestProperty上面定义了很多属性,这是否意味着 URL 包含所有这些属性?除了我上面使用的属性之外,还有其他属性吗?

采纳答案by sendon1982

A1: HTTP header fields, which include general-header (section 4.5), request-header (section 5.3), response-header (section 6.2), and entity-header (section 7.1) fields, follow the same generic format as that given in Section 3.1 of RFC 822 [9]. Each header field consists of a name followed by a colon (":") and the field value. Field names are case-insensitive.

A1:HTTP 头字段,包括通用头(4.5 节)、请求头(5.3 节)、响应头(6.2 节)和实体头(7.1 节)字段,遵循与给定相同的通用格式在 RFC 822 [9] 的第 3.1 节中。每个头字段由一个名称后跟一个冒号(“:”)和字段值组成。字段名称不区分大小写

A2: These variables will be in the HTTP request header. If you want URL contains these parameters, you need to append them on the URL:

A2:这些变量将在 HTTP 请求标头中。如果您希望 URL 包含这些参数,则需要将它们附加到 URL 上:

http://www.google.com?bar=far&boo=foo