javaandroidjsonurlhttpurlconnection

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 !


Solution

  • 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