Java HttpURLConnection:内容长度计算 [英] Java HttpURLConnection: Content Length computation

查看:497
本文介绍了Java HttpURLConnection:内容长度计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为bitbucket开发一个用于发布RESTful API的库。我取得了很好的进展,现在我要解决更新问题 a>需要HTTP PUT请求。

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 Issue which demands an HTTP PUT Request.

现在我因为HTTP错误代码而被卡住了 411长度要求。经过一段谷歌搜索后,我发现以下代码示例

Now I'm stuck because of the HTTP Error Code 411 Length Required. After a bit of googling, I found the following 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;

现在我的问题:究竟测量了什么?

Now my question: What is exactly measured?


  • 查询字符串

  • urlencoded查询字符串

  • 只有参数的值... ??

并且是 connection.setRequestProperty(Content-Length,String.valueOf(< mycomputedInt>) ); 一种设置内容长度属性的适当方式?

And is connection.setRequestProperty("Content-Length", String.valueOf(<mycomputedInt>)); an appriopate way of setting the content length attribute?

示例赞赏。提前致谢。

编辑:

例如,你可以用bitbucket wiki条目中的以下curl示例解释计算:

For instance, you could explain computation with the following curl example from the bitbucket wiki entry:

curl -X PUT -d "content=Updated%20Content" \
https://api.bitbucket.org/1.0/repositories/sarahmaddox/sarahmaddox/issues/1/


推荐答案

你正在做请求,对。 content-length是请求正文的字节数。在你的情况下

Your are doing the request, right. The content-length is the number of bytes of your request body. In your case

int content-length = "content=Updated%20Content".getBytes("UTF-8").length;

测量的是什么?

url编码的查询字符串(在请求/实体主体中时)

the url encoded query string (when in the request/entity body)

这篇关于Java HttpURLConnection:内容长度计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆