List of usage examples for java.net HttpURLConnection getInputStream
public InputStream getInputStream() throws IOException
From source file:com.github.brandtg.discovery.TestHelixServiceDiscoveryBundle.java
private static void checkServices(List<InetSocketAddress> services) throws Exception { for (InetSocketAddress service : services) { HttpURLConnection conn = (HttpURLConnection) new URL( String.format("http://%s:%d/hello-world", service.getHostName(), service.getPort())) .openConnection(); String result = IOUtils.toString(conn.getInputStream()); Assert.assertEquals(result, "Hello World!"); }/*from w w w .j av a2s.c o m*/ }
From source file:com.greenline.guahao.biz.manager.payment.util.AlipayCore.java
/** * ??ATN/* w w w . j a v a2 s . c o m*/ * * @param urlvalue URL? * @return ?ATN ? invalid?? ?partnerkey? true * ? false ?????? */ private static String checkUrl(String urlvalue) { String inputLine = ""; try { URL url = new URL(urlvalue); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); inputLine = in.readLine(); } catch (Exception e) { log.error(e); inputLine = ""; } return inputLine; }
From source file:Hib.ControllerInsertJSON.java
static void doThat() throws MalformedURLException, JSONException { URL Url = new URL("http://api.wunderground.com/api/4228dd85f026caea/conditions/q/Colorado/COS.json"); // URL Url = new URL("http://api.wunderground.com/api/4228dd85f026caea/conditions/q/Mexico/Cancun.json"); //This next URL is still being played with. Some of the formatting is hard to figure out, but Wunderground //writes a perfectly formatted JSON file that is easy to read with Java. // URL Url = new URL("https://api.darksky.net/forecast/08959bb1e2c7eae0f3d1fafb5d538032/38.886,-104.7201"); try {//w w w . ja v a 2s.co m HttpURLConnection urlCon = (HttpURLConnection) Url.openConnection(); // This part will read the data returned thru HTTP and load it into memory // I have this code left over from my CIT260 project. InputStream stream = urlCon.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); StringBuilder result = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { result.append(line); } // The next lines read certain parts of the JSON data and print it out on the screen //Creates the JSONObject object and loads the JSON file from the URLConnection //Into a StringWriter object. I am printing this out in raw format just so I can see it doing something JSONObject json = new JSONObject(result.toString()); JSONObject coloradoInfo = (JSONObject) json.get("current_observation"); StringWriter out = new StringWriter(); json.write(out); String jsonTxt = json.toString(); System.out.print(jsonTxt); // List<String> list = new ArrayList<>(); // JSONArray array = json.getJSONArray(jsonTxt); // System.out.print(jsonTxt); DB_jason_json person = new DB_jason_json(); person.setJson_id(coloradoInfo.getString("temperature_string")); person.setLax_json(coloradoInfo.getJSONObject("display_location").getString("city")); person.setStrict_json(coloradoInfo.getJSONObject("display_location").getString("state_name")); person.setUnique_json(coloradoInfo.getString("wind_string")); Model.addPerson(person); // session.flush(); //session.close(); // System.out.println(person); System.out.println("JSON ADDED!!!!!"); } catch (IOException e) { System.out.println("***ERROR*******************ERROR********************. " + "\nURL: " + Url.toString() + "\nERROR: " + e.toString()); // for (int i =0;i<array.length();i++){ //list.add(array.getJSONObject(i).getString("current_observation")); //} } }
From source file:com.socialize.util.ImageUtils.java
public static Bitmap getBitmapFromURL(String src) { Bitmap myBitmap = null;//from ww w . ja va 2s.c om HttpURLConnection connection = null; try { URL url = new URL(src); connection = (HttpURLConnection) url.openConnection(); connection.setDoInput(true); connection.connect(); InputStream input = connection.getInputStream(); myBitmap = BitmapFactory.decodeStream(input); } catch (IOException e) { e.printStackTrace(); myBitmap = null; } finally { try { connection.disconnect(); } catch (Exception ignored) { } } return myBitmap; }
From source file:io.hops.hopsworks.api.tensorflow.TensorboardProxyServlet.java
public static String getHTML(String urlToRead) throws Exception { StringBuilder result = new StringBuilder(); URL url = new URL(urlToRead); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line;/* w ww . ja va 2 s. c o m*/ while ((line = rd.readLine()) != null) { result.append(line); } rd.close(); conn.disconnect(); return result.toString(); }
From source file:Main.java
public static String getRequest(String query) { HttpURLConnection connection = null; try {/*from w ww . j av a 2 s. co m*/ connection = (HttpURLConnection) new URL(url + query).openConnection(); connection.setRequestMethod("GET"); connection.setRequestProperty("Accept-Charset", charset); statusCode = connection.getResponseCode(); if (statusCode != 200) { return null; } InputStream response = connection.getInputStream(); BufferedReader bR = new BufferedReader(new InputStreamReader(response)); String line = ""; StringBuilder responseStrBuilder = new StringBuilder(); while ((line = bR.readLine()) != null) { responseStrBuilder.append(line); } response.close(); return responseStrBuilder.toString(); } catch (IOException e) { e.printStackTrace(); } return ""; }
From source file:Main.java
public static String getHttpText(String urlInfo, String userName, String password) throws MalformedURLException, IOException { URL url = null;/*w w w.java2s . c o m*/ String tempStr = null; url = new URL(urlInfo); HttpURLConnection huc = null; String credit = userName + ":" + password; String encoding = new sun.misc.BASE64Encoder().encode(credit.getBytes()); StringBuffer sb = new StringBuffer(); huc = (HttpURLConnection) url.openConnection(); huc.setAllowUserInteraction(false); huc.setRequestProperty("Authorization", "Basic " + encoding); BufferedReader br = new BufferedReader(new InputStreamReader(huc.getInputStream(), "utf-8")); String line = null; while ((line = br.readLine()) != null) { sb.append(line).append("\n"); } tempStr = sb.toString(); return tempStr; }
From source file:Main.java
public static JSONObject postRequest(String query) { HttpURLConnection connection = null; try {//from w w w . jav a 2 s . c o m connection = (HttpURLConnection) new URL(url + query).openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Accept-Charset", charset); statusCode = connection.getResponseCode(); if (statusCode != 200) { return null; } InputStream response = connection.getInputStream(); BufferedReader bR = new BufferedReader(new InputStreamReader(response)); String line = ""; StringBuilder responseStrBuilder = new StringBuilder(); while ((line = bR.readLine()) != null) { responseStrBuilder.append(line); } response.close(); return new JSONObject(responseStrBuilder.toString()); } catch (IOException | JSONException e) { e.printStackTrace(); } return new JSONObject(); }
From source file:com.mk4droid.IMC_Services.Download_Data.java
/** * Download Image from a certain url// www. ja v a 2s . c o m * * @param fullPath the url of the image * @return */ public static byte[] Down_Image(String fullPath) { try { //----- Split---- String[] AllInfo = fullPath.split("/"); // Encode filename as UTF8 ------- String fnExt = AllInfo[AllInfo.length - 1]; String fnExt_UTF8 = URLEncoder.encode(fnExt, "UTF-8"); //- Replace new fn to old AllInfo[AllInfo.length - 1] = fnExt_UTF8; //------ Concatenate to a single string ----- String newfullPath = AllInfo[0]; for (int i = 1; i < AllInfo.length; i++) newfullPath += "/" + AllInfo[i]; // empty space becomes + after UTF8, then replace with %20 newfullPath = newfullPath.replace("+", "%20"); //------------ Download ------------- URL myFileUrl = new URL(newfullPath); HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection(); conn.setDoInput(true); conn.setConnectTimeout(10000); conn.connect(); InputStream isBitmap = conn.getInputStream(); return readBytes(isBitmap); } catch (Exception e) { Log.e(Constants_API.TAG, "Download_Data: Down_Image: Error in http connection " + e.getMessage()); return null; } }
From source file:Main.java
public static Bitmap getImageFromURL(URL url) { Bitmap bitmap = null;/*from w w w.j a va2 s.c o m*/ HttpURLConnection connection = null; try { connection = (HttpURLConnection) url.openConnection(); if (connection.getResponseCode() != 200) { return null; } if (!CONTENT_TYPE_IMAGE.equalsIgnoreCase(connection.getContentType().substring(0, 5))) { return null; } bitmap = BitmapFactory.decodeStream(connection.getInputStream()); } catch (IOException e) { e.printStackTrace(); } if (connection != null) { connection.disconnect(); } return bitmap; }