public static void sendGetImageRequest( String urlString, String authentication, String fileName ) throws Exception { HttpGet method = new HttpGet( urlString ); HttpClientContext context = HttpClientUtil.createPreemptiveBasicAuthentication( host, port, user, password ); //adding authorization token if ( authentication != null ) { method.addHeader( new BasicHeader( "Authorization", authentication ) ); } //executing method HttpClient client = HttpClientManager.getInstance().createDefaultClient(); HttpResponse httpResponse = context != null ? client.execute( method, context ) : client.execute( method ); int code = httpResponse.getStatusLine().getStatusCode(); byte[] response = HttpClientUtil.responseToByteArray( httpResponse ); method.releaseConnection(); if ( code >= HttpStatus.SC_BAD_REQUEST ) { System.out.println( "Error occurred during getting transformation image." ); } System.out.println( "Image was stored to " + fileName ); FileOutputStream fos = null; try { fos = new FileOutputStream( fileName ); fos.write( response ); fos.flush(); } finally { fos.close(); } } }
public static boolean sendGetImageRequest( String urlString, String authentication, String fileName ) throws Exception { HttpGet method = new HttpGet( urlString ); HttpClientContext context = HttpClientUtil.createPreemptiveBasicAuthentication( host, port, user, password ); //adding authorization token if ( authentication != null ) { method.addHeader( new BasicHeader( "Authorization", authentication ) ); } //executing method HttpClient client = HttpClientManager.getInstance().createDefaultClient(); HttpResponse httpResponse = context != null ? client.execute( method, context ) : client.execute( method ); int code = httpResponse.getStatusLine().getStatusCode(); byte[] response = HttpClientUtil.responseToByteArray( httpResponse ); method.releaseConnection(); if ( code >= HttpStatus.SC_BAD_REQUEST ) { System.out.println( "Error occurred during getting job image." ); return false; } FileOutputStream fos = null; try { fos = new FileOutputStream( fileName ); fos.write( response ); fos.flush(); } finally { fos.close(); } return true; }