List of usage examples for java.net HttpURLConnection getInputStream
public InputStream getInputStream() throws IOException
From source file:com.beust.android.translate.Translate.java
/** * Forms an HTTP request and parses the response for a translation. * * @param text The String to translate./*from ww w .j a v a 2 s.co m*/ * @param from The language code to translate from. * @param to The language code to translate to. * @return The translated String. * @throws Exception */ private static String retrieveTranslation(String text, String from, String to) throws Exception { try { StringBuilder url = new StringBuilder(); url.append(URL_STRING).append(from).append("%7C").append(to); url.append(TEXT_VAR).append(URLEncoder.encode(text, ENCODING)); Log.d(TranslateService.TAG, "Connecting to " + url.toString()); HttpURLConnection uc = (HttpURLConnection) new URL(url.toString()).openConnection(); uc.setDoInput(true); uc.setDoOutput(true); try { Log.d(TranslateService.TAG, "getInputStream()"); InputStream is = uc.getInputStream(); String result = toString(is); JSONObject json = new JSONObject(result); return ((JSONObject) json.get("responseData")).getString("translatedText"); } finally { // http://java.sun.com/j2se/1.5.0/docs/guide/net/http-keepalive.html uc.getInputStream().close(); if (uc.getErrorStream() != null) uc.getErrorStream().close(); } } catch (Exception ex) { throw ex; } }
From source file:Main.java
public static String readPeopleData(String uri) { URL url;//from w w w . j av a2 s . c o m StringBuffer jsonstring = null; HttpURLConnection connection; Logger.getAnonymousLogger().info("Getting data for " + uri); try { url = new URL(uri); connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(1000 * 5); InputStreamReader is = new InputStreamReader(connection.getInputStream()); BufferedReader buff = new BufferedReader(is); jsonstring = new StringBuffer(); String line = ""; do { line = buff.readLine(); if (line != null) jsonstring.append(line); } while (line != null); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } Logger.getAnonymousLogger().info("Returning Data " + jsonstring); if (null != jsonstring) { return jsonstring.toString().trim(); } else { return null; } }
From source file:Main.java
private static String httpGet(String address) { InputStream inputStream = null; HttpURLConnection httpURLConnection = null; try {/* w w w . j ava 2s .c o m*/ URL url = new URL(address); httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.connect(); if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) { inputStream = httpURLConnection.getInputStream(); int len = 0; byte[] buffer = new byte[1024]; StringBuffer stringBuffer = new StringBuffer(); while ((len = inputStream.read(buffer)) != -1) { stringBuffer.append(new String(buffer, 0, len)); } return stringBuffer.toString(); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { close(inputStream); if (httpURLConnection != null) { httpURLConnection.disconnect(); } } return null; }
From source file:net.daporkchop.porkselfbot.util.HTTPUtils.java
/** * Performs a GET request to the specified URL and returns the result. * <p />// www .j a v a 2 s. c o m * The response will be parsed as UTF-8. * If the server returns an error but still provides a body, the body will be returned as normal. * If the server returns an error without any body, a relevant {@link java.io.IOException} will be thrown. * * @param url URL to submit the GET request to * @return Raw text response from the server * @throws IOException The request was not successful */ public static String performGetRequest(final URL url) throws IOException { Validate.notNull(url); final HttpURLConnection connection = createUrlConnection(url); InputStream inputStream = null; try { inputStream = connection.getInputStream(); final String result = IOUtils.toString(inputStream, Charsets.UTF_8); return result; } catch (final IOException e) { IOUtils.closeQuietly(inputStream); inputStream = connection.getErrorStream(); if (inputStream != null) { final String result = IOUtils.toString(inputStream, Charsets.UTF_8); return result; } else { throw e; } } finally { IOUtils.closeQuietly(inputStream); } }
From source file:Main.java
static public String download_json(String url_addr) { StringBuilder result = new StringBuilder(); try {// w w w . j a va2 s . c o m URL url = new URL(url_addr); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); if (conn != null) { conn.setConnectTimeout(10000); conn.setUseCaches(false); if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) { BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); for (;;) { String line = br.readLine(); if (line == null) break; result.append(line + '\n'); } br.close(); } else { conn.disconnect(); return null; } conn.disconnect(); } } catch (Exception e) { Log.e(TAG, e.toString()); return null; } return result.toString(); }
From source file:com.andrious.btc.data.jsonUtils.java
private static InputStreamReader getInputStream(String urlString) throws IOException { HttpURLConnection conn = urlConnect(urlString); return new InputStreamReader(conn.getInputStream()); }
From source file:com.ettoremastrogiacomo.sktradingjava.starters.Temp.java
public static void fetchEuroNext() throws Exception { String u0 = "https://www.euronext.com/en/equities/directory"; com.ettoremastrogiacomo.utils.HttpFetch httpf = new com.ettoremastrogiacomo.utils.HttpFetch(); if (Init.use_http_proxy.equals("true")) { httpf.setProxy(Init.http_proxy_host, Integer.parseInt(Init.http_proxy_port), Init.http_proxy_user, Init.http_proxy_password); }// w w w. j a v a 2 s . c o m String s = new String(httpf.HttpGetUrl(u0, Optional.empty(), Optional.empty())); int k1 = s.indexOf("\\/en\\/popup\\/data\\/download?"); int k2 = s.indexOf("\"", k1); String u1 = s.substring(k1, k2 - 1); //LOG.debug(u1); u1 = u1.replace("\\u0026", "&"); u1 = "https://www.euronext.com" + u1.replace("/", ""); u1 = u1.replace("\\", "/"); LOG.debug(u1); s = new String(httpf.HttpGetUrl(u1, Optional.empty(), Optional.empty())); Document doc = Jsoup.parse(s); java.util.HashMap<String, String> vmap = new java.util.HashMap<>(); vmap.put("format", "1"); vmap.put("layout", "2"); vmap.put("decimal_separator", "1"); vmap.put("date_format", "1"); vmap.put("op", "Go"); Elements links = doc.select("input[name=\"form_build_id\"]"); links.forEach((x) -> { vmap.put("form_build_id", x.attr("value")); }); links = doc.select("input[name=\"form_id\"]"); links.forEach((x) -> { vmap.put("form_id", x.attr("value")); }); HttpURLConnection post = httpf.sendPostRequest(u1, vmap); StringBuffer response; try (BufferedReader in = new BufferedReader(new InputStreamReader(post.getInputStream()))) { String inputLine; response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append("\n").append(inputLine); } } String res = response.toString(); String[] lines = res.split("\n"); for (String line : lines) { String[] row = line.split("\t"); if (row.length == 13) { LOG.debug(row[0] + "\t" + row[1] + "\t" + row[2] + "\t" + row[3] + "\t" + row[4] + "\t" + row[5]); } } }
From source file:com.whp.android.location.geocode.GeocodeHelper.java
/** * getAddresses/*from w w w . j a v a 2 s . com*/ * * @param url * @return */ public static GeocodeResult getAddresses(StringBuilder url) { GeocodeResult result = null; try { HttpURLConnection httpURLConnection = null; StringBuilder stringBuilder = new StringBuilder(); httpURLConnection = (HttpURLConnection) new URL(url.toString()).openConnection(); InputStreamReader inputStreamReader = new InputStreamReader(httpURLConnection.getInputStream()); int read; char[] buff = new char[1024]; while ((read = inputStreamReader.read(buff)) != -1) { stringBuilder.append(buff, 0, read); } httpURLConnection.disconnect(); JSONObject jObject = new JSONObject(stringBuilder.toString()); result = GeocodeJsonParser.parse(jObject); } catch (Exception e) { e.printStackTrace(); } return result; }
From source file:io.github.retz.scheduler.MesosHTTPFetcher.java
static List<Map<String, Object>> fetchTasks(String master, String frameworkId, int offset, int limit) throws MalformedURLException { URL url = new URL("http://" + master + "/tasks?offset=" + offset + "&limit=" + limit); HttpURLConnection conn; try {/*from ww w. ja v a 2s . c o m*/ conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setDoOutput(true); return parseTasks(conn.getInputStream(), frameworkId); } catch (IOException e) { return new LinkedList<>(); } }
From source file:Main.java
public static String jsonGetRequest(String urlQueryString) { String json = null;/* w w w. ja v a2 s . co m*/ try { URL url = new URL(urlQueryString); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setInstanceFollowRedirects(false); connection.setRequestMethod("GET"); connection.setRequestProperty("Content-Type", "application/json"); connection.setRequestProperty("charset", "utf-8"); connection.connect(); InputStream inStream = connection.getInputStream(); json = streamToString(inStream); // input stream to string } catch (IOException ex) { ex.printStackTrace(); } return json; }