private HttpURLConnection getURLConnection ( String str ) throws MalformedURLException { try { if ( isHttps ) { /* when communicating with the server which has unsigned or invalid * certificate (https), SSLException or IOException is thrown. * the following line is a hack to avoid that */ Security . addProvider ( new com . sun . net . ssl . internal . ssl . Provider ( ) ) ; System . setProperty ( "java.protocol.handler.pkgs" , "com.sun.net.ssl.internal.www.protocol" ) ; if ( isProxy ) { System . setProperty ( "https.proxyHost" , proxyHost ) ; System . setProperty ( "https.proxyPort" , proxyPort + "" ) ; } } else { if ( isProxy ) { System . setProperty ( "http.proxyHost" , proxyHost ) ; System . setProperty ( "http.proxyPort" , proxyPort + "" ) ; } } URL url = new URL ( str ) ; HttpURLConnection uc = ( HttpURLConnection ) url . openConnection ( ) ; // if this header has not been set by a request set the user agent. if ( headers == null || ( headers != null && headers . get ( "user-agent" ) == null ) ) { // set user agent to mimic a common browser String ua = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)" ; uc . setRequestProperty ( "user-agent" , ua ) ; } uc . setInstanceFollowRedirects ( false ) ; return uc ; } catch ( MalformedURLException me ) { throw new MalformedURLException ( str + " is not a valid URL" ) ; } catch ( Exception e ) { throw new RuntimeException ( "Unknown error creating UrlConnection: " + e ) ; } }
private method to get the URLConnection