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();
}
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;
}
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));
}
}
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());
}
}
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;
}
Aggregations