<i id='0XEBb'><tr id='0XEBb'><dt id='0XEBb'><q id='0XEBb'><span id='0XEBb'><b id='0XEBb'><form id='0XEBb'><ins id='0XEBb'></ins><ul id='0XEBb'></ul><sub id='0XEBb'></sub></form><legend id='0XEBb'></legend><bdo id='0XEBb'><pre id='0XEBb'><center id='0XEBb'></center></pre></bdo></b><th id='0XEBb'></th></span></q></dt></tr></i><div id='0XEBb'><tfoot id='0XEBb'></tfoot><dl id='0XEBb'><fieldset id='0XEBb'></fieldset></dl></div>

<small id='0XEBb'></small><noframes id='0XEBb'>

    <tfoot id='0XEBb'></tfoot>

    1. <legend id='0XEBb'><style id='0XEBb'><dir id='0XEBb'><q id='0XEBb'></q></dir></style></legend>
      • <bdo id='0XEBb'></bdo><ul id='0XEBb'></ul>

    2. 为什么 Content-Length HTTP 标头字段使用的值与 Java 代码中给出的值不同?

      Why does Content-Length HTTP header field use a value other than the one given in Java code?(为什么 Content-Length HTTP 标头字段使用的值与 Java 代码中给出的值不同?)
      <tfoot id='UpKaU'></tfoot>
      <legend id='UpKaU'><style id='UpKaU'><dir id='UpKaU'><q id='UpKaU'></q></dir></style></legend>

          <bdo id='UpKaU'></bdo><ul id='UpKaU'></ul>
              <i id='UpKaU'><tr id='UpKaU'><dt id='UpKaU'><q id='UpKaU'><span id='UpKaU'><b id='UpKaU'><form id='UpKaU'><ins id='UpKaU'></ins><ul id='UpKaU'></ul><sub id='UpKaU'></sub></form><legend id='UpKaU'></legend><bdo id='UpKaU'><pre id='UpKaU'><center id='UpKaU'></center></pre></bdo></b><th id='UpKaU'></th></span></q></dt></tr></i><div id='UpKaU'><tfoot id='UpKaU'></tfoot><dl id='UpKaU'><fieldset id='UpKaU'></fieldset></dl></div>
                  <tbody id='UpKaU'></tbody>
              • <small id='UpKaU'></small><noframes id='UpKaU'>

                本文介绍了为什么 Content-Length HTTP 标头字段使用的值与 Java 代码中给出的值不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一段 Java 代码将字节数组传输到 HTTP 服务器:

                I have a piece of Java code to transfer a byte array to HTTP server:

                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setDoInput(true);
                connection.setDoOutput(true);
                connection.setUseCaches(false);
                connection.setRequestMethod("POST");
                connection.setRequestProperty("Connection", "Keep-Alive");
                connection.setRequestProperty("Content-Type", "multipart/form-data; boundary="
                    + myBoundary);
                connection.setRequestProperty("Content-Length", 1024);
                

                我使用此代码传输大小大于 1024 的字节数组.它运行良好.但是实际的 HTTP 消息(由 Wireshark 捕获)显示 Content-Length 的值是实际大小而不是1024. 为什么?

                I used this code to transfer a byte array whose size is greater than 1024. It worked well. But the actual HTTP message (captured by Wireshark) shows that the value of Content-Length is the actual size instead of 1024. Why?

                我在 HTTP 规范 中进行了搜索,但没有找到任何提示.我没有使用任何传输编码或传输编码.

                I searched in HTTP spec but found no hint. I did not use any Transfer-Encoding or Transfer-coding.

                推荐答案

                我猜 HttpURLConnection 将简单地用正确的值覆盖 Content-Length 标头,因为它知道撒谎是不好的 ;-)

                I'd guess that the HttpURLConnection will simply override the Content-Length header with the correct value, since it knows that lying about it is no good ;-)

                事实上:在 sun.net.www.protocol.HttpURLConnection 如果合适,Content-Length 会被设置.这发生在 用户指定的标头设置之后,因此该值将被覆盖.

                And indeed: at the lines 535-550 of sun.net.www.protocol.HttpURLConnection the Content-Length is set if appropriate. This happens after the user-specified headers are set, so that value will be overwritten.

                这是正确的:如果您传输的数据量与声称的数量不匹配,那么您只会混淆另一端.

                And it's right about that: if the amount of data you transfer does not match the claimed amount, then you'll only confuse the other end.

                检查 sun.net.www.protocol.http.HttpURLConnection的来源好像有一个headers列表是限制的,调用setRequestProperty.Content-Length 在该列表中.不幸的是,这似乎没有记录(至少我找不到任何关于此的文档,只有 此处讨论相关问题).

                Checking the source of sun.net.www.protocol.http.HttpURLConnection it seems that there is a list of headers that are restricted and will silently be ignored when calling setRequestProperty. Content-Length is among that list. Unfortunately this seems to be undocumented (at least I couldn't find any documentation on this, only a discussion of a related problem here).

                搜索 引入此功能"的 ChangeSet 似乎引入此更改是对安全漏洞 CVE-2010-3541 和 CVE-2010-3573 (有关此主题的 Redhat 错误).

                Googling for the Bug IDs (?) mentioned in the ChangeSet that introduced this "functionality" it seems that this change was introduces as a reaction to the security vulnerabilities CVE-2010-3541 and CVE-2010-3573 (Redhat bug on this topic).

                可以通过在 JVM 启动时将系统属性 sun.net.http.allowRestrictedHeaders 设置为 true 手动禁用限制.

                The restriction can manually be disabled by setting the System property sun.net.http.allowRestrictedHeaders to true on JVM startup.

                这篇关于为什么 Content-Length HTTP 标头字段使用的值与 Java 代码中给出的值不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Compiling C++ for the JVM(为 JVM 编译 C++)
                Compile to java bytecode (without using Java)(编译成java字节码(不使用Java))
                How to drive C#, C++ or Java compiler to compute 1+2+3+...+1000 at compile time?(如何在编译时驱动 C#、C++ 或 Java 编译器计算 1+2+3+...+1000?)
                Java ClassLoader: load same class twice(Java ClassLoader:两次加载相同的类)
                How to debug .class files in ECLIPSE?(如何在 ECLIPSE 中调试 .class 文件?)
                Java quot;The blank final field may not have been initializedquot; Anonymous Interface vs Lambda Expression(Java“可能尚未初始化空白的最终字段匿名接口与 Lambda 表达式)
                <tfoot id='F20YM'></tfoot>
                  <tbody id='F20YM'></tbody>
                <i id='F20YM'><tr id='F20YM'><dt id='F20YM'><q id='F20YM'><span id='F20YM'><b id='F20YM'><form id='F20YM'><ins id='F20YM'></ins><ul id='F20YM'></ul><sub id='F20YM'></sub></form><legend id='F20YM'></legend><bdo id='F20YM'><pre id='F20YM'><center id='F20YM'></center></pre></bdo></b><th id='F20YM'></th></span></q></dt></tr></i><div id='F20YM'><tfoot id='F20YM'></tfoot><dl id='F20YM'><fieldset id='F20YM'></fieldset></dl></div>

                  <bdo id='F20YM'></bdo><ul id='F20YM'></ul>
                    <legend id='F20YM'><style id='F20YM'><dir id='F20YM'><q id='F20YM'></q></dir></style></legend>
                        • <small id='F20YM'></small><noframes id='F20YM'>