List of usage examples for java.net URL openConnection
public URLConnection openConnection() throws java.io.IOException
From source file:be.geecko.openlauncher.net.SuggestionsTask.java
private static String readUrl(String http) throws IOException { URL url = new URL(http); URLConnection connection = url.openConnection(); InputStream in = connection.getInputStream(); String encoding = connection.getContentEncoding(); encoding = encoding == null ? "UTF-8" : encoding; return IOUtils.toString(in, encoding); }
From source file:Main.java
private static boolean confirmDownload(Context context, String stringUrl) { try {//from w ww .j a va2 s . c o m URL url = new URL(stringUrl); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); long candidateDate = urlConnection.getLastModified(); final SharedPreferences prefsManager = PreferenceManager .getDefaultSharedPreferences(context.getApplicationContext()); final long savedDate = prefsManager.getLong(stringUrl, 0); urlConnection.disconnect(); if (candidateDate <= savedDate) { return false; } timeToConfirm = candidateDate; return true; } catch (Throwable localThrowable) { localThrowable.printStackTrace(); return false; } }
From source file:Main.java
public static URI unredirect(URI uri) throws IOException { if (!REDIRECTOR_DOMAINS.contains(uri.getHost())) { return uri; }/*from www.j a va 2 s .c o m*/ URL url = uri.toURL(); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setInstanceFollowRedirects(false); connection.setDoInput(false); connection.setRequestMethod("HEAD"); connection.setRequestProperty("User-Agent", "ZXing (Android)"); try { connection.connect(); switch (connection.getResponseCode()) { case HttpURLConnection.HTTP_MULT_CHOICE: case HttpURLConnection.HTTP_MOVED_PERM: case HttpURLConnection.HTTP_MOVED_TEMP: case HttpURLConnection.HTTP_SEE_OTHER: case 307: // No constant for 307 Temporary Redirect ? String location = connection.getHeaderField("Location"); if (location != null) { try { return new URI(location); } catch (URISyntaxException e) { // nevermind } } } return uri; } finally { connection.disconnect(); } }
From source file:com.tnc.android.graphite.utils.GraphiteConnection.java
private static BufferedReader getReader(String urlString) throws Exception { URL url = new URL(urlString); HttpURLConnection http = (HttpURLConnection) url.openConnection(); http.setConnectTimeout(45000);// w ww . j a v a 2 s . c om http.setReadTimeout(45000); BufferedReader reader = new BufferedReader(new InputStreamReader(http.getInputStream()), 8192); return reader; }
From source file:Main.java
private static HttpURLConnection initHttpURLConn(String requestURL) throws MalformedURLException, IOException, ProtocolException { URL url = new URL(requestURL); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(TIME_OUT); connection.setReadTimeout(TIME_OUT); connection.setDoInput(true);//from w w w. j a va2 s. com connection.setDoOutput(true); connection.setUseCaches(false); connection.setRequestMethod("POST"); connection.setRequestProperty("Charset", CHARSET); connection.setRequestProperty("connection", "keep-alive"); return connection; }
From source file:Main.java
/** * @param url//from w w w.ja v a2s .co m * @return */ public static Bitmap getBitMapFromURL(URL url) { Bitmap mBitmap = null; try { HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoInput(true); conn.connect(); conn.getContentLength(); InputStream is = conn.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is); mBitmap = BitmapFactory.decodeStream(bis); bis.close(); is.close(); } catch (Exception e) { Log.e("Exception in MapScrollActivity.getBitmapFromURL", "" + e.getMessage()); e.printStackTrace(); } return mBitmap; }
From source file:Main.java
public static void downloadImage(String imageUrl) { if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { Log.d("TAG", "monted sdcard"); } else {//from ww w . j av a2s . c o m Log.d("TAG", "has no sdcard"); } HttpURLConnection con = null; FileOutputStream fos = null; BufferedOutputStream bos = null; BufferedInputStream bis = null; File imageFile = null; try { URL url = new URL(imageUrl); con = (HttpURLConnection) url.openConnection(); con.setConnectTimeout(5 * 1000); con.setReadTimeout(15 * 1000); con.setDoInput(true); con.setDoOutput(true); bis = new BufferedInputStream(con.getInputStream()); imageFile = new File(getImagePath(imageUrl)); fos = new FileOutputStream(imageFile); bos = new BufferedOutputStream(fos); byte[] b = new byte[1024]; int length; while ((length = bis.read(b)) != -1) { bos.write(b, 0, length); bos.flush(); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (bis != null) { bis.close(); } if (bos != null) { bos.close(); } if (con != null) { con.disconnect(); } } catch (IOException e) { e.printStackTrace(); } } if (imageFile != null) { } }
From source file:bakuposter.gcm.server.POST2GCMessage.java
public static void post(String apiKey, Content content) { try {/* www.ja v a 2 s . c o m*/ URL url = new URL("https://android.googleapis.com/gcm/send"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/json"); conn.setRequestProperty("Authorization", "key=" + apiKey); conn.setDoOutput(true); ObjectMapper mapper = new ObjectMapper(); System.out.println(content.getRegistration_ids().get(0)); DataOutputStream wr = new DataOutputStream(conn.getOutputStream()); mapper.writeValue(wr, content); wr.flush(); wr.close(); int responseCode = conn.getResponseCode(); System.out.println("responseCode = " + responseCode); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
From source file:Main.java
public static String getHtml(String getUrl, String charsetName) { String html = ""; URL url; try {/*w w w .j a v a2 s . co m*/ url = new URL(getUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty("User-Agent", userAgent); connection.setRequestProperty("Connection", "Keep-Alive"); connection.setRequestProperty("Cache-Control", "no-cache"); connection.setReadTimeout(timeout); connection.setFollowRedirects(true); connection.connect(); InputStream inStrm = connection.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(inStrm, charsetName)); String temp = ""; while ((temp = br.readLine()) != null) { html = html + (temp + '\n'); } } catch (Exception e) { e.printStackTrace(); } return html; }
From source file:com.grosscommerce.ICEcat.utilities.Downloader.java
private static HttpURLConnection prepareConnection(String urlFrom, String login, String pwd) throws ProtocolException, IOException, MalformedURLException { URL url = new URL(urlFrom); HttpURLConnection uc = (HttpURLConnection) url.openConnection(); // set up url connection to get retrieve information back uc.setRequestMethod("GET"); uc.setDoInput(true);//w w w.j av a 2 s .c o m String val = (new StringBuffer(login).append(":").append(pwd)).toString(); byte[] base = val.getBytes(); String authorizationString = "Basic " + Base64.encodeBase64String(base); uc.setRequestProperty("Authorization", authorizationString); uc.setRequestProperty("Accept-Encoding", "gzip, xml"); return uc; }