HttpURLConnection connection = null; try { URL url = new URL(sUrl[0]); connection = (HttpURLConnection) url.openConnection(); connection.connect(); if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) { return "Server returned HTTP " + connection.getResponseCode() + " " + connection.getResponseMessage(); input = connection.getInputStream(); output = new FileOutputStream("/sdcard/file_name.extension"); connection.disconnect();
/** * This implementation opens an InputStream for the given URL. * <p>It sets the {@code useCaches} flag to {@code false}, * mainly to avoid jar file locking on Windows. * @see java.net.URL#openConnection() * @see java.net.URLConnection#setUseCaches(boolean) * @see java.net.URLConnection#getInputStream() */ @Override public InputStream getInputStream() throws IOException { URLConnection con = this.url.openConnection(); ResourceUtils.useCachesIfNecessary(con); try { return con.getInputStream(); } catch (IOException ex) { // Close the HTTP connection (if applicable). if (con instanceof HttpURLConnection) { ((HttpURLConnection) con).disconnect(); } throw ex; } }
future = executor.submit(new Callable<MyResult>() { @Override public MyResult call() throws IOException { HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); try { // .. use the connection ... } finally { connection.disconnect(); } } }); MyResult result = future.get(TIMEOUT, TimeUnit.SECONDS);
logger.debug("Retrieving Atmosphere's latest version from http://async-io.org/version.html"); HttpURLConnection urlConnection = (HttpURLConnection) URI.create("http://async-io.org/version.html").toURL().openConnection(); urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0"); urlConnection.setRequestProperty("Connection", "keep-alive"); urlConnection.setInstanceFollowRedirects(true); BufferedReader in = new BufferedReader(new InputStreamReader( urlConnection.getInputStream())); } catch (IOException ex) { urlConnection.disconnect();
URLConnection con = url.openConnection(); customizeConnection(con); if (con instanceof HttpURLConnection) { HttpURLConnection httpCon = (HttpURLConnection) con; int code = httpCon.getResponseCode(); if (code != HttpURLConnection.HTTP_OK) { httpCon.disconnect(); return false;
public void request(String urlString) { HttpURLConnection urlConnection = null; try { URL url = new URL(urlString); urlConnection = openURLConnection(url); urlConnection.setInstanceFollowRedirects(true); urlConnection.setRequestMethod(GET_METHOD_NAME); urlConnection.setRequestProperty("User-agent", uaName + " (" + osString + ")"); logger.debug("Sending Server's information to Atmosphere's Google Analytics {} {}", urlString, uaName + " (" + osString + ")"); urlConnection.connect(); int responseCode = getResponseCode(urlConnection); if (responseCode != HttpURLConnection.HTTP_OK) { logError("JGoogleAnalytics: Error tracking, url=" + urlString); } else { logMessage(SUCCESS_MESSAGE); } } catch (Exception e) { logError(e.getMessage()); } finally { if (urlConnection != null) { urlConnection.disconnect(); } } }
URL url = new URL(dataSpec.uri.toString()); @HttpMethod int httpMethod = dataSpec.httpMethod; byte[] httpBody = dataSpec.httpBody; makeConnection( url, httpMethod, httpBody, position, length, allowGzip, false /* followRedirects */); int responseCode = connection.getResponseCode(); String location = connection.getHeaderField("Location"); if ((httpMethod == DataSpec.HTTP_METHOD_GET || httpMethod == DataSpec.HTTP_METHOD_HEAD) || responseCode == HTTP_STATUS_TEMPORARY_REDIRECT || responseCode == HTTP_STATUS_PERMANENT_REDIRECT)) { connection.disconnect(); url = handleRedirect(url, location); } else if (httpMethod == DataSpec.HTTP_METHOD_POST || responseCode == HttpURLConnection.HTTP_SEE_OTHER)) { connection.disconnect(); httpMethod = DataSpec.HTTP_METHOD_GET; httpBody = null;
URL url = new URL("http://www.android.com/"); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); try { InputStream in = new BufferedInputStream(urlConnection.getInputStream()); readStream(in); finally { urlConnection.disconnect(); } }
@Override @NonNull public Result<HttpResponse> apply(@NonNull final HttpRequest request) { try { final HttpURLConnection connection = (HttpURLConnection) new URL(request.url).openConnection(); try { return success(getHttpResponseResult(request, connection)); } finally { connection.disconnect(); } } catch (final IOException exception) { return failure(exception); } }
URLConnection con = url.openConnection(); customizeConnection(con); HttpURLConnection httpCon = (con instanceof HttpURLConnection ? (HttpURLConnection) con : null); if (httpCon != null) { int code = httpCon.getResponseCode(); if (code == HttpURLConnection.HTTP_OK) { return true; httpCon.disconnect(); return false;
@Nonnull @Override public InputStream asInputStream() throws IOException { URLConnection con = this.url.openConnection(); con.setUseCaches(true); try { return con.getInputStream(); } catch (IOException ex) { if (con instanceof HttpURLConnection) { ((HttpURLConnection) con).disconnect(); } throw ex; } }
String uri = "http://localhost:8080/CustomerService/rest/customers/1"; URL url = new URL(uri); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setRequestProperty("Accept", "application/xml"); JAXBContext jc = JAXBContext.newInstance(Customer.class); InputStream xml = connection.getInputStream(); Customer customer = (Customer) jc.createUnmarshaller().unmarshal(xml); connection.disconnect();
urlConnection = (HttpURLConnection) mUrl.openConnection(); writeToUrlConnection(urlConnection); responseCode = urlConnection.getResponseCode(); inputStream = new BufferedInputStream(urlConnection.getInputStream()); result = readInputStream(inputStream); } finally { if (urlConnection != null) { urlConnection.disconnect();
private InputStream inputStream() throws IOException { final URLConnection connection = this.url.openConnection(); if (connection.getClass().getSimpleName().startsWith("JNLP")) { connection.setUseCaches(true); } final InputStream inputStream; try { inputStream = connection.getInputStream(); } catch (final IOException e) { if (connection instanceof HttpURLConnection) { // disconnect() will probably close the underlying socket, which is fine given we had an exception ((HttpURLConnection) connection).disconnect(); } throw e; } return inputStream; }
/** * If the first URL we try to access with a HTTP proxy is HTTPS then the authentication cache will not have been * pre-populated, so we try to access at least one HTTP URL before the very first HTTPS url. * @param proxy * @param url the actual URL being opened. */ private void jenkins48775workaround(Proxy proxy, URL url) { if ("https".equals(url.getProtocol()) && !authCacheSeeded && proxy != Proxy.NO_PROXY) { HttpURLConnection preAuth = null; try { // We do not care if there is anything at this URL, all we care is that it is using the proxy preAuth = (HttpURLConnection) new URL("http", url.getHost(), -1, "/").openConnection(proxy); preAuth.setRequestMethod("HEAD"); preAuth.connect(); } catch (IOException e) { // ignore, this is just a probe we don't care at all } finally { if (preAuth != null) { preAuth.disconnect(); } } authCacheSeeded = true; } else if ("https".equals(url.getProtocol())){ // if we access any http url using a proxy then the auth cache will have been seeded authCacheSeeded = authCacheSeeded || proxy != Proxy.NO_PROXY; } }
} else { URLConnection con = url.openConnection(); customizeConnection(con); HttpURLConnection httpCon = (con instanceof HttpURLConnection ? (HttpURLConnection) con : null); if (httpCon != null) { int code = httpCon.getResponseCode(); if (code == HttpURLConnection.HTTP_OK) { return true; httpCon.disconnect(); return false; } else {
/** * This implementation opens an InputStream for the given URL. * <p>It sets the {@code useCaches} flag to {@code false}, * mainly to avoid jar file locking on Windows. * @see java.net.URL#openConnection() * @see java.net.URLConnection#setUseCaches(boolean) * @see java.net.URLConnection#getInputStream() */ @Override public InputStream getInputStream() throws IOException { URLConnection con = this.url.openConnection(); ResourceUtils.useCachesIfNecessary(con); try { return con.getInputStream(); } catch (IOException ex) { // Close the HTTP connection (if applicable). if (con instanceof HttpURLConnection) { ((HttpURLConnection) con).disconnect(); } throw ex; } }
private int tryGetFileSize(URL url) { HttpURLConnection conn = null; try { conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("HEAD"); conn.getInputStream(); return conn.getContentLength(); } catch (IOException e) { return -1; } finally { conn.disconnect(); } }
URLConnection con = url.openConnection(); customizeConnection(con); if (con instanceof HttpURLConnection) { HttpURLConnection httpCon = (HttpURLConnection) con; int code = httpCon.getResponseCode(); if (code != HttpURLConnection.HTTP_OK) { httpCon.disconnect(); return false;
/** * This implementation opens an InputStream for the given URL. * It sets the "UseCaches" flag to <code>false</code>, * mainly to avoid jar file locking on Windows. * * @return The input stream * @throws IOException if there is an error * @see java.net.URL#openConnection() * @see java.net.URLConnection#setUseCaches(boolean) * @see java.net.URLConnection#getInputStream() */ public InputStream getInputStream() throws IOException { URLConnection con = url.openConnection(); useCachesIfNecessary(con); try { return con.getInputStream(); } catch (IOException ex) { // Close the HTTP connection (if applicable). if (con instanceof HttpURLConnection) { ((HttpURLConnection) con).disconnect(); } throw ex; } }