List of usage examples for java.net HttpURLConnection getInputStream
public InputStream getInputStream() throws IOException
From source file:Main.java
/** * This method fetches data from a given url * * @param strUrl Url from which the data will be fetched * @return A String representing the resource obtained in the connection * @throws IOException If something went wrong with the connection */// w w w . j av a 2 s . c o m public static String getDataFromUrl(String strUrl) throws IOException { InputStream iStream; HttpURLConnection urlConnection; URL url = new URL(strUrl); // Creating an http connection to communicate with url urlConnection = (HttpURLConnection) url.openConnection(); // Connecting to url urlConnection.connect(); // Reading data from url iStream = urlConnection.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(iStream)); StringBuilder sb = new StringBuilder(); String line; while ((line = br.readLine()) != null) { sb.append(line); } br.close(); iStream.close(); urlConnection.disconnect(); return sb.toString(); }
From source file:com.google.cloud.runtime.jetty.util.HttpUrlUtil.java
/** * Obtain the text (non-binary) response body from an {@link HttpURLConnection}, * using the response provided charset.//from w w w .j a v a 2 s .c o m * <p> * Note: Normal HttpURLConnection doesn't use the provided charset properly. * </p> * * @param http the {@link HttpURLConnection} to obtain the response body from * @return the text of the response body * @throws IOException if unable to get the text of the response body */ public static String getResponseBody(HttpURLConnection http) throws IOException { Charset responseEncoding = StandardCharsets.UTF_8; if (http.getContentEncoding() != null) { responseEncoding = Charset.forName(http.getContentEncoding()); } return IOUtils.toString(http.getInputStream(), responseEncoding); }
From source file:Main.java
public static String getJsonContent(String url_path) { try {//from w w w .ja v a 2 s.co m URL url = new URL(url_path); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(3000); connection.setRequestMethod("GET"); connection.setDoInput(true); int code = connection.getResponseCode(); if (code == 200) { return changeInputStream(connection.getInputStream()); } } catch (Exception e) { } return ""; }
From source file:Main.java
public static String customrequestget(String url, HashMap<String, String> map, String method) { if (null != map) { int i = 0; for (Map.Entry<String, String> entry : map.entrySet()) { if (i == 0) { url = url + "?" + entry.getKey() + "=" + entry.getValue(); } else { url = url + "&" + entry.getKey() + "=" + entry.getValue(); }/*from w ww .j av a 2 s . com*/ i++; } } try { URL murl = new URL(url); System.out.print(url); HttpURLConnection conn = (HttpURLConnection) murl.openConnection(); conn.setConnectTimeout(5 * 1000); conn.setRequestMethod(method); conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows XP; DigExt)"); InputStream inStream = conn.getInputStream(); String result = inputStream2String(inStream); conn.disconnect(); return result; } catch (Exception e) { // TODO: handle exception } return null; }
From source file:com.arthurpitman.common.HttpUtils.java
/** * Downloads a file from a URL and saves it to the local file system. * @param url the URL to connect to.//w w w . j av a 2 s . co m * @param requestProperties optional request properties, <code>null</code> if not required. * @param outputFile local file to write to. * @throws IOException */ public static void downloadFile(final String url, final RequestProperty[] requestProperties, final File outputFile) throws IOException { for (int i = 0;; i++) { HttpURLConnection connection = null; try { connection = connectGet(url, requestProperties); StreamUtils.readStreamIntoFile(connection.getInputStream(), outputFile, BUFFER_SIZE, true); return; } catch (IOException e) { if (i == CONNECTION_RETRIES) { throw e; } } finally { // always close the connection if (connection != null) { connection.disconnect(); } } // allow recovery time try { Thread.sleep(CONNECTION_RETRY_SLEEP); } catch (InterruptedException e) { } } }
From source file:OCRRestAPI.java
private static void DownloadConvertedFile(String outputFileUrl) throws IOException { URL downloadUrl = new URL(outputFileUrl); HttpURLConnection downloadConnection = (HttpURLConnection) downloadUrl.openConnection(); if (downloadConnection.getResponseCode() == HttpURLConnection.HTTP_OK) { InputStream inputStream = downloadConnection.getInputStream(); // opens an output stream to save into file FileOutputStream outputStream = new FileOutputStream("C:\\converted_file.doc"); int bytesRead = -1; byte[] buffer = new byte[4096]; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); }/*w w w . j a va2 s.c o m*/ outputStream.close(); inputStream.close(); } downloadConnection.disconnect(); }
From source file:com.itwizard.mezzofanti.Translate.java
/** * Forms an HTTP request and parses the response for a translation. * * @param text The String to translate./*w w w .j av 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("TEST", "Connecting to " + url.toString()); HttpURLConnection uc = (HttpURLConnection) new URL(url.toString()).openConnection(); uc.setDoInput(true); uc.setDoOutput(true); try { Log.d("TEST", "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
private static String connect(String uri, String charsetName) { String result = ""; try {/*from www. j a va 2s . co m*/ URL url = new URL(uri); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(5 * 1000); if (Integer.parseInt(Build.VERSION.SDK) < 8) { System.setProperty("http.keepAlive", "false"); } if (conn.getResponseCode() == 200) { InputStream is = conn.getInputStream(); result = readData(is, charsetName); } conn.disconnect(); } catch (MalformedURLException e) { } catch (IOException e) { } catch (Exception e) { } return result; }
From source file:com.android.feedmeandroid.HTTPClient.java
public static Bitmap downloadFile(final String fileUrl) { final Bitmap[] bitmap = new Bitmap[1]; Thread t = new Thread(new Runnable() { public void run() { URL myFileUrl = null; try { myFileUrl = new URL(fileUrl); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace();//from w ww . j a v a 2 s. com } try { HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection(); conn.setDoInput(true); conn.connect(); InputStream is = conn.getInputStream(); Bitmap bmImg = BitmapFactory.decodeStream(is); bitmap[0] = bmImg; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); t.start(); try { t.join(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } return bitmap[0]; }
From source file:com.mboarder.util.Translate.java
/** * Forms an HTTP request and parses the response for a translation. * * @param text The String to translate.// w w w .ja 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(TAG, "Connecting to " + url.toString()); HttpURLConnection uc = (HttpURLConnection) new URL(url.toString()).openConnection(); uc.setDoInput(true); uc.setDoOutput(true); try { Log.d(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; } }