找出 html 页的大小

Find out the size of html page

我正在使用以下代码从网站 url 打开输入流。我想知道在打开流之前是否有任何方法可以找出 html 源的大小。

try
{
    URL u = new URL(url);
    InputStreamReader isr = new InputStreamReader(u.openStream());
    BufferedReader reader = new BufferedReader(isr);
    String str;
    while ((str = reader.readLine()) != null) {
        webContent += str;
    }
    reader.close();
}
catch (IOException e)
{
    Console.print(e.getMessage());
}

试试这个:

URL u = new URL(url);
URLConnection conn = u.openConnection();
int length = conn.getContentLength();
InputStreamReader isr = new InputStreamReader(conn.getInputStream());
// ...

现在 length 将包含所请求资源的 Content-Length 字节。请注意,这不适用于 chunked 传输,在这种情况下,您需要继续阅读直到到达流的末尾以确定其长度。