Java Code Examples for java.net.HttpURLConnection#getInputStream()
The following examples show how to use
java.net.HttpURLConnection#getInputStream() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestHttpFSServer.java From big-c with Apache License 2.0 | 8 votes |
@Test @TestDir @TestJetty @TestHdfs public void testOpenOffsetLength() throws Exception { createHttpFSServer(false); byte[] array = new byte[]{0, 1, 2, 3}; FileSystem fs = FileSystem.get(TestHdfsHelper.getHdfsConf()); fs.mkdirs(new Path("/tmp")); OutputStream os = fs.create(new Path("/tmp/foo")); os.write(array); os.close(); String user = HadoopUsersConfTestHelper.getHadoopUsers()[0]; URL url = new URL(TestJettyHelper.getJettyURL(), MessageFormat.format("/webhdfs/v1/tmp/foo?user.name={0}&op=open&offset=1&length=2", user)); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); Assert.assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode()); InputStream is = conn.getInputStream(); Assert.assertEquals(1, is.read()); Assert.assertEquals(2, is.read()); Assert.assertEquals(-1, is.read()); }
Example 2
Source File: VmwareContext.java From cloudstack with Apache License 2.0 | 6 votes |
public void uploadResourceContent(String urlString, byte[] content) throws Exception { // vSphere does not support POST HttpURLConnection conn = getHTTPConnection(urlString, "PUT"); OutputStream out = conn.getOutputStream(); out.write(content); out.flush(); BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), getCharSetFromConnection(conn))); String line; while ((in.ready()) && (line = in.readLine()) != null) { if (s_logger.isTraceEnabled()) s_logger.trace("Upload " + urlString + " response: " + line); } out.close(); in.close(); }
Example 3
Source File: StreamingRetry.java From openjdk-8-source with GNU General Public License v2.0 | 6 votes |
void test() throws IOException { ss = new ServerSocket(0); ss.setSoTimeout(ACCEPT_TIMEOUT); int port = ss.getLocalPort(); (new Thread(this)).start(); try { URL url = new URL("http://localhost:" + port + "/"); HttpURLConnection uc = (HttpURLConnection) url.openConnection(); uc.setDoOutput(true); uc.setChunkedStreamingMode(4096); OutputStream os = uc.getOutputStream(); os.write("Hello there".getBytes()); InputStream is = uc.getInputStream(); is.close(); } catch (IOException expected) { //expected.printStackTrace(); } finally { ss.close(); } }
Example 4
Source File: HttpServerTest.java From msf4j with Apache License 2.0 | 6 votes |
@Test public void testUploadReject() throws Exception { HttpURLConnection urlConn = request("/test/v1/uploadReject", HttpMethod.POST, true); try { urlConn.setChunkedStreamingMode(1024); urlConn.getOutputStream().write("Rejected Content".getBytes(Charsets.UTF_8)); try { urlConn.getInputStream(); fail(); } catch (IOException e) { // Expect to get exception since server response with 400. Just drain the error stream. IOUtils.toByteArray(urlConn.getErrorStream()); assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), urlConn.getResponseCode()); } } finally { urlConn.disconnect(); } }
Example 5
Source File: DownloadTask.java From YalpStore with GNU General Public License v2.0 | 6 votes |
private void start() throws DownloadException { HttpURLConnection connection; InputStream in; try { connection = NetworkUtil.getHttpURLConnection(request.getUrl()); if (!TextUtils.isEmpty(request.getCookieString())) { connection.addRequestProperty("Cookie", request.getCookieString()); } if (request.getDestination().exists()) { connection.setRequestProperty("Range", "Bytes=" + request.getDestination().length() + "-"); } in = connection.getInputStream(); } catch (IOException e) { e.printStackTrace(); throw new DownloadException("Could not open network connection: " + e.getMessage(), DownloadManager.Error.HTTP_DATA_ERROR); } try { writeToFile(in); } finally { connection.disconnect(); } }
Example 6
Source File: HttpServerTest.java From msf4j with Apache License 2.0 | 5 votes |
@Test public void testFormDataParamWithSimpleRequest() throws IOException, URISyntaxException { // Send x-form-url-encoded request HttpURLConnection connection = request("/test/v1/formDataParam", HttpMethod.POST); String rawData = "name=wso2&age=10"; ByteBuffer encodedData = Charset.defaultCharset().encode(rawData); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", MediaType.APPLICATION_FORM_URLENCODED); connection.setRequestProperty("Content-Length", String.valueOf(encodedData.array().length)); try (OutputStream os = connection.getOutputStream()) { os.write(Arrays.copyOf(encodedData.array(), encodedData.limit())); } InputStream inputStream = connection.getInputStream(); String response = StreamUtil.asString(inputStream); IOUtils.closeQuietly(inputStream); connection.disconnect(); assertEquals(response, "wso2:10"); // Send multipart/form-data request connection = request("/test/v1/formDataParam", HttpMethod.POST); MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.addPart("name", new StringBody("wso2", ContentType.TEXT_PLAIN)); builder.addPart("age", new StringBody("10", ContentType.TEXT_PLAIN)); HttpEntity build = builder.build(); connection.setRequestProperty("Content-Type", build.getContentType().getValue()); try (OutputStream out = connection.getOutputStream()) { build.writeTo(out); } inputStream = connection.getInputStream(); response = StreamUtil.asString(inputStream); IOUtils.closeQuietly(inputStream); connection.disconnect(); assertEquals(response, "wso2:10"); }
Example 7
Source File: SNSIntegrationTest.java From aws-sdk-java-v2 with Apache License 2.0 | 5 votes |
private InputStream getCertificateStream(String certUrl) { try { URL cert = new URL(certUrl); HttpURLConnection connection = (HttpURLConnection) cert.openConnection(); if (connection.getResponseCode() != 200) { throw new RuntimeException("Received non 200 response when requesting certificate " + certUrl); } return connection.getInputStream(); } catch (IOException e) { throw new UncheckedIOException("Unable to request certificate " + certUrl, e); } }
Example 8
Source File: CounterTest.java From nopol with GNU General Public License v2.0 | 5 votes |
private void sendTo(String url, String cmd) throws IOException { URL obj = new URL(url + "/" + cmd); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); con.setRequestMethod("GET"); con.getInputStream(); BufferedReader in = new BufferedReader( new InputStreamReader(con.getInputStream())); String inputLine; StringBuilder response = new StringBuilder(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); assertEquals(cmd, response.toString()); }
Example 9
Source File: SchemaDynamicValidityTest.java From microprofile-graphql with Apache License 2.0 | 5 votes |
private String getContent(HttpURLConnection connection) throws IOException{ try (StringWriter sw = new StringWriter(); BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) { String inputLine; while ((inputLine = in.readLine()) != null){ sw.write(inputLine); sw.write("\n"); } return sw.toString(); } }
Example 10
Source File: StreamingRetry.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
void test(String method) throws Exception { ss = new ServerSocket(0); ss.setSoTimeout(ACCEPT_TIMEOUT); int port = ss.getLocalPort(); Thread otherThread = new Thread(this); otherThread.start(); try { URL url = new URL("http://localhost:" + port + "/"); HttpURLConnection uc = (HttpURLConnection) url.openConnection(); uc.setDoOutput(true); if (method != null) uc.setRequestMethod(method); uc.setChunkedStreamingMode(4096); OutputStream os = uc.getOutputStream(); os.write("Hello there".getBytes()); InputStream is = uc.getInputStream(); is.close(); } catch (IOException expected) { //expected.printStackTrace(); } finally { ss.close(); otherThread.join(); } }
Example 11
Source File: UrlNetworkManager.java From Yahala-Messenger with MIT License | 5 votes |
@Override public void retrieveImage(String url, File f) { InputStream is = null; OutputStream os = null; HttpURLConnection conn = null; applyChangeonSdkVersion(settings.getSdkVersion()); try { conn = openConnection(url); conn.setConnectTimeout(settings.getConnectionTimeout()); conn.setReadTimeout(settings.getReadTimeout()); handleHeaders(conn); if (conn.getResponseCode() == TEMP_REDIRECT) { redirectManually(f, conn); } else { is = conn.getInputStream(); os = new FileOutputStream(f); fileUtil.copyStream(is, os); } } catch (FileNotFoundException fnfe) { throw new ImageNotFoundException(); } catch (Throwable ex) { ex.printStackTrace(); // TODO } finally { if (conn != null && settings.getDisconnectOnEveryCall()) { conn.disconnect(); } fileUtil.closeSilently(is); fileUtil.closeSilently(os); } }
Example 12
Source File: HTTP.java From prayer-times-android with Apache License 2.0 | 5 votes |
public static String get(String Url) { try { URL url = new URL(Url); HttpURLConnection hc = (HttpURLConnection) url.openConnection(); hc.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2"); hc.connect(); int responseCode = hc.getResponseCode(); BufferedReader in = new BufferedReader(new InputStreamReader(hc.getInputStream())); String line; StringBuilder data = new StringBuilder(); while ((line = in.readLine()) != null) { data.append(line).append("\n"); } in.close(); return data.toString(); } catch (Exception e) { e.printStackTrace(); } return null; }
Example 13
Source File: LCNetClient.java From AILibs with GNU Affero General Public License v3.0 | 5 votes |
public void deleteNet(final String identifier) throws IOException { HttpURLConnection httpCon = this.establishHttpCon("delete", identifier); try (OutputStreamWriter out = new OutputStreamWriter(httpCon.getOutputStream());) { httpCon.getInputStream(); } }
Example 14
Source File: BasicLongCredentials.java From jdk8u_jdk with GNU General Public License v2.0 | 5 votes |
public static void main (String[] args) throws Exception { HttpServer server = HttpServer.create(new InetSocketAddress(0), 0); try { Handler handler = new Handler(); HttpContext ctx = server.createContext("/test", handler); BasicAuthenticator a = new BasicAuthenticator(REALM) { public boolean checkCredentials (String username, String pw) { return USERNAME.equals(username) && PASSWORD.equals(pw); } }; ctx.setAuthenticator(a); server.start(); Authenticator.setDefault(new MyAuthenticator()); URL url = new URL("http://localhost:"+server.getAddress().getPort()+"/test/"); HttpURLConnection urlc = (HttpURLConnection)url.openConnection(); InputStream is = urlc.getInputStream(); int c = 0; while (is.read()!= -1) { c ++; } if (c != 0) { throw new RuntimeException("Test failed c = " + c); } if (error) { throw new RuntimeException("Test failed: error"); } System.out.println ("OK"); } finally { server.stop(0); } }
Example 15
Source File: URLConnectionTest.java From j2objc with Apache License 2.0 | 5 votes |
/** * http://code.google.com/p/android/issues/detail?id=14562 */ public void testReadAfterLastByte() throws Exception { server.enqueue(new MockResponse() .setBody("ABC") .clearHeaders() .addHeader("Connection: close") .setSocketPolicy(SocketPolicy.DISCONNECT_AT_END)); server.play(); HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection(); InputStream in = connection.getInputStream(); assertEquals("ABC", readAscii(in, 3)); assertEquals(-1, in.read()); assertEquals(-1, in.read()); // throws IOException in Gingerbread }
Example 16
Source File: htmlActivity.java From stynico with MIT License | 5 votes |
private void urlConGetWebData() throws IOException { String pediyUrl=ped.getText().toString(); URL url = new URL(pediyUrl); HttpURLConnection httpConn = (HttpURLConnection)url.openConnection(); if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) { //Log.d("TAG", "---into-----urlConnection---success--"); InputStreamReader isr = new InputStreamReader(httpConn.getInputStream(), "utf-8"); int i; String content = ""; while ((i = isr.read()) != -1) { content = content + (char)i; } mHandler.obtainMessage(MSG_SUCCESS, content).sendToTarget(); isr.close(); httpConn.disconnect(); } else { //Log.d("TAG", "---into-----urlConnection---fail--"); } }
Example 17
Source File: GeoIPService.java From proxylive with MIT License | 4 votes |
@Scheduled(fixedDelay = 86400 * 1000) //Every 24H @PostConstruct private void downloadIPLocationDatabase() throws Exception { if(config.getGeoIP().isEnabled()){ logger.info("Downloading GEOIP Database from: "+config.getGeoIP().getUrl()); File tmpGEOIPFileRound = File.createTempFile("geoIP", "mmdb"); FileOutputStream fos = new FileOutputStream(tmpGEOIPFileRound); HttpURLConnection connection = getURLConnection(config.getGeoIP().getUrl()); if (connection.getResponseCode() != 200) { return; } TarArchiveInputStream tarGzGeoIPStream = new TarArchiveInputStream(new GZIPInputStream(connection.getInputStream())); TarArchiveEntry entry= null; int offset; long pointer=0; while ((entry = tarGzGeoIPStream.getNextTarEntry()) != null) { pointer+=entry.getSize(); if(entry.getName().endsWith("GeoLite2-City.mmdb")){ byte[] content = new byte[(int) entry.getSize()]; offset=0; //FileInputStream fis = new FileInputStream(entry.getFile()); //IOUtils.copy(fis,fos); //tarGzGeoIPStream.skip(pointer); //tarGzGeoIPStream.read(content,offset,content.length-offset); //IOUtils.write(content,fos); int r; byte[] b = new byte[1024]; while ((r = tarGzGeoIPStream.read(b)) != -1) { fos.write(b, 0, r); } //fis.close(); break; } } tarGzGeoIPStream.close(); fos.flush(); fos.close(); connection.disconnect(); geoIPDB = new DatabaseReader.Builder(tmpGEOIPFileRound).withCache(new CHMCache()).build(); if (tmpGEOIPFile != null && tmpGEOIPFile.exists()) { tmpGEOIPFile.delete(); } tmpGEOIPFile = tmpGEOIPFileRound; } }
Example 18
Source File: DefaultGiteaConnection.java From gitea-plugin with MIT License | 4 votes |
private <T> T post(UriTemplate template, Object body, final Class<T> modelClass) throws IOException, InterruptedException { HttpURLConnection connection = openConnection(template); withAuthentication(connection); connection.setRequestMethod("POST"); byte[] bytes; if (body != null) { bytes = mapper.writer(new StdDateFormat()).writeValueAsBytes(body); connection.setRequestProperty("Content-Type", "application/json"); connection.setRequestProperty("Content-Length", Integer.toString(bytes.length)); connection.setDoOutput(true); } else { bytes = null; connection.setDoOutput(false); } connection.setDoInput(!Void.class.equals(modelClass)); try { connection.connect(); if (bytes != null) { try (OutputStream os = connection.getOutputStream()) { os.write(bytes); } } int status = connection.getResponseCode(); if (status / 100 == 2) { if (Void.class.equals(modelClass)) { return null; } try (InputStream is = connection.getInputStream()) { return mapper.readerFor(modelClass).readValue(is); } } throw new GiteaHttpStatusException( status, connection.getResponseMessage(), bytes != null ? new String(bytes, StandardCharsets.UTF_8) : null ); } finally { connection.disconnect(); } }
Example 19
Source File: VersionChecker.java From Item-NBT-API with MIT License | 4 votes |
protected static void checkForUpdates() throws Exception { URL url = new URL(REQUEST_URL); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.addRequestProperty("User-Agent", USER_AGENT);// Set // User-Agent // If you're not sure if the request will be successful, // you need to check the response code and use #getErrorStream if it // returned an error code InputStream inputStream = connection.getInputStream(); InputStreamReader reader = new InputStreamReader(inputStream); // This could be either a JsonArray or JsonObject JsonElement element = new JsonParser().parse(reader); if (element.isJsonArray()) { // Is JsonArray JsonArray updates = (JsonArray) element; JsonObject latest = (JsonObject) updates.get(updates.size() - 1); int versionDifference = getVersionDifference(latest.get("name").getAsString()); if (versionDifference == -1) { // Outdated MinecraftVersion.logger.log(Level.WARNING, "[NBTAPI] The NBT-API at '" + NBTItem.class.getPackage() + "' seems to be outdated!"); MinecraftVersion.logger.log(Level.WARNING, "[NBTAPI] Current Version: '" + MinecraftVersion.VERSION + "' Newest Version: " + latest.get("name").getAsString() + "'"); MinecraftVersion.logger.log(Level.WARNING, "[NBTAPI] Please update the nbt-api or the plugin that contains the api!"); } else if (versionDifference == 0) { MinecraftVersion.logger.log(Level.INFO, "[NBTAPI] The NBT-API seems to be up-to-date!"); } else if (versionDifference == 1) { MinecraftVersion.logger.log(Level.WARNING, "[NBTAPI] The NBT-API at '" + NBTItem.class.getPackage() + "' seems to be a future Version, not yet released on Spigot!"); MinecraftVersion.logger.log(Level.WARNING, "[NBTAPI] Current Version: '" + MinecraftVersion.VERSION + "' Newest Version: " + latest.get("name").getAsString() + "'"); } } else { // wut?! MinecraftVersion.logger.log(Level.WARNING, "[NBTAPI] Error when looking for Updates! Got non Json Array: '" + element.toString() + "'"); } }
Example 20
Source File: InterceptorTestBase.java From msf4j with Apache License 2.0 | 3 votes |
/** * Get java object from http url connection. * * @param urlConn http url connection * @return string from response * @throws IOException on any exception */ protected String getResponseString(HttpURLConnection urlConn) throws IOException { InputStream inputStream = urlConn.getInputStream(); String response = StreamUtil.asString(inputStream); IOUtils.closeQuietly(inputStream); return response; }