JDK-6397569 : URLConnection.getContentLength() returns -1 for some websites
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 5.0u6
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2006-03-13
  • Updated: 2011-02-16
  • Resolved: 2006-03-14
Description
OPERATING SYSTEM(S): Windows

FULL JDK VERSION(S): All

DESCRIPTION:
Enclosed is a simple testcase which can be used to reproduce the reported behaviour.

Steps :
- Compile and run testcase using the same JVM.

Exceptions displayed on console:
GWMon_JCW Start...
http://www.yahoo.co.kr/index.html
Type= text/html; charset=euc-kr
urlcon.getResponseCode() = 200
Length = -1
java.lang.NegativeArraySizeException:
        at GWMon_JCW_test.getStatus(GWMon_JCW_test.java:87)
        at GWMon_JCW_test.run(GWMon_JCW_test.java:123)

TESTCASE:

/**
 * @author Uno System
 *
 * JCW
 */
import java.io.* ;
import java.util.* ;
import java.net.* ;
import java.text.* ;
import java.net.URLConnection ;

public class GWMon_JCW_test extends Thread
{
	protected static SimpleDateFormat m_DF = new SimpleDateFormat( "yyyy/MM/dd HH:mm:ss", java.util.Locale.ENGLISH ) ;

	String m_CfgName = "" ;

	String m_PathStt = "" ;
	String m_PathLog = "" ;
	String m_PathErr = "" ;

	int    m_WatchTime = 3 ;
	String m_URL = "" ;


	/**
	 *
	 * GWMon_JCW Entry Point
	 */
	public static void main(String[] args)
	{
		try
		{
			GWMon_JCW_test monitor = new GWMon_JCW_test( "" ) ;
			monitor.start() ;
		}
		catch( Exception e )
		{
			e.printStackTrace() ;
			System.exit( 0 ) ;
		}
	}
	
	/**
	 * GWMon_JCW 
	 * @param String config file name
	 * @exception Exception
	 */
	public GWMon_JCW_test( String cfg ) throws Exception
	{
		m_CfgName = cfg ;

	    //m_URL = "http://192.168.60.5/cm/login.html" ;
		m_URL = "http://www.yahoo.co.kr/index.html" ;
                
	}

	protected String getStatus()
	{
		URL url = null ;
		DataInputStream    dis    = null ;
		HttpURLConnection  urlcon = null ;
		String sRet = "" ;
		
		try
		{
			System.out.println( m_URL ) ;

			url = new URL( m_URL ) ;
			urlcon = (HttpURLConnection) url.openConnection() ;

			System.out.println("Type= " + urlcon.getContentType());
			System.out.println( "urlcon.getResponseCode() = " + urlcon.getResponseCode() ) ;

			if( urlcon.getResponseCode() != 200 )
			{
				return "" ;
			}
                        
			System.out.println( "Length = " + urlcon.getContentLength() ) ;

 			// Method used in existing
 			// Content Length   Read
 			 
 			int length = urlcon.getContentLength() ;
			byte[] b = new byte[length] ;

			dis = new DataInputStream( urlcon.getInputStream()) ;

			dis.readFully( b ) ;
			sRet = new String( b, "EUC-KR" ) ;

			System.out.println( "sRet = " + sRet ) ;

	   }catch( Exception e )
		{
			e.printStackTrace() ;
			sRet = "" ;
		}
		finally
		{
			try
			{
				dis.close() ;
			}
			catch( Exception e )
			{
			}
		}
 		return sRet ;
	}
	
	/**
	 * JCW 
	 */
	public void run()
	{
		
		System.out.println("GWMon_JCW Start..." ) ;
		while( true )
		{
			String stt = getStatus() ;

			try
			{
				Thread.sleep( m_WatchTime * 1000 ) ;
			}
			catch( Exception e ){}
		}
	}
}

Comments
EVALUATION A value of -1 for getContentLength is perfectly valid, it means the content-length header hasn't been provided by the server. See javadoc: /** * Returns the value of the <code>content-length</code> header field. * * @return the content length of the resource that this connection's URL * references, or <code>-1</code> if the content length is * not known. */
14-03-2006