private static HttpURLConnection openURLConnection ( URL url ) throws IOException { // This method should be moved to a utility class in BioJava 5.0 final int timeout = 5000 ; final String useragent = "BioJava" ; HttpURLConnection conn = ( HttpURLConnection ) url . openConnection ( ) ; conn . setRequestProperty ( "User-Agent" , useragent ) ; conn . setInstanceFollowRedirects ( true ) ; conn . setConnectTimeout ( timeout ) ; conn . setReadTimeout ( timeout ) ; int status = conn . getResponseCode ( ) ; while ( status == HttpURLConnection . HTTP_MOVED_TEMP || status == HttpURLConnection . HTTP_MOVED_PERM || status == HttpURLConnection . HTTP_SEE_OTHER ) { // Redirect! String newUrl = conn . getHeaderField ( "Location" ) ; if ( newUrl . equals ( url . toString ( ) ) ) { throw new IOException ( "Cyclic redirect detected at " + newUrl ) ; } // Preserve cookies String cookies = conn . getHeaderField ( "Set-Cookie" ) ; // open the new connection again url = new URL ( newUrl ) ; conn . disconnect ( ) ; conn = ( HttpURLConnection ) url . openConnection ( ) ; if ( cookies != null ) { conn . setRequestProperty ( "Cookie" , cookies ) ; } conn . addRequestProperty ( "User-Agent" , useragent ) ; conn . setInstanceFollowRedirects ( true ) ; conn . setConnectTimeout ( timeout ) ; conn . setReadTimeout ( timeout ) ; conn . connect ( ) ; status = conn . getResponseCode ( ) ; logger . info ( "Redirecting from {} to {}" , url , newUrl ) ; } conn . connect ( ) ; return conn ; }
Open a URL connection .