Java HttpURLConnection:内容长度计算
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6354928/
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
Java HttpURLConnection: Content Length computation
提问by f4lco
I'm currently developing a library for the bitbucket issues RESTful API. I made good progress and now I'm going to tackle the section Updating an Issuewhich demands an HTTP PUT Request.
我目前正在为 bitbucket 问题 RESTful API 开发一个库。我取得了很好的进展,现在我将处理更新需要 HTTP PUT 请求的问题部分。
Now I'm stuck because of the HTTP Error Code 411 Length Required
. After a bit of googling, I found the following code example:
现在我因为 HTTP 错误代码而被卡住了411 Length Required
。经过一番谷歌搜索,我找到了以下代码示例:
// CORRECT: get a UTF-8 encoded byte array from the response
// String and set the content-length to the length of the
// resulting byte array.
String response = [insert XML with UTF-8 characters here];
byte[] responseBytes;
try {
responseBytes = response.getBytes("UTF-8");
}
catch ( UnsupportedEncodingException e ) {
System.err.print("My computer hates UTF-8");
}
this.contentLength_ = responseBytes.length;
Now my question: What is exactly measured?
现在我的问题是:什么是准确测量的?
- the query string
- the urlencoded query string
- only the values of the parameters...??
- 查询字符串
- urlencoded 查询字符串
- 只有参数的值...??
And is connection.setRequestProperty("Content-Length", String.valueOf(<mycomputedInt>));
an appriopate way of setting the content length attribute?
并且是connection.setRequestProperty("Content-Length", String.valueOf(<mycomputedInt>));
设置内容长度属性的适当方法吗?
Examples appreciated. Thanks in advance.
示例赞赏。提前致谢。
edit:
编辑:
For instance, you could explain computation with the following curl example from the bitbucket wiki entry:
例如,您可以使用 bitbucket wiki 条目中的以下 curl 示例来解释计算:
curl -X PUT -d "content=Updated%20Content" \
https://api.bitbucket.org/1.0/repositories/sarahmaddox/sarahmaddox/issues/1/
采纳答案by webstrap
Your are doing the request, right. The content-length is the number of bytes of your request body. In your case
您正在执行请求,对。content-length 是请求正文的字节数。在你的情况下
int content-length = "content=Updated%20Content".getBytes("UTF-8").length;
What is exactly measured?
准确测量的是什么?
the url encoded query string (when in the request/entity body)
url 编码的查询字符串(在请求/实体正文中时)
回答by johnstok
HTTP spec on 411:
411上的 HTTP 规范:
The server refuses to accept the request without a defined Content- Length. The client MAY repeat the request if it adds a valid Content-Length header field containing the length of the message-body in the request message.
服务器拒绝接受没有定义内容长度的请求。如果客户端在请求消息中添加了包含消息正文长度的有效 Content-Length 头字段,则客户端可以重复该请求。
HTTP spec on the Content-Length header:
Content-Length 标头上的HTTP 规范:
The Content-Length entity-header field indicates the size of the entity-body, in decimal number of OCTETs
Content-Length entity-header字段指示实体主体的大小,以十进制OCTET数表示
HTTP spec on HTTP entity length:
HTTP实体长度的HTTP 规范:
entity-body := Content-Encoding( Content-Type( data ) )
The entity-length of a message is the length of the message-body before any transfer-codings have been applied.
消息的实体长度是应用任何传输编码之前消息体的长度。
To summarise if you want to send an un-compressed UTF-8 string you would determine the bytes to send as:
总而言之,如果您想发送未压缩的 UTF-8 字符串,您将确定要发送的字节数:
Identity( UTF-8( "content=Updated%20Content" ) )
The Content-Length is set to the number of bytes output.
Content-Length 设置为输出的字节数。
If you are sending UTF-8 data I would also strongly suggest you set a Content-Typeheader.
如果您要发送 UTF-8 数据,我还强烈建议您设置一个Content-Type标头。