gpt4 book ai didi

java - 为什么 Content-Length 返回 -1?

转载 作者:行者123 更新时间:2023-12-01 11:16:27 24 4
gpt4 key购买 nike

HttpURLConnection conn = (HttpURLConnection) new URL("http://cuonline.ac.in/ranking/cap_admitted.php?clg=231").openConnection();
System.out.println(conn.getContentLength());

为什么它返回的内容长度为 -1 ?我做错了什么吗?

最佳答案

服务器可能未为该页面设置该 header ,该语句执行时可能尚未完成加载和/或您可能存在连接问题。

参见http://developer.android.com/reference/java/net/URLConnection.html#getContentLength%28%29

至于您的具体网址,它似乎尚未设置

SERVER RESPONSE: HTTP/1.1 200 OK
Date:
Sun, 02 Aug 2015 16:02:49 GMT
Server:
Apache/2.2.14 (Ubuntu)
X-Powered-By:
PHP/5.3.2-1ubuntu4.29
Vary:
Accept-Encoding
Connection:
close
Content-Type:
text/html

http://tools.seobook.com/server-header-checker/?page=single&url=http%3A%2F%2Fcuonline.ac.in%2Franking%2Fcap_admitted.php%3Fclg%3D231&useragent=1&typeProtocol=11

您可能必须读取内容 conn.getInputStream() 并对其进行计数。

关于java - 为什么 Content-Length 返回 -1?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31773658/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com