Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find out the size of html page

I am using the following code to open an input stream from a website url. I wanted to know if there is any way to find out the size of html source before I open the stream.

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());
}
like image 599
Xtr33mm Avatar asked Dec 13 '25 21:12

Xtr33mm


1 Answers

Try this:

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

Now length will contain the Content-Length of the requested resource in bytes. Note that this will not work for chunked transfers, in which case you will need to keep reading until you reach the end of the stream to determine its length.

like image 84
Candy Gumdrop Avatar answered Dec 15 '25 12:12

Candy Gumdrop



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!