List of usage examples for java.net URLConnection getInputStream
public InputStream getInputStream() throws IOException
From source file:com.tbs.devcorner.simple.App.java
public static void main(String[] args) { try {/* ww w . j a v a 2 s .c o m*/ // Build Connection URL api_url = new URL("http://www.earthquakescanada.nrcan.gc.ca/api/earthquakes/"); URLConnection api = api_url.openConnection(); // Set HTTP Headers api.setRequestProperty("Accept", "application/json"); api.setRequestProperty("Accept-Language", "en"); // Get Response JSONTokener tokener = new JSONTokener(api.getInputStream()); JSONObject jsondata = new JSONObject(tokener); // Display API name System.out.println(jsondata.getJSONObject("metadata").getJSONObject("request").getJSONObject("name") .get("en").toString()); // Iterate over latest links JSONObject latest = jsondata.getJSONObject("latest"); for (Object item : latest.keySet()) { System.out.println(item.toString() + " -> " + latest.get(item.toString())); } } catch (MalformedURLException e) { System.out.println("Malformed URL"); } catch (IOException e) { System.out.println("IO Error"); } }
From source file:Main.java
public static void main(String[] args) throws Exception { URL url = new URL("http://www.java2s.com"); URLConnection conn = url.openConnection(); conn.setDoOutput(true);/*from ww w . j a v a 2 s . c o m*/ OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream()); writer.write("value=1&anotherValue=1"); writer.flush(); String line; BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); while ((line = reader.readLine()) != null) { System.out.println(line); } writer.close(); reader.close(); }
From source file:com.tbs.devcorner.simple.App.java
public static void main(String[] args) { try {//ww w .ja va 2s.c o m // Crer la connexion URL api_url = new URL("http://www.earthquakescanada.nrcan.gc.ca/api/earthquakes/"); URLConnection api = api_url.openConnection(); // Dfinir les en-ttes HTTP api.setRequestProperty("Accept", "application/json"); api.setRequestProperty("Accept-Language", "fr"); // Obtenir la rponse JSONTokener tokener = new JSONTokener(api.getInputStream()); JSONObject jsondata = new JSONObject(tokener); // Afficher le nom de lAPI System.out.println(jsondata.getJSONObject("metadata").getJSONObject("request").getJSONObject("name") .get("fr").toString()); // Rpter lopration sur les liens les plus rcents JSONObject latest = jsondata.getJSONObject("latest"); for (Object item : latest.keySet()) { System.out.println(item.toString() + " -> " + latest.get(item.toString())); } } catch (MalformedURLException e) { System.out.println("URL mal forme"); } catch (IOException e) { System.out.println("Erreur dE/S"); } }
From source file:MainClass.java
public static void main(String args[]) throws Exception { URL u = new URL("http://www.java2s.com/binary.dat"); URLConnection uc = u.openConnection(); String contentType = uc.getContentType(); int contentLength = uc.getContentLength(); if (contentType.startsWith("text/") || contentLength == -1) { throw new IOException("This is not a binary file."); }/*w w w . ja v a 2 s .c o m*/ InputStream raw = uc.getInputStream(); InputStream in = new BufferedInputStream(raw); byte[] data = new byte[contentLength]; int bytesRead = 0; int offset = 0; while (offset < contentLength) { bytesRead = in.read(data, offset, data.length - offset); if (bytesRead == -1) break; offset += bytesRead; } in.close(); if (offset != contentLength) { throw new IOException("Only read " + offset + " bytes; Expected " + contentLength + " bytes"); } String filename = u.getFile().substring(filename.lastIndexOf('/') + 1); FileOutputStream out = new FileOutputStream(filename); out.write(data); out.flush(); out.close(); }
From source file:Reverse.java
public static void main(String[] args) throws Exception { if (args.length != 1) { System.err.println("Usage: java Reverse " + "string_to_reverse"); System.exit(1);/* ww w .j a v a 2s. com*/ } String stringToReverse = URLEncoder.encode(args[0]); URL url = new URL("http://java.sun.com/cgi-bin/backwards"); URLConnection connection = url.openConnection(); connection.setDoOutput(true); PrintWriter out = new PrintWriter(connection.getOutputStream()); out.println("string=" + stringToReverse); out.close(); BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String inputLine; while ((inputLine = in.readLine()) != null) System.out.println(inputLine); in.close(); }
From source file:com.doculibre.constellio.lang.html.HtmlLangDetector.java
public static void main(String[] args) throws Exception { //java.net.URL url = new java.net.URL("http://www.gouv.qc.ca"); java.net.URL url = new java.net.URL("http://andrew.triumf.ca/multilingual/samples/french.meta.html"); java.net.URLConnection connection = url.openConnection(); java.io.InputStream is = null; String content = null;//ww w . jav a 2s.c o m try { is = connection.getInputStream(); content = IOUtils.toString(is); } finally { IOUtils.closeQuietly(is); } HtmlLangDetector langDetector = new HtmlLangDetector(); System.out.println(langDetector.getLang(content)); }
From source file:Main.java
public static void main(String args[]) throws Exception { String fullURL = args[0];/*w w w . j a v a 2 s .c o m*/ URL u = new URL(fullURL); URLConnection conn = u.openConnection(); conn.setDoInput(true); OutputStream theControl = conn.getOutputStream(); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(theControl)); for (int i = 1; i < args.length; i++) { out.write(args[i] + "\n"); } out.close(); InputStream theData = conn.getInputStream(); String contentType = conn.getContentType(); if (contentType.toLowerCase().startsWith("text")) { BufferedReader in = new BufferedReader(new InputStreamReader(theData)); String line; while ((line = in.readLine()) != null) { System.out.println(line); } } }
From source file:Main.java
public static void main(String args[]) throws Exception { String fullURL = args[0];//from w w w .j av a 2s. c o m URL u = new URL(fullURL); URLConnection conn = u.openConnection(); conn.setDoOutput(true); OutputStream theControl = conn.getOutputStream(); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(theControl)); for (int i = 1; i < args.length; i++) { out.write(args[i] + "\n"); } out.close(); InputStream theData = conn.getInputStream(); String contentType = conn.getContentType(); if (contentType.toLowerCase().startsWith("text")) { BufferedReader in = new BufferedReader(new InputStreamReader(theData)); String line; while ((line = in.readLine()) != null) { System.out.println(line); } } }
From source file:org.dlut.mycloudserver.service.performancemonitor.PerformanceListener.java
public static void main(String[] args) { long t1 = System.currentTimeMillis(); String url = "http://127.0.0.1:8001"; URLConnection conn; try {/*from ww w .jav a2 s . c om*/ conn = new URL(url).openConnection(); conn.setConnectTimeout(1000); conn.setReadTimeout(1000); InputStream is = conn.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String res = br.readLine(); br.close(); is.close(); JSONObject jsonRes = JSON.parseObject(res); double loadAverage = jsonRes.getDoubleValue("loadAverage"); System.out.println(loadAverage); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } long t2 = System.currentTimeMillis(); System.out.println((t2 - t1) + "ms"); }
From source file:geocodingissues.Main.java
/** * @param args the command line arguments *//*from ww w . j a va2s.co m*/ public static void main(String[] args) throws JSONException { Main x = new Main(); ResultSet rs = null; x.establishConnection(); //x.addColumns(); //already did this rs = x.getLatLong(); int id; double latitude; double longitude; String req; String street_no; String street; String neighborhood; String locality; String PC; JSONObject jObject; JSONArray resultArray; JSONArray compArray; try { while (rs.next()) { id = rs.getInt("id"); latitude = rs.getDouble("latitude"); longitude = rs.getDouble("longitude"); //System.out.println("id: " + id); req = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + Double.toString(latitude) + "," + Double.toString(longitude) + "&result_type=street_address|neighborhood|locality|postal_code&key=" + key; try { URL url = new URL(req + "&sensor=false"); URLConnection conn = url.openConnection(); ByteArrayOutputStream output = new ByteArrayOutputStream(1024); IOUtils.copy(conn.getInputStream(), output); output.close(); req = output.toString(); } catch (Exception e) { System.out.println("Geocoding Error"); } if (req.contains("OVER_QUERY_LIMIT")) { System.out.println("Over Daily Query Limit"); System.exit(0); } if (!req.contains("ZERO_RESULTS")) { //System.out.println("req: "); //System.out.println(req); jObject = new JSONObject(req); resultArray = jObject.getJSONArray("results"); // Retrieve information on street address and insert into table compArray0 = resultArray.getJSONObject(0).getJSONArray("address_components"); street_no = compArray0.getJSONObject(0).getString("long_name"); street = compArray0.getJSONObject(1).getString("long_name"); x.insertValues(id, street_no, street); // Retrieve information on neighborhood and insert into table compArray1 = resultArray.getJSONObject(1).getJSONArray("address_components"); neighborhood = compArray1.getJSONObject(0).getString("long_name"); x.insertNbhd(id, neighborhood); // Retrieve information on locality and insert into table compArray2 = resultArray.getJSONObject(2).getJSONArray("address_components"); locality = compArray2.getJSONObject(0).getString("long_name"); x.insertLocality(id, locality); // Retrieve information on postal code and insert into table compArray3 = resultArray.getJSONObject(3).getJSONArray("address_components"); PC = compArray3.getJSONObject(0).getString("long_name"); x.insertPC(id, PC); } } } catch (Exception e) { System.out.println("Problem when updating the database."); } x.closeConnection(); }