List of usage examples for java.net HttpURLConnection getHeaderField
public String getHeaderField(int n)
From source file:com.ardnezar.lookapp.RoomParametersFetcher.java
private LinkedList<PeerConnection.IceServer> requestTurnServers(String url) throws IOException, JSONException { LinkedList<PeerConnection.IceServer> turnServers = new LinkedList<PeerConnection.IceServer>(); Log.d(TAG, "Request TURN from: " + url); HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); connection.setConnectTimeout(TURN_HTTP_TIMEOUT_MS); connection.setReadTimeout(TURN_HTTP_TIMEOUT_MS); int responseCode = connection.getResponseCode(); if (responseCode != 200) { throw new IOException("Non-200 response when requesting TURN server from " + url + " : " + connection.getHeaderField(null)); }//ww w . jav a 2 s . c o m InputStream responseStream = connection.getInputStream(); String response = drainStream(responseStream); connection.disconnect(); Log.d(TAG, "TURN response: " + response); JSONObject responseJSON = new JSONObject(response); String username = responseJSON.getString("username"); String password = responseJSON.getString("password"); JSONArray turnUris = responseJSON.getJSONArray("uris"); for (int i = 0; i < turnUris.length(); i++) { String uri = turnUris.getString(i); turnServers.add(new PeerConnection.IceServer(uri, username, password)); } return turnServers; }
From source file:org.exoplatform.utils.image.ExoPicassoDownloader.java
@Override public Response load(Uri uri, int networkPolicy) throws IOException { // TODO use networkPolicy as in com.squareup.picasso.UrlConnectionDownloader // https://github.com/square/picasso/blob/picasso-parent-2.5.2/picasso/src/main/java/com/squareup/picasso/UrlConnectionDownloader.java HttpURLConnection connection = connection(uri); connection.setInstanceFollowRedirects(true); connection.setUseCaches(true);/*from ww w . j a va 2 s .c o m*/ int responseCode = connection.getResponseCode(); // Handle HTTP redirections that are not managed by HttpURLConnection // automatically, e.g. HTTP -> HTTPS // TODO consider using OkHttp instead if (responseCode >= 300 && responseCode < 400) { String location = connection.getHeaderField("Location"); connection.disconnect(); connection = connection(Uri.parse(location)); connection.setInstanceFollowRedirects(true); connection.setUseCaches(true); responseCode = connection.getResponseCode(); } // Either the original or the new request have failed -> error if (responseCode >= 300) { connection.disconnect(); throw new ResponseException(responseCode + " " + connection.getResponseMessage(), networkPolicy, responseCode); } long contentLength = connection.getHeaderFieldInt("Content-Length", -1); // boolean fromCache = // parseResponseSourceHeader(connection.getHeaderField(RESPONSE_SOURCE)); boolean fromCache = false; return new Response(connection.getInputStream(), fromCache, contentLength); }
From source file:org.jumpmind.symmetric.transport.http.HttpIncomingTransport.java
/** * This method support redirection from an http connection to an https connection. * See {@link http://java.sun.com/j2se/1.4.2/docs/guide/deployment/deployment-guide/upgrade-guide/article-17.html} * for more information.//from w w w. j a v a2 s . c om * * @param connection * @return * @throws IOException */ private HttpURLConnection openConnectionCheckRedirects(HttpURLConnection connection) throws IOException { boolean redir; int redirects = 0; do { connection.setInstanceFollowRedirects(false); redir = false; int stat = connection.getResponseCode(); if (stat >= 300 && stat <= 307 && stat != 306 && stat != HttpURLConnection.HTTP_NOT_MODIFIED) { URL base = connection.getURL(); redirectionUrl = connection.getHeaderField("Location"); URL target = null; if (redirectionUrl != null) { target = new URL(base, redirectionUrl); } connection.disconnect(); // Redirection should be allowed only for HTTP and HTTPS // and should be limited to 5 redirections at most. if (target == null || !(target.getProtocol().equals("http") || target.getProtocol().equals("https")) || redirects >= 5) { throw new SecurityException("illegal URL redirect"); } redir = true; connection = HttpTransportManager.openConnection(target, getBasicAuthUsername(), getBasicAuthPassword()); connection.setConnectTimeout(httpTimeout); connection.setReadTimeout(httpTimeout); redirects++; } } while (redir); return connection; }
From source file:github.srlee309.lessWrongBookCreator.scraper.PostSectionExtractor.java
/** * @param src of image to download and save * @param folder to which to save the image * @param fileName to use for the saved image *//*from www . j a va2 s . com*/ protected final void saveImage(String src, String folder, String fileName) { if (fileName.contains("?")) { fileName = fileName.substring(0, fileName.lastIndexOf("?")); } fileName = fileName.replaceAll("[^a-zA-Z0-9.-]", "_"); // replace non valid file fileName characters File outputFile = new File(folder + "\\" + fileName); try { URL url = new URL(src); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.0"); connection.setRequestMethod("HEAD"); connection.setInstanceFollowRedirects(false); int rspCode = connection.getResponseCode(); if (rspCode == 301) { // redirected, so get new url String newUrl = connection.getHeaderField("Location"); url = new URL(newUrl); } connection.disconnect(); FileUtils.copyURLToFile(url, outputFile); } catch (MalformedURLException e) { logger.error("Malformed url exception for image src: " + src, e); } catch (IOException e) { logger.error("IO exception for saving image src locally: " + src, e); } }
From source file:ir.rasen.charsoo.controller.image_loader.core.download.BaseImageDownloader.java
/** * Retrieves {@link InputStream} of image by URI (image is located in the network). * * @param imageUri Image URI//from ww w . jav a 2 s . co m * @param extra Auxiliary object which was passed to {@link DisplayImageOptions.Builder#extraForDownloader(Object) * DisplayImageOptions.extraForDownloader(Object)}; can be null * @return {@link InputStream} of image * @throws IOException if some I/O error occurs during network request or if no InputStream could be created for * URL. */ protected InputStream getStreamFromNetwork(String imageUri, Object extra) throws IOException, JSONException { HttpURLConnection conn = createConnection(imageUri, extra); String strImage = ""; int redirectCount = 0; while (conn.getResponseCode() / 100 == 3 && redirectCount < MAX_REDIRECT_COUNT) { conn = createConnection(conn.getHeaderField("Location"), extra); redirectCount++; } InputStream imageStream = new InputStream() { @Override public int read() throws IOException { return 0; } }; try { imageStream = conn.getInputStream(); StringBuilder sb = new StringBuilder(); String line; BufferedReader br = null; try { br = new BufferedReader(new InputStreamReader(imageStream)); while ((line = br.readLine()) != null) { sb.append(line); } } catch (IOException e) { e.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } JSONObject jsonObject = new JSONObject(sb.toString()); if (jsonObject != null) { strImage = new JSONObject(jsonObject.getString(Params.RESULT)).getString(Params.IMAGE); imageStream = new ByteArrayInputStream(strImage.getBytes()); } else { imageStream = conn.getInputStream(); } } catch (Exception e) { // Read all data to allow reuse connection (http://bit.ly/1ad35PY) IoUtils.readAndCloseStream(conn.getErrorStream()); try { throw e; } catch (IOException e1) { e1.printStackTrace(); } catch (JSONException e1) { e1.printStackTrace(); } } if (!shouldBeProcessed(conn)) { IoUtils.closeSilently(imageStream); throw new IOException("Image request failed with response code " + conn.getResponseCode()); } return new ContentLengthInputStream(new BufferedInputStream(imageStream, BUFFER_SIZE), strImage.length()); }
From source file:com.sogrey.sinaweibo.utils.FileUtil.java
/** * ???xx.??.// w w w . ja v a2 s . co m * * @param connection * * @return ?? */ public static String getRealFileName(HttpURLConnection connection) { String name = null; try { if (connection == null) { return name; } if (connection.getResponseCode() == 200) { for (int i = 0;; i++) { String mime = connection.getHeaderField(i); if (mime == null) { break; } // "Content-Disposition","attachment; filename=1.txt" // Content-Length if ("content-disposition".equals(connection.getHeaderFieldKey(i).toLowerCase())) { Matcher m = Pattern.compile(".*filename=(.*)").matcher(mime.toLowerCase()); if (m.find()) { return m.group(1).replace("\"", ""); } } } } } catch (Exception e) { e.printStackTrace(); LogUtil.e(FileUtil.class, "???"); } return name; }
From source file:bolts.WebViewAppLinkResolver.java
@Override public Task<AppLink> getAppLinkFromUrlInBackground(final Uri url) { final Capture<String> content = new Capture<String>(); final Capture<String> contentType = new Capture<String>(); return Task.callInBackground(new Callable<Void>() { @Override//from ww w. j a va2s.c o m public Void call() throws Exception { URL currentURL = new URL(url.toString()); URLConnection connection = null; while (currentURL != null) { // Fetch the content at the given URL. connection = currentURL.openConnection(); if (connection instanceof HttpURLConnection) { // Unfortunately, this doesn't actually follow redirects if they go from http->https, // so we have to do that manually. ((HttpURLConnection) connection).setInstanceFollowRedirects(true); } connection.setRequestProperty(PREFER_HEADER, META_TAG_PREFIX); connection.connect(); if (connection instanceof HttpURLConnection) { HttpURLConnection httpConnection = (HttpURLConnection) connection; if (httpConnection.getResponseCode() >= 300 && httpConnection.getResponseCode() < 400) { currentURL = new URL(httpConnection.getHeaderField("Location")); httpConnection.disconnect(); } else { currentURL = null; } } else { currentURL = null; } } try { content.set(readFromConnection(connection)); contentType.set(connection.getContentType()); } finally { if (connection instanceof HttpURLConnection) { ((HttpURLConnection) connection).disconnect(); } } return null; } }).onSuccessTask(new Continuation<Void, Task<JSONArray>>() { @Override public Task<JSONArray> then(Task<Void> task) throws Exception { // Load the content in a WebView and use JavaScript to extract the meta tags. final TaskCompletionSource<JSONArray> tcs = new TaskCompletionSource<>(); final WebView webView = new WebView(context); webView.getSettings().setJavaScriptEnabled(true); webView.setNetworkAvailable(false); webView.setWebViewClient(new WebViewClient() { private boolean loaded = false; private void runJavaScript(WebView view) { if (!loaded) { // After the first resource has been loaded (which will be the pre-populated data) // run the JavaScript meta tag extraction script loaded = true; view.loadUrl(TAG_EXTRACTION_JAVASCRIPT); } } @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); runJavaScript(view); } @Override public void onLoadResource(WebView view, String url) { super.onLoadResource(view, url); runJavaScript(view); } }); // Inject an object that will receive the JSON for the extracted JavaScript tags webView.addJavascriptInterface(new Object() { @JavascriptInterface public void setValue(String value) { try { tcs.trySetResult(new JSONArray(value)); } catch (JSONException e) { tcs.trySetError(e); } } }, "boltsWebViewAppLinkResolverResult"); String inferredContentType = null; if (contentType.get() != null) { inferredContentType = contentType.get().split(";")[0]; } webView.loadDataWithBaseURL(url.toString(), content.get(), inferredContentType, null, null); return tcs.getTask(); } }, Task.UI_THREAD_EXECUTOR).onSuccess(new Continuation<JSONArray, AppLink>() { @Override public AppLink then(Task<JSONArray> task) throws Exception { Map<String, Object> alData = parseAlData(task.getResult()); AppLink appLink = makeAppLinkFromAlData(alData, url); return appLink; } }); }
From source file:CloudManagerAPI.java
private boolean hasJsonContent(HttpURLConnection connection) { final String contentType = connection.getHeaderField("Content-Type"); final String contentLength = connection.getHeaderField("Content-Length"); return "application/json".equals(contentType) && !"0".equals(contentLength); }
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();/*w ww. j a va 2 s . c o m*/ 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.github.dozermapper.core.builder.xml.SchemaLSResourceResolver.java
/** * Attempts to open a http connection for the systemId resource, and follows the first redirect * * @param systemId url to the XSD/*from ww w.j ava 2 s . c o m*/ * @return stream to XSD * @throws IOException if fails to find XSD */ private InputStream resolveFromURL(String systemId) throws IOException { LOG.debug("Trying to download [{}]", systemId); URL obj = new URL(systemId); HttpURLConnection conn = (HttpURLConnection) obj.openConnection(); int status = conn.getResponseCode(); if ((status != HttpURLConnection.HTTP_OK) && (status == HttpURLConnection.HTTP_MOVED_TEMP || status == HttpURLConnection.HTTP_MOVED_PERM || status == HttpURLConnection.HTTP_SEE_OTHER)) { LOG.debug("Received status of {}, attempting to follow Location header", status); String newUrl = conn.getHeaderField("Location"); conn = (HttpURLConnection) new URL(newUrl).openConnection(); } return conn.getInputStream(); }