Search in sources :

Example 31 with HttpURLConnection

use of java.net.HttpURLConnection in project hive by apache.

the class TestLlapWebServices method getURLResponseAsString.

private String getURLResponseAsString(String baseURL) throws IOException {
    URL url = new URL(baseURL);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    Assert.assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode());
    StringWriter writer = new StringWriter();
    IOUtils.copy(conn.getInputStream(), writer, "UTF-8");
    return writer.toString();
}
Also used : HttpURLConnection(java.net.HttpURLConnection) StringWriter(java.io.StringWriter) URL(java.net.URL)

Example 32 with HttpURLConnection

use of java.net.HttpURLConnection in project buck by facebook.

the class HttpDownloader method createConnection.

protected HttpURLConnection createConnection(URI uri) throws IOException {
    HttpURLConnection connection;
    if (proxy.isPresent()) {
        connection = (HttpURLConnection) uri.toURL().openConnection(proxy.get());
    } else {
        connection = (HttpURLConnection) uri.toURL().openConnection();
    }
    connection.setConnectTimeout((int) TimeUnit.SECONDS.toMillis(20));
    connection.setInstanceFollowRedirects(true);
    return connection;
}
Also used : HttpURLConnection(java.net.HttpURLConnection)

Example 33 with HttpURLConnection

use of java.net.HttpURLConnection in project buck by facebook.

the class HttpDownloader method fetch.

@Override
public boolean fetch(BuckEventBus eventBus, URI uri, Optional<PasswordAuthentication> authentication, Path output) throws IOException {
    if (!("https".equals(uri.getScheme()) || "http".equals(uri.getScheme()))) {
        return false;
    }
    DownloadEvent.Started started = DownloadEvent.started(uri);
    eventBus.post(started);
    try {
        HttpURLConnection connection = createConnection(uri);
        if (authentication.isPresent()) {
            if ("https".equals(uri.getScheme()) && connection instanceof HttpsURLConnection) {
                PasswordAuthentication p = authentication.get();
                String authStr = p.getUserName() + ":" + new String(p.getPassword());
                String authEncoded = BaseEncoding.base64().encode(authStr.getBytes(StandardCharsets.UTF_8));
                connection.addRequestProperty("Authorization", "Basic " + authEncoded);
            } else {
                LOG.info("Refusing to send basic authentication over plain http.");
                return false;
            }
        }
        if (HttpURLConnection.HTTP_OK != connection.getResponseCode()) {
            LOG.info("Unable to download %s: %s", uri, connection.getResponseMessage());
            return false;
        }
        long contentLength = connection.getContentLengthLong();
        try (InputStream is = new BufferedInputStream(connection.getInputStream());
            OutputStream os = new BufferedOutputStream(Files.newOutputStream(output))) {
            long read = 0;
            while (true) {
                int r = is.read();
                read++;
                if (r == -1) {
                    break;
                }
                if (read % PROGRESS_REPORT_EVERY_N_BYTES == 0) {
                    eventBus.post(new DownloadProgressEvent(uri, contentLength, read));
                }
                os.write(r);
            }
        }
        return true;
    } finally {
        eventBus.post(DownloadEvent.finished(started));
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) BufferedOutputStream(java.io.BufferedOutputStream) HttpURLConnection(java.net.HttpURLConnection) BufferedInputStream(java.io.BufferedInputStream) BufferedOutputStream(java.io.BufferedOutputStream) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) PasswordAuthentication(java.net.PasswordAuthentication)

Example 34 with HttpURLConnection

use of java.net.HttpURLConnection in project jmonkeyengine by jMonkeyEngine.

the class HttpZipLocator method readData.

private InputStream readData(int offset, int length) throws IOException {
    HttpURLConnection conn = (HttpURLConnection) zipUrl.openConnection();
    conn.setDoOutput(false);
    conn.setUseCaches(false);
    conn.setInstanceFollowRedirects(false);
    String range = "-";
    if (offset != Integer.MAX_VALUE) {
        range = offset + range;
    }
    if (length != Integer.MAX_VALUE) {
        if (offset != Integer.MAX_VALUE) {
            range = range + (offset + length - 1);
        } else {
            range = range + length;
        }
    }
    conn.setRequestProperty("Range", "bytes=" + range);
    conn.connect();
    if (conn.getResponseCode() == HttpURLConnection.HTTP_PARTIAL) {
        return conn.getInputStream();
    } else if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
        throw new IOException("Your server does not support HTTP feature Content-Range. Please contact your server administrator.");
    } else {
        throw new IOException(conn.getResponseCode() + " " + conn.getResponseMessage());
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) IOException(java.io.IOException)

Example 35 with HttpURLConnection

use of java.net.HttpURLConnection in project Android by hmkcode.

the class MainActivity method HttpGet.

private String HttpGet(String myUrl) throws IOException {
    InputStream inputStream = null;
    String result = "";
    URL url = new URL(myUrl);
    // create HttpURLConnection
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    // make GET request to the given URL
    conn.connect();
    // receive response as inputStream
    inputStream = conn.getInputStream();
    // convert inputstream to string
    if (inputStream != null)
        result = convertInputStreamToString(inputStream);
    else
        result = "Did not work!";
    return result;
}
Also used : HttpURLConnection(java.net.HttpURLConnection) InputStream(java.io.InputStream) URL(java.net.URL)

Aggregations

HttpURLConnection (java.net.HttpURLConnection)3831 URL (java.net.URL)2447 IOException (java.io.IOException)1634 InputStream (java.io.InputStream)1082 InputStreamReader (java.io.InputStreamReader)692 Test (org.junit.Test)650 BufferedReader (java.io.BufferedReader)573 OutputStream (java.io.OutputStream)466 MalformedURLException (java.net.MalformedURLException)372 URLConnection (java.net.URLConnection)248 HashMap (java.util.HashMap)216 OutputStreamWriter (java.io.OutputStreamWriter)208 Map (java.util.Map)199 Gson (com.google.gson.Gson)190 ByteArrayOutputStream (java.io.ByteArrayOutputStream)186 ArrayList (java.util.ArrayList)168 ExecutionException (java.util.concurrent.ExecutionException)161 File (java.io.File)159 AsyncTask (android.os.AsyncTask)158 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)157