adamine
adamine

Reputation: 143

httpUrlConnextion.getContentLength returning incorrect value

HttpURLConnection connection =(HttpURLConnection)blogFeedUrl.openConnection();
            connection.connect();
                int contentLength = connection.getContentLength();

So i have those 2 lines of code,when :

URL blogFeedUrl = new URL("http://blog.teamtreehouse.com/api/get_recent_summary/?count=2");

everything works fine and contentLength get an acceptable value. but when:

URL blogFeedUrl = new URL("http://raja12.esy.es/api/get_recent_summary/?count=2");

getContentLength is returning -1 and it doesn't work. The thing is that the two url are similar(you can check). That's so confusing! Thnank you !

Upvotes: 0

Views: 343

Answers (2)

ainlolcat
ainlolcat

Reputation: 554

Headers from each server differs. http://blog.teamtreehouse.com/api/get_recent_summary/?count=2 returns content-length but http://raja12.esy.es/api/get_recent_summary/?count=2 headers do not contains content-length header.

You can see headers if You press F12 in browser, go to Network tab and run request. You will see 2 operations - your get and favicon get. Check your get for headers.

Suppose You can change settings of server or just ignore content-length header if it less then 0

Upvotes: 1

Wai Yan
Wai Yan

Reputation: 582

From this Javadoc, the content length may be larger than Integer.MAX_VALUE (65535).

Can you try with getContentLengthLong() instead and see the results?

Upvotes: 0

Related Questions