List of usage examples for java.net HttpURLConnection getInputStream
public InputStream getInputStream() throws IOException
From source file:edu.xjtu.qxcamerabridge.LiveviewImageExtractor.java
private static InputStream getLiveviewInputStream(String liveviewURL) throws MalformedURLException, IOException { URL url = new URL(liveviewURL); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setReadTimeout(2000);//from w ww . j av a2s .co m connection.connect(); if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { return connection.getInputStream(); } return null; }
From source file:com.rumblefish.friendlymusic.api.WebRequest.java
public static Bitmap getBitmapAtURL(URL url) { InputStream inStream = null;//from w w w . j a va2 s . c o m HttpURLConnection _conn = null; Bitmap bitmap = null; try { _conn = (HttpURLConnection) url.openConnection(); _conn.setDoInput(true); _conn.connect(); inStream = _conn.getInputStream(); bitmap = BitmapFactory.decodeStream(inStream); inStream.close(); _conn.disconnect(); inStream = null; _conn = null; } catch (Exception ex) { // nothing } if (inStream != null) { try { inStream.close(); } catch (Exception ex) { } } if (_conn != null) { _conn.disconnect(); } return bitmap; }
From source file:com.oracle.jes.samples.hellostorage.IptoGeo.java
public static String getCurrentIPAddress2() throws MalformedURLException, IOException { HttpURLConnection conn = (HttpURLConnection) (new URL("http://ifconfig.me/ip").openConnection()); //1 HttpClient httpclient = HttpClientBuilder.create().build(); //1 HttpGet g1 = new HttpGet("http://ifconfig.me/ip"); //1 HttpResponse res = httpclient.execute(g1); BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); //1BufferedReader reader = new BufferedReader(new InputStreamReader(res.getEntity().getContent())); String response = reader.readLine(); if (conn.getResponseCode() == 200) { return response.trim(); } else {//from ww w.j a v a 2 s .c o m return null; } //1 return response; }
From source file:org.coffeeking.controller.service.util.ConnectedCupServiceUtils.java
public static String readResponseFromGetRequest(HttpURLConnection httpConnection) throws DeviceManagementException { BufferedReader bufferedReader; try {/*ww w .j a v a 2 s .co m*/ bufferedReader = new BufferedReader(new InputStreamReader(httpConnection.getInputStream())); } catch (IOException e) { String errorMsg = "There is an issue with connecting the reader to the input stream at: " + httpConnection.getURL(); log.error(errorMsg); throw new DeviceManagementException(errorMsg, e); } String responseLine; StringBuilder completeResponse = new StringBuilder(); try { while ((responseLine = bufferedReader.readLine()) != null) { completeResponse.append(responseLine); } } catch (IOException e) { String errorMsg = "Error occured whilst trying read from the connection stream at: " + httpConnection.getURL(); log.error(errorMsg); throw new DeviceManagementException(errorMsg, e); } try { bufferedReader.close(); } catch (IOException e) { log.error("Could not succesfully close the bufferedReader to the connection at: " + httpConnection.getURL()); } return completeResponse.toString(); }
From source file:com.hackerati.android.user_sdk.volley.HHurlStack.java
/** * Initializes an {@link HttpEntity} from the given * {@link HttpURLConnection}./*from w ww. j a v a2 s. c o m*/ * * @param connection * @return an HttpEntity populated with data from <code>connection</code>. */ private static HttpEntity entityFromConnection(final HttpURLConnection connection) { final BasicHttpEntity entity = new BasicHttpEntity(); InputStream inputStream; try { inputStream = connection.getInputStream(); } catch (final IOException ioe) { inputStream = connection.getErrorStream(); } entity.setContent(inputStream); entity.setContentLength(connection.getContentLength()); entity.setContentEncoding(connection.getContentEncoding()); entity.setContentType(connection.getContentType()); return entity; }
From source file:net.ftb.util.AppUtils.java
public static String ConnectionToString(URLConnection c) { boolean failed = false; HttpURLConnection conn = (HttpURLConnection) c; try {// w ww. jav a 2s . c o m if (conn.getErrorStream() != null) { return IOUtils.toString(conn.getErrorStream(), Charsets.UTF_8); } else if (conn.getInputStream() != null) { return IOUtils.toString(conn.getInputStream(), Charsets.UTF_8); } else { return null; } } catch (FileNotFoundException e) { // ignore this } catch (IOException e) { failed = true; } catch (Exception e) { failed = true; Logger.logDebug("failed", e); } if (failed) { try { return IOUtils.toString(c.getInputStream(), Charsets.UTF_8); } catch (Exception e) { Logger.logDebug("failed", e); } } return null; }
From source file:cit360.sandbox.BackEndMenu.java
public static final void urltest() { URL url;/*from w w w.j av a 2 s . c o m*/ HttpURLConnection urlConnection = null; try { url = new URL("http://marvelcomicsuniverse.com"); urlConnection = (HttpURLConnection) url.openConnection(); InputStream in = urlConnection.getInputStream(); InputStreamReader isw = new InputStreamReader(in); int data = isw.read(); while (data != -1) { char current = (char) data; data = isw.read(); System.out.print(current); } } catch (Exception e) { e.printStackTrace(); } finally { try { urlConnection.disconnect(); } catch (Exception e) { e.printStackTrace(); //If you want further info on failure... } } }
From source file:controllers.IndexServlet.java
private static void Convert(HttpServletRequest request, HttpServletResponse response, PrintWriter writer) { response.setContentType("text/plain"); try {/*from ww w .j av a 2 s .c o m*/ String fileName = request.getParameter("filename"); String fileUri = DocumentManager.GetFileUri(fileName); String fileExt = FileUtility.GetFileExtension(fileName); FileType fileType = FileUtility.GetFileType(fileName); String internalFileExt = DocumentManager.GetInternalExtension(fileType); if (DocumentManager.GetConvertExts().contains(fileExt)) { String key = ServiceConverter.GenerateRevisionId(fileUri); Pair<Integer, String> res = ServiceConverter.GetConvertedUri(fileUri, fileExt, internalFileExt, key, true); int result = res.getKey(); String newFileUri = res.getValue(); if (result != 100) { writer.write("{ \"step\" : \"" + result + "\", \"filename\" : \"" + fileName + "\"}"); return; } String correctName = DocumentManager .GetCorrectName(FileUtility.GetFileNameWithoutExtension(fileName) + internalFileExt); URL url = new URL(newFileUri); java.net.HttpURLConnection connection = (java.net.HttpURLConnection) url.openConnection(); InputStream stream = connection.getInputStream(); if (stream == null) { throw new Exception("Stream is null"); } File convertedFile = new File(DocumentManager.StoragePath(correctName, null)); try (FileOutputStream out = new FileOutputStream(convertedFile)) { int read; final byte[] bytes = new byte[1024]; while ((read = stream.read(bytes)) != -1) { out.write(bytes, 0, read); } out.flush(); } connection.disconnect(); //remove source file ? //File sourceFile = new File(DocumentManager.StoragePath(fileName, null)); //sourceFile.delete(); fileName = correctName; } writer.write("{ \"filename\" : \"" + fileName + "\"}"); } catch (Exception ex) { writer.write("{ \"error\": \"" + ex.getMessage() + "\"}"); } }
From source file:com.mopaas_mobile.http.BaseHttpRequester.java
public static String doGETwithHeader(String urlstr, String token, List<BasicNameValuePair> params) throws IOException { String result = null;//from w ww .j av a2 s. co m String content = ""; for (int i = 0; i < params.size(); i++) { content = content + "&" + URLEncoder.encode(((NameValuePair) params.get(i)).getName(), "UTF-8") + "=" + URLEncoder.encode(((NameValuePair) params.get(i)).getValue(), "UTF-8"); } URL url = new URL(urlstr + "?" + content.substring(1)); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty("token", token); connection.setDoInput(true); connection.setRequestMethod("GET"); connection.setUseCaches(false); connection.connect(); InputStream is = connection.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8")); StringBuffer b = new StringBuffer(); int ch; while ((ch = br.read()) != -1) { b.append((char) ch); } result = b.toString().trim(); connection.disconnect(); return result; }
From source file:GoogleAPI.java
/** * Forms an HTTP request, sends it using GET method and returns the result of the request as a JSONObject. * // ww w .j a va 2s . c o m * @param url The URL to query for a JSONObject. * @return The translated String. * @throws Exception on error. */ protected static JSONObject retrieveJSON(final URL url) throws Exception { try { final HttpURLConnection uc = (HttpURLConnection) url.openConnection(); uc.setRequestProperty("referer", referrer); uc.setRequestMethod("GET"); uc.setDoOutput(true); try { final String result = inputStreamToString(uc.getInputStream()); return new JSONObject(result); } 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 new Exception("[google-api-translate-java] Error retrieving translation.", ex); } }