我创建了一个普通的 HttpURLConnection 对象。例如,我设置了 3 个请求 header 。
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");
我查看设置的请求 header 。
System.out.println(conn.getRequestProperties());
标题“Content-Length”从不包含在结果中。
{User-Agent=[some_agent], Content-Type=[some_content-type]}
如果我调用 conn.getRequestProperty("Content-Length")
,我也会得到 null
。
代码工作正常,但为什么“Content-Length”消失了?
最佳答案
不能设置内容长度;它是自动完成的。但是,当内容长度事先已知时,您可以使用 setFixedLengthStreamingMode()
。
关于Java - HttpURLConnection - 请求 header 中未显示内容长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18674164/