List of usage examples for java.net HttpURLConnection getInputStream
public InputStream getInputStream() throws IOException
From source file:Main.java
/** * Send the Http's request with the address of url * @param String url//from w w w . jav a 2 s.com * @return String */ public static String getHttpRequest(String url) { //should use the StringBuilder or StringBuffer, String is not used. StringBuffer jsonContent = new StringBuffer(); try { URL getUrl = new URL(url); HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection(); connection.connect(); //Getting the inputting stream, and then read the stream. BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String lines = ""; while ((lines = reader.readLine()) != null) { jsonContent.append(lines); } reader.close(); connection.disconnect(); } catch (Exception e) { e.printStackTrace(); } //ScanningActivity.log("getHttpRequest(): " + content); //BooksPutIn.log("getHttpRequest(): " + content); return jsonContent.toString(); }
From source file:com.dianping.phoenix.dev.core.tools.generator.stable.GitRepositoryListGenerator.java
private static String curl(String url) throws Exception { URL reqUrl = new URL(url); HttpURLConnection conn = (HttpURLConnection) reqUrl.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(3000);/*from w w w. j a v a 2 s .c o m*/ System.out.println(url); return IOUtils.toString(new InputStreamReader(conn.getInputStream(), "UTF-8")); }
From source file:dev.meng.wikidata.util.http.HttpUtils.java
public static JSONObject queryForJSONResponse(URL url, String encoding) throws ProtocolException, IOException, StringConvertionException { JSONObject response = null;/*w w w . ja va2 s .co m*/ HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.connect(); if (connection.getResponseCode() == 200) { response = new JSONObject(StringUtils.inputStreamToString(connection.getInputStream(), encoding)); } else { throw new IOException("Error in opening: " + url + ", " + connection.getResponseCode() + " " + connection.getResponseMessage()); } return response; }
From source file:com.isol.app.tracker.Utilita.java
public static String getHttpData(String url) throws IOException, MalformedURLException { HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); InputStream in = conn.getInputStream(); try {//ww w. jav a2 s .co m StringBuilder sb = new StringBuilder(); BufferedReader r = new BufferedReader(new InputStreamReader(new DoneHandlerInputStream(in))); for (String line = r.readLine(); line != null; line = r.readLine()) { sb.append(line); } r.close(); return sb.toString(); } finally { in.close(); } }
From source file:me.prokopyl.commandtools.migration.UUIDFetcher.java
static public Map<String, UUID> fetch(List<String> names) throws IOException { Map<String, UUID> uuidMap = new HashMap<String, UUID>(); HttpURLConnection connection = createConnection(); writeBody(connection, names);//from w w w .jav a 2s . co m JSONArray array; try { array = (JSONArray) jsonParser.parse(new InputStreamReader(connection.getInputStream())); } catch (ParseException ex) { throw new IOException("Invalid response from server, unable to parse received JSON : " + ex.toString()); } for (Object profile : array) { JSONObject jsonProfile = (JSONObject) profile; String id = (String) jsonProfile.get("id"); String name = (String) jsonProfile.get("name"); uuidMap.put(name, fromMojangUUID(id)); } return uuidMap; }
From source file:Main.java
public static int getRequest(String link) throws IOException { URL url = new URL(link); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); System.out.println(connection.getResponseCode()); if (connection.getResponseCode() == HttpURLConnection.HTTP_OK || connection.getResponseCode() == 302) { InputStream is = connection.getInputStream(); setParsedString(convertInputStreamToString(is)); }/*from w ww . j av a 2 s.com*/ return connection.getResponseCode(); }
From source file:Main.java
public static Reader getUri(URL url) throws IOException { //Log.d(TAG, "getUri: " + url.toString()); boolean useGzip = false; HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(30 * 1000);// www.j a v a2s. c o m conn.setRequestProperty("Accept-Encoding", "gzip"); conn.connect(); InputStream in = conn.getInputStream(); final Map<String, List<String>> headers = conn.getHeaderFields(); // This is a map, but we can't assume the key we're looking for // is in normal casing. So it's really not a good map, is it? final Set<Map.Entry<String, List<String>>> set = headers.entrySet(); for (Iterator<Map.Entry<String, List<String>>> i = set.iterator(); i.hasNext();) { Map.Entry<String, List<String>> entry = i.next(); if ("Content-Encoding".equalsIgnoreCase(entry.getKey())) { for (Iterator<String> j = entry.getValue().iterator(); j.hasNext();) { String str = j.next(); if (str.equalsIgnoreCase("gzip")) { useGzip = true; break; } } // Break out of outer loop. if (useGzip) { break; } } } if (useGzip) { return new BufferedReader(new InputStreamReader(new GZIPInputStream(in)), 8 * 1024); } else { return new BufferedReader(new InputStreamReader(in), 8 * 1024); } }
From source file:software.uncharted.util.HTTPUtil.java
public static String get(String url) throws IOException { URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); // optional default is GET con.setRequestMethod("GET"); BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine;// w ww .j ava2 s. c om StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); return response.toString(); }
From source file:Main.java
public static byte[] downloadImage(final URL url) { InputStream in = null;/*from www .j a v a 2 s . c o m*/ int responseCode = -1; try { final HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setDoInput(true); con.connect(); responseCode = con.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { in = con.getInputStream(); return getByteFromInputStream(in); } } catch (final IOException e) { Log.e(LOG_TAG, "Failed to download image from: " + url.toString(), e); } return null; }
From source file:org.kegbot.app.util.Downloader.java
/** * Downloads an HTTP resource to an output file. * * @param url the resource to download * @param output the output file//ww w . j a v a 2 s . c o m * @throws IOException upon any error */ public static void downloadRaw(final String url, final File output) throws IOException { final URL destUrl = new URL(url); final HttpURLConnection connection = (HttpURLConnection) destUrl.openConnection(); final FileOutputStream out = new FileOutputStream(output); final InputStream input = connection.getInputStream(); try { byte buffer[] = new byte[4096]; int len; while ((len = input.read(buffer)) >= 0) { out.write(buffer, 0, len); } } finally { out.close(); input.close(); } }