爪哇,HttpURLConnection的和设置内容长度 [英] Java, HttpURLConnection and setting the content length

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

问题描述

我设置了​​内容的长度在我的HttpURLConnection,对于PUT。

I'm setting the length of the content in my HttpURLConnection, for a PUT.

urlConnection.setRequestProperty("Content-Length", "" + responseJSONArray.toString(2).getBytes("UTF8").length);

字节的实际数量是74。然而,当我查询的URLConnection 的内容长度我回来 1 。这是为什么?为什么是长度不相等(因为我设置)?

The actual number of bytes is 74. However, when I query the content length of urlConnection I'm returned -1. Why is that? And why are lengths not equal (given that I set this)?

我必须设置内容长度,因为我收到来自服务器的 411 响应。

I must set the content-length because I'm receiving a 411 response from the server.

(同样,在我见过的第二个参数太阳例子调用setRequestProperty 的类型是 INT ,而不是字符串,这似乎很奇怪。)

(Also, in the Sun examples I've seen the second argument of setRequestProperty is of type int and not String, which seems odd.)

推荐答案

您应该不设置这个头吧。使用 setFixedLengthStreamingMode() setChunkedTransferMode()

You shouldn't set this header yourself. Use setFixedLengthStreamingMode() or setChunkedTransferMode().

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

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