简体   繁体   English

为什么java没有读取Content-length响应头?

[英]Why is java not reading the Content-length response header?

I have the following code to read the content-length response header of the resource pointed by the following url but this code doesn't read the content length and returns -1. 我有以下代码来读取由以下url指向的资源的内容长度响应头,但此代码不读取内容长度并返回-1。 I am wondering why. 我想知道为什么。

 try{


String song_url = "https://doc-0c-6o-docs.googleusercontent.com/docs/securesc/mtop3kcd4g9holc81thpce8mmck772ja/10to2su90o897bl390ojai9srph2fmvq/1407182400000/15635312626103908265/15635312626103908265/0B8CobC-HnuclMm5sUnVpOW13eEk?e=download&h=16653014193614665626&nonce=5beqd4pl58o30&user=15635312626103908265&hash=qbqjjhkrpco10b8ggu7ko5qa5c64th2m";


                    URL web = new URL(song_url);

                    con1 = web.openConnection();

                     total_size = con1.getContentLength();

                    ((HttpURLConnection)con1).disconnect();

                    return total_size;

                }
                catch(Exception e){

                    e.printStackTrace();
                }

                ((HttpURLConnection)con1).disconnect();

Now, the strange thing is if you just paste this url in the browser 现在,奇怪的是如果你只是将这个url粘贴到浏览器中

https://doc-0c-6o-docs.googleusercontent.com/docs/securesc/mtop3kcd4g9holc81thpce8mmck772ja/10to2su90o897bl390ojai9srph2fmvq/1407182400000/15635312626103908265/15635312626103908265/0B8CobC-HnuclMm5sUnVpOW13eEk?e=download&h=16653014193614665626&nonce=5beqd4pl58o30&user=15635312626103908265&hash=qbqjjhkrpco10b8ggu7ko5qa5c64th2m https://doc-0c-6o-docs.googleusercontent.com/docs/securesc/mtop3kcd4g9holc81thpce8mmck772ja/10to2su90o897bl390ojai9srph2fmvq/1407182400000/15635312626103908265/15635312626103908265/0B8CobC-HnuclMm5sUnVpOW13eEk?e=download&h=16653014193614665626&nonce=5beqd4pl58o30&user=15635312626103908265&hash=qbqjjhkrpco10b8ggu7ko5qa5c64th2m

and then under inspect element > network in google chrome then you will see that the browser reads the content length correctly. 然后在谷歌浏览器中检查元素>网络,然后您将看到浏览器正确读取内容长度。

content-length:79476777 内容长度:79476777

But why is the java code failing to read the content-length? 但是为什么java代码无法读取内容长度? Is this a problem with the library i am using? 这是我正在使用的库的问题吗? Thanks for the help! 谢谢您的帮助!

That would be because the server isn't sending a Content-length header. 那是因为服务器没有发送Content-length头。 There are a least two other ways it can convey the length: 它至少还有两种传达长度的方式:

  1. By closing the socket after sending the request body. 通过在发送请求主体后关闭套接字。 In HTTP 1.1 I think this would have to be accompanied by a 'Connection: close ' header. 在HTTP 1.1中我认为这必须伴随着'Connection:close'标题。 In this case the content length is the length of the body, and you have to read it all to measure it. 在这种情况下,内容长度是正文的长度,您必须全部阅读以测量它。

  2. By using chunked transfer encoding. 通过使用分块传输编码。 In this case the content length is the sum of the lengths of the chunks. 在这种情况下,内容长度是块长度的总和。 Again you would have to read it all to determine the length. 你必须再读一遍以确定长度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM