[英]Java - HttpURLConnection - Content-Length isn't shown in request header
I create a normal HttpURLConnection-object. 我创建了一个普通的HttpURLConnection对象。 I set for example 3 request headers.
我设置了3个请求标头。
HttpURLConnection conn = (HttpURLConnection) someURL.openConnection();
conn.setDoInput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("User-Agent", "some_agent");
conn.setRequestProperty("Content-Type", "some_content-type");
conn.setRequestProperty("Content-Length", "some_content-length");
I look at setted request headers. 我查看设置的请求标头。
System.out.println(conn.getRequestProperties());
The header "Content-Length" is never contained in the result. 标题“Content-Length”永远不会包含在结果中。
{User-Agent=[some_agent], Content-Type=[some_content-type]}
I am getting also null
if I call conn.getRequestProperty("Content-Length")
. 如果我调用
conn.getRequestProperty("Content-Length")
我conn.getRequestProperty("Content-Length")
null
。
The code works fine but why does "Content-Length" disappear? 代码工作正常但为什么“内容长度”消失了?
You can't set the content length; 您无法设置内容长度; it's done automatically.
它是自动完成的。 You can, however, use
setFixedLengthStreamingMode()
, when the content length is known in advance. 但是,如果事先知道内容长度,则可以使用
setFixedLengthStreamingMode()
。
You need to set up System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
您需要设置
System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
More details about it(similar issue): https://community.oracle.com/thread/1999694?tstart=0 关于它的更多细节(类似问题): https : //community.oracle.com/thread/1999694?tstart = 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.