List of usage examples for java.net HttpURLConnection setInstanceFollowRedirects
public void setInstanceFollowRedirects(boolean followRedirects)
From source file:org.apache.hadoop.mapred.TestJobRetire.java
private JobID validateJobRetire(JobConf jobConf, Path inDir, Path outDir, JobTracker jobtracker) throws IOException { RunningJob rj = UtilsForTests.runJob(jobConf, inDir, outDir, 0, 0); rj.waitForCompletion();//from ww w. j a v a 2 s . c om assertTrue(rj.isSuccessful()); JobID id = rj.getID(); //wait for job to get retired waitTillRetire(id, jobtracker); RetireJobInfo retired = jobtracker.retireJobs.get(id); assertTrue("History url not set", retired.getHistoryFile() != null && retired.getHistoryFile().length() > 0); assertNotNull("Job is not in cache", jobtracker.getJobStatus(id)); // get the job conf filename String name = jobtracker.getLocalJobFilePath(id); File file = new File(name); assertFalse("JobConf file not deleted", file.exists()); //test redirection URL jobUrl = new URL(rj.getTrackingURL()); HttpURLConnection conn = (HttpURLConnection) jobUrl.openConnection(); conn.setInstanceFollowRedirects(false); conn.connect(); assertEquals(HttpURLConnection.HTTP_MOVED_TEMP, conn.getResponseCode()); conn.disconnect(); URL redirectedUrl = new URL(conn.getHeaderField("Location")); conn = (HttpURLConnection) redirectedUrl.openConnection(); conn.connect(); assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode()); conn.disconnect(); return id; }
From source file:com.spotify.helios.client.DefaultHttpConnector.java
private HttpURLConnection connect0(final URI ipUri, final String method, final byte[] entity, final Map<String, List<String>> headers, final String endpointHost) throws IOException { if (log.isTraceEnabled()) { log.trace("req: {} {} {} {} {} {}", method, ipUri, headers.size(), Joiner.on(',').withKeyValueSeparator("=").join(headers), entity.length, Json.asPrettyStringUnchecked(entity)); } else {//ww w .ja v a2 s. com log.debug("req: {} {} {} {}", method, ipUri, headers.size(), entity.length); } final HttpURLConnection connection = (HttpURLConnection) ipUri.toURL().openConnection(); handleHttps(connection, endpointHost, hostnameVerifierProvider, extraHttpsHandler); connection.setRequestProperty("Accept-Encoding", "gzip"); connection.setInstanceFollowRedirects(false); connection.setConnectTimeout(httpTimeoutMillis); connection.setReadTimeout(httpTimeoutMillis); for (Map.Entry<String, List<String>> header : headers.entrySet()) { for (final String value : header.getValue()) { connection.addRequestProperty(header.getKey(), value); } } if (entity.length > 0) { connection.setDoOutput(true); connection.getOutputStream().write(entity); } setRequestMethod(connection, method, connection instanceof HttpsURLConnection); return connection; }
From source file:com.entertailion.android.dial.HttpRequestHelper.java
public InputStream getHttpStream(String urlString) throws IOException { InputStream in = null;//from w w w . j a v a2 s . co m int response = -1; URL url = new URL(urlString); URLConnection conn = url.openConnection(); if (!(conn instanceof HttpURLConnection)) throw new IOException("Not an HTTP connection"); try { HttpURLConnection httpConn = (HttpURLConnection) conn; httpConn.setAllowUserInteraction(false); httpConn.setInstanceFollowRedirects(true); httpConn.setRequestMethod("GET"); httpConn.connect(); response = httpConn.getResponseCode(); if (response == HttpURLConnection.HTTP_OK) { in = httpConn.getInputStream(); } } catch (Exception e) { throw new IOException("Error connecting"); } // end try-catch return in; }
From source file:com.shopgun.android.sdk.network.impl.HttpURLNetwork.java
private HttpURLConnection openConnection(Request<?> request, URL url) throws IOException { HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(request.getTimeOut()); connection.setReadTimeout(request.getTimeOut()); connection.setUseCaches(false);/*from w w w. ja va 2s. c o m*/ connection.setDoInput(true); connection.setInstanceFollowRedirects(false); setHeaders(request, connection); setRequestMethod(connection, request); return connection; }
From source file:org.lnicholls.galleon.util.IMDB.java
public static String getIMDBID2(String key) { String imdb = null;//from w w w.ja va2 s . c o m if (key != null) { StringBuffer buffer = new StringBuffer(); byte[] buf = new byte[1024]; int amount = 0; try { URL url = new URL("http://nicholls.us/imdb/imdbsearchxml.php?name=" + URLEncoder.encode(key)); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestProperty("User-Agent", "Galleon " + Tools.getVersion()); conn.setInstanceFollowRedirects(true); InputStream input = conn.getInputStream(); while ((amount = input.read(buf)) > 0) { buffer.append(new String(buf, 0, amount)); } input.close(); conn.disconnect(); SAXReader saxReader = new SAXReader(); StringReader stringReader = new StringReader(buffer.toString().trim()); Document document = saxReader.read(stringReader); //Document document = saxReader.read(new File("d:/galleon/imdb.xml")); Element root = document.getRootElement(); return Tools.getAttribute(root, "imdb"); } catch (Exception ex) { Tools.logException(IMDB.class, ex, "Could not get IMDB ID: " + key); } } return imdb; }
From source file:org.cloudfoundry.identity.uaa.test.TestAccountSetup.java
private OAuth2RestTemplate createRestTemplate(OAuth2ProtectedResourceDetails resource, AccessTokenRequest accessTokenRequest) { OAuth2ClientContext context = new DefaultOAuth2ClientContext(accessTokenRequest); OAuth2RestTemplate client = new OAuth2RestTemplate(resource, context); client.setRequestFactory(new SimpleClientHttpRequestFactory() { @Override/* w ww . j a v a 2 s . co m*/ protected void prepareConnection(HttpURLConnection connection, String httpMethod) throws IOException { super.prepareConnection(connection, httpMethod); connection.setInstanceFollowRedirects(false); } }); client.setErrorHandler(new OAuth2ErrorHandler(client.getResource()) { // Pass errors through in response entity for status code analysis @Override public boolean hasError(ClientHttpResponse response) throws IOException { return false; } @Override public void handleError(ClientHttpResponse response) throws IOException { } }); List<HttpMessageConverter<?>> list = new ArrayList<HttpMessageConverter<?>>(); list.add(new StringHttpMessageConverter()); list.add(new MappingJackson2HttpMessageConverter()); client.setMessageConverters(list); return client; }
From source file:org.ow2.proactive_grid_cloud_portal.cli.cmd.sched.PackageDownloader.java
/** * This method returns the real URL if the given URL forwards to another one. Otherwise it returns the given URL. * * @param url//from w w w . j a v a 2s .c o m * @return * @throws IOException */ private URL getRealURLIfForwarded(URL url) throws IOException { HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setInstanceFollowRedirects(false); con.connect(); return con.getHeaderField("Location") != null ? new URL(con.getHeaderField("Location")) : url; }
From source file:com.laurencedawson.image_management.ImageManager.java
/** * Grab and save an image directly to disk * @param file The Bitmap file/*from w w w . j av a 2s. c om*/ * @param url The URL of the image * @param imageCallback The callback associated with the request */ public static void cacheImage(final File file, ImageRequest imageCallback) { HttpURLConnection urlConnection = null; FileOutputStream fileOutputStream = null; InputStream inputStream = null; boolean isGif = false; try { // Setup the connection urlConnection = (HttpURLConnection) new URL(imageCallback.mUrl).openConnection(); urlConnection.setConnectTimeout(ImageManager.LONG_CONNECTION_TIMEOUT); urlConnection.setReadTimeout(ImageManager.LONG_REQUEST_TIMEOUT); urlConnection.setUseCaches(true); urlConnection.setInstanceFollowRedirects(true); // Set the progress to 0 imageCallback.sendProgressUpdate(imageCallback.mUrl, 0); // Connect inputStream = urlConnection.getInputStream(); // Do not proceed if the file wasn't downloaded if (urlConnection.getResponseCode() == 404) { urlConnection.disconnect(); return; } // Check if the image is a GIF String contentType = urlConnection.getHeaderField("Content-Type"); if (contentType != null) { isGif = contentType.equals(GIF_MIME); } // Grab the length of the image int length = 0; try { String fileLength = urlConnection.getHeaderField("Content-Length"); if (fileLength != null) { length = Integer.parseInt(fileLength); } } catch (NumberFormatException e) { if (ImageManager.DEBUG) { e.printStackTrace(); } } // Write the input stream to disk fileOutputStream = new FileOutputStream(file, true); int byteRead = 0; int totalRead = 0; final byte[] buffer = new byte[8192]; int frameCount = 0; // Download the image while ((byteRead = inputStream.read(buffer)) != -1) { // If the image is a gif, count the start of frames if (isGif) { for (int i = 0; i < byteRead - 3; i++) { if (buffer[i] == 33 && buffer[i + 1] == -7 && buffer[i + 2] == 4) { frameCount++; // Once we have at least one frame, stop the download if (frameCount > 1) { fileOutputStream.write(buffer, 0, i); fileOutputStream.close(); imageCallback.sendProgressUpdate(imageCallback.mUrl, 100); imageCallback.sendCachedCallback(imageCallback.mUrl, true); urlConnection.disconnect(); return; } } } } // Write the buffer to the file and update the total number of bytes // read so far (used for the callback) fileOutputStream.write(buffer, 0, byteRead); totalRead += byteRead; // Update the callback with the current progress if (length > 0) { imageCallback.sendProgressUpdate(imageCallback.mUrl, (int) (((float) totalRead / (float) length) * 100)); } } // Tidy up after the download if (fileOutputStream != null) { fileOutputStream.close(); } // Sent the callback that the image has been downloaded imageCallback.sendCachedCallback(imageCallback.mUrl, true); if (inputStream != null) { inputStream.close(); } // Disconnect the connection urlConnection.disconnect(); } catch (final MalformedURLException e) { if (ImageManager.DEBUG) { e.printStackTrace(); } // If the file exists and an error occurred, delete the file if (file != null) { file.delete(); } } catch (final IOException e) { if (ImageManager.DEBUG) { e.printStackTrace(); } // If the file exists and an error occurred, delete the file if (file != null) { file.delete(); } } }
From source file:uk.ac.ucl.excites.sapelli.collector.util.AsyncDownloader.java
private boolean download(String downloadUrl) { if (downloadUrl == null || downloadUrl.isEmpty()) { failure = new Exception("No URL given!"); return false; }// w ww .ja v a 2s . co m //Log.d(getClass().getSimpleName(), "Download URL: " + downloadUrl); if (DeviceControl.isOnline(context)) { InputStream input = null; OutputStream output = null; try { URL url = new URL(downloadUrl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setInstanceFollowRedirects(false); // we handle redirects manually below (otherwise HTTP->HTTPS redirects don't work): conn.connect(); // Detect & follow redirects: int status = conn.getResponseCode(); //Log.d(getClass().getSimpleName(), "Response Code: " + status); if (status == HttpURLConnection.HTTP_MOVED_TEMP || status == HttpURLConnection.HTTP_MOVED_PERM || status == HttpURLConnection.HTTP_SEE_OTHER) { // follow redirect url from "location" header field String newUrl = conn.getHeaderField("Location"); //Log.d(getClass().getSimpleName(), "Redirect to URL : " + newUrl); return download(newUrl); } // Getting file length final int fileLength = conn.getContentLength(); publishProgress(fileLength < 0 ? // when fileLength = -1 this means the server hasn't specified the file length -1 : // progressDialog will open and be set to indeterminate mode 0); // progressDialog will open and be set to 0 // Input stream to read file - with 8k buffer input = new BufferedInputStream(url.openStream(), 8192); // Output stream to write file output = new BufferedOutputStream(new FileOutputStream(downloadedFile)); byte data[] = new byte[1024]; int total = 0; int percentage = 0; int bytesRead; while ((bytesRead = input.read(data)) != -1) { // Complete % completion: if (fileLength > 0) // don't divide by 0 and only update progress if we know the fileLength (i.e. != -1) { int newPercentage = (int) ((total += bytesRead) / ((float) fileLength) * 100f); if (newPercentage != percentage) publishProgress(percentage = newPercentage); } // Write data to file... output.write(data, 0, bytesRead); } // Flush output: output.flush(); } catch (Exception e) { failure = e; return false; } finally { // Close streams: StreamHelpers.SilentClose(input); StreamHelpers.SilentClose(output); } //Log.d(getClass().getSimpleName(), "Download done"); return true; } else { failure = new Exception("The device is not online."); return false; } }
From source file:com.amastigote.xdu.query.module.WaterAndElectricity.java
@Override public boolean login(@NotNull String username, @NotNull String password) throws IOException { preLogin();//w w w . ja va 2 s. co m URL url = new URL(HOST + LOGIN_SUFFIX); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setRequestMethod("POST"); httpURLConnection.setInstanceFollowRedirects(false); httpURLConnection.setDoOutput(true); httpURLConnection.setRequestProperty("Cookie", "ASP.NET_SessionId=" + ASP_dot_NET_SessionId); httpURLConnection.setRequestProperty("AjaxPro-Method", "getLoginInput"); JSONObject jsonObject = new JSONObject(); jsonObject.put("webName", username); jsonObject.put("webPass", password); httpURLConnection.connect(); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(httpURLConnection.getOutputStream()); outputStreamWriter.write(jsonObject.toString()); outputStreamWriter.flush(); outputStreamWriter.close(); httpURLConnection.getResponseCode(); httpURLConnection.disconnect(); return checkIsLogin(username); }