List of usage examples for java.net URL openConnection
public URLConnection openConnection() throws java.io.IOException
From source file:cz.mgn.mediservice.rest.Loader.java
protected static String load(String request) { String result = null;/*from ww w . j ava 2 s. c o m*/ try { URL url = new URL(SERVER_URL + "?" + request); URLConnection con = url.openConnection(); result = IOUtils.toString(con.getInputStream(), "UTF-8"); } catch (IOException ex) { Logger.getLogger(Loader.class.getName()).log(Level.SEVERE, null, ex); } return result; }
From source file:emily.command.fun.CatFactCommand.java
public static String getCatFact() { try {//from w ww. ja v a 2 s . com URL loginurl = new URL("https://catfact.ninja/fact"); URLConnection yc = loginurl.openConnection(); yc.setConnectTimeout(10 * 1000); BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream())); String inputLine = in.readLine(); JsonParser parser = new JsonParser(); JsonObject array = parser.parse(inputLine).getAsJsonObject(); return ":cat: " + array.get("fact").getAsString(); } catch (Exception e) { System.out.println(e); } return null; }
From source file:Main.java
public static String getResponseInFile(String address, File file) throws IOException { String dataString = ""; InputStream inputStream = null; OutputStream outputStream = null; try {// w w w. j a v a 2 s . c om URL url = new URL(address); URLConnection connection = url.openConnection(); inputStream = connection.getInputStream(); byte[] data; if (inputStream.available() > BUFFER_SIZE) { data = new byte[BUFFER_SIZE]; } else { data = new byte[inputStream.available()]; } int dataCount; while ((dataCount = inputStream.read(data)) > 0) { outputStream.write(data, 0, dataCount); } OutputStream output = new OutputStream() { private StringBuilder string = new StringBuilder(); @Override public void write(int b) throws IOException { this.string.append((char) b); } //Netbeans IDE automatically overrides this toString() public String toString() { return this.string.toString(); } }; output.write(data); dataString = output.toString(); } finally { try { inputStream.close(); } catch (Exception e) { } try { outputStream.close(); } catch (Exception e) { } } return dataString; }
From source file:com.prodyna.liferay.devcon.hystrix.demo.service.ChuckNorrisFactsServiceClient.java
/** * Call Chuck Norris facts service using simple {@link HttpURLConnection}. * //from w ww . j a v a2 s . co m * @return Fact about Chuck Norris. * @throws IOException * is thrown if the service could not be called for some reason. */ public static String callChuckNorrisFactsService() throws IOException { URL url = new URL(CHUCK_NORRIS_SERVICE_ENDPOINT); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); InputStream inputStream = connection.getInputStream(); String response = IOUtils.toString(inputStream); JSONObject jsonResponse = new JSONObject(response); String chuckNorrisFact = jsonResponse.getString("fact"); return chuckNorrisFact; }
From source file:editor.util.URLUtil.java
public static String readText(String uri) { String res = ""; try {//from ww w . ja v a 2 s . c om java.net.URL url = new java.net.URL(uri); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); InputStream in = connection.getInputStream(); byte buff[] = new byte[200]; int ch; while ((ch = in.read(buff)) != -1) res += new String(buff, 0, ch); in.close(); return res; } catch (Exception e) { return e.getLocalizedMessage(); } }
From source file:Main.java
/** Downloads a file from the specified URL and stores the file to the specified location * @param fileUrl the URL from which the file should be downloaded * @param storageLocation the location to which the downloaded file should be stored. If the file exists, it will * be overridden! /*from www .ja va 2s. c om*/ * @throws IOException */ public static void downloadFileFromWebserver(String fileUrl, String storageLocation) throws IOException { URL url = new URL(fileUrl); File file = new File(storageLocation); URLConnection urlConnection = url.openConnection(); InputStream inputStream = urlConnection.getInputStream(); BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream); FileOutputStream fileOutputStream = new FileOutputStream(file); byte[] buffer = new byte[1024]; int bytesInBuffer = 0; while ((bytesInBuffer = bufferedInputStream.read(buffer)) != -1) { fileOutputStream.write(buffer, 0, bytesInBuffer); } fileOutputStream.close(); }
From source file:Yak_Hax.Yak_Hax_Mimerme.PostRequest.java
public static String PostBodyRequest(String URL, String JSONRaw, String UserAgent) throws IOException { String type = "application/json"; URL u = new URL(URL); HttpURLConnection conn = (HttpURLConnection) u.openConnection(); conn.setDoOutput(true);/*from w w w. j a v a2 s . c o m*/ conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", type); conn.setRequestProperty("User-Agent", UserAgent); OutputStream os = conn.getOutputStream(); os.write(JSONRaw.getBytes()); os.flush(); os.close(); String response = null; DataInputStream input = new DataInputStream(conn.getInputStream()); while (null != ((response = input.readLine()))) { input.close(); return response; } return null; }
From source file:Main.java
public static String post(String url, Map<String, String> params) { try {//w ww .ja va 2 s .c om URL u = new URL(url); HttpURLConnection connection = (HttpURLConnection) u.openConnection(); connection.setDoInput(true); connection.setDoOutput(true); connection.setRequestMethod("POST"); connection.setUseCaches(false); PrintWriter pw = new PrintWriter(connection.getOutputStream()); StringBuilder sbParams = new StringBuilder(); if (params != null) { for (String key : params.keySet()) { sbParams.append(key + "=" + params.get(key) + "&"); } } if (sbParams.length() > 0) { String strParams = sbParams.substring(0, sbParams.length() - 1); Log.e("cat", "strParams:" + strParams); pw.write(strParams); pw.flush(); pw.close(); } BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream())); StringBuffer response = new StringBuffer(); String readLine = ""; while ((readLine = br.readLine()) != null) { response.append(readLine); } br.close(); return response.toString(); } catch (Exception e) { e.printStackTrace(); return null; } }
From source file:Main.java
public static int sendMessage(String auth_token, String registrationId, String message) throws IOException { StringBuilder postDataBuilder = new StringBuilder(); postDataBuilder.append(PARAM_REGISTRATION_ID).append("=").append(registrationId); postDataBuilder.append("&").append(PARAM_COLLAPSE_KEY).append("=").append("0"); postDataBuilder.append("&").append("data.payload").append("=") .append(URLEncoder.encode("Lars war hier", UTF8)); byte[] postData = postDataBuilder.toString().getBytes(UTF8); // Hit the dm URL. URL url = new URL("https://android.clients.google.com/c2dm/send"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true);//from w w w .ja v a2 s . c o m conn.setUseCaches(false); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8"); conn.setRequestProperty("Content-Length", Integer.toString(postData.length)); conn.setRequestProperty("Authorization", "GoogleLogin auth=" + auth_token); OutputStream out = conn.getOutputStream(); out.write(postData); out.close(); int responseCode = conn.getResponseCode(); return responseCode; }
From source file:Main.java
@Nullable private static InputStream getStreamFromUri(Uri selectedImageURI, Context theContext) throws IOException { switch (selectedImageURI.getScheme()) { case "content": return theContext.getContentResolver().openInputStream(selectedImageURI); case "file": return new FileInputStream(new File(URI.create(selectedImageURI.toString()))); case "http": // Fall through case "https": final URL url = new URL(selectedImageURI.toString()); final HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoInput(true);/*from w w w.ja v a 2s . co m*/ connection.connect(); return connection.getInputStream(); default: Log.w(TAG, "getStreamFromUri(): unsupported Uri scheme: " + selectedImageURI.getScheme()); return null; } }