为什么Content-Length返回-1?

时间:2015-08-02 15:51:53

标签: java

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

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

1 个答案:

答案 0 :(得分:1)

服务器可能没有为该页面设置该标头,在该语句执行时可能没有完成加载和/或您可能遇到连接问题。

请参阅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()并改为计算。

相关问题