Shoaib
Shoaib

Reputation: 185

HttpURLConnection getting content length from server

I using the following connection to resume my download:

connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setReadTimeout(7000);
connection.setRequestProperty("Range", "bytes=" + localFileSize + "-");

long fileLengthOnServer = connection.getContentLength();
connection.connect();

The value of the fileLengthOnServer is -1. Can anyone explain that in which conditions fileLengthOnServer becomes -1. I guess, but not sure that the localFileSize is the same as the one on the server. Please help!

Upvotes: 0

Views: 571

Answers (1)

Trash Can
Trash Can

Reputation: 6814

You need to call this long fileLengthOnServer = connection.getContentLength(); after connection.connect(). You can't get the response headers without connecting to the server first

Upvotes: 2

Related Questions