我目前正在为 bitbucket issues RESTful API 开发一个库。我取得了很好的进展,现在我要处理 Updating an Issue 部分这需要一个 HTTP PUT 请求。
现在我因为 HTTP 错误代码 411 Length Required 而卡住了.经过一番谷歌搜索后,我发现了以下 code example :
// 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;
现在我的问题是:究竟测量了什么?
并且是connection.setRequestProperty("Content-Length", String.valueOf(<mycomputedInt>));设置内容长度属性的合适方法?
示例表示赞赏。提前致谢。
编辑:
例如,您可以使用 bitbucket wiki 条目中的以下 curl 示例来解释计算:
curl -X PUT -d "content=Updated%20Content" \
https://api.bitbucket.org/1.0/repositories/sarahmaddox/sarahmaddox/issues/1/
最佳答案
您正在执行请求,对吧。内容长度是请求正文的字节数。在你的情况下
int content-length = "content=Updated%20Content".getBytes("UTF-8").length;
具体测量的是什么?
url 编码的查询字符串(在请求/实体主体中时)
关于Java HttpURLConnection : Content Length computation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6354928/