You can improve that with 
final HttpURLConnection conn = (HttpURLConnection)ub.toURI().openConnection();