List of usage examples for java.net HttpURLConnection disconnect
public abstract void disconnect();
From source file:Main.java
/** * Return '' or error message if error occurs during URL connection. * /* w w w . ja v a 2 s .co m*/ * @param url The URL to ckeck * @return */ public static String getUrlStatus(String url) { URL u; URLConnection conn; int connectionTimeout = 500; try { u = new URL(url); conn = u.openConnection(); conn.setConnectTimeout(connectionTimeout); // TODO : set proxy if (conn instanceof HttpURLConnection) { HttpURLConnection httpConnection = (HttpURLConnection) conn; httpConnection.setInstanceFollowRedirects(true); httpConnection.connect(); httpConnection.disconnect(); // FIXME : some URL return HTTP200 with an empty reply from server // which trigger SocketException unexpected end of file from server int code = httpConnection.getResponseCode(); if (code == HttpURLConnection.HTTP_OK) { return ""; } else { return "Status: " + code; } } // TODO : Other type of URLConnection } catch (Exception e) { e.printStackTrace(); return e.toString(); } return ""; }
From source file:Main.java
/** * Makes a GET call to the server.//from w w w. j a v a 2 s . c o m * @return The serve response (expected is JSON) */ public static String httpGet(String urlStr) { try { URL url = new URL(urlStr); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); InputStream in = new BufferedInputStream(urlConnection.getInputStream()); String r = readStream(in); urlConnection.disconnect(); return r; } catch (MalformedURLException e) { Log.v("MyLog", "GET: Bad URL"); e.printStackTrace(); return "GET: Bad URL"; } catch (IOException e) { Log.v("MyLog", "GET: Bad Con"); e.printStackTrace(); return "GET: Bad Con [" + urlStr + "]"; } }
From source file:Main.java
public static boolean isUpdateAvailable(Context paramContext, String paramString) { try {/*from w w w . j ava 2 s. c o m*/ HttpURLConnection localHttpURLConnection = (HttpURLConnection) new URL(paramString).openConnection(); long l1 = localHttpURLConnection.getHeaderFieldDate("Last-Modified", System.currentTimeMillis()); localHttpURLConnection.disconnect(); long l2 = PreferenceManager.getDefaultSharedPreferences(paramContext.getApplicationContext()) .getLong(paramString, 0L); boolean toReturn = l1 > l2; return toReturn; } catch (Throwable localThrowable) { localThrowable.printStackTrace(); } return false; }
From source file:org.elegosproject.romupdater.DownloadManager.java
public static boolean checkHttpFile(String url) { try {/*from ww w .j a va 2 s .c om*/ HttpParams httpParameters = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParameters, 3000); Log.i(TAG, "Testing " + url + "..."); URL theUrl = new URL(url); HttpURLConnection connection = (HttpURLConnection) theUrl.openConnection(); if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { connection.disconnect(); } else { Log.i(TAG, "HTTP Response code: " + connection.getResponseCode()); return false; } } catch (IOException e) { Log.e(TAG, e.toString()); return false; } return true; }
From source file:com.melchor629.musicote.Utils.java
/** * HostTest/* w w w . j av a 2s . co m*/ * Sirve para comprobar si est encendido el PC * Nothing to see here... * * @param host HOST IP * @return int response */ public static int HostTest(String host) { int response = 0; try { StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); URL urlhttp = new URL("http://" + host); HttpURLConnection http = (HttpURLConnection) urlhttp.openConnection(); http.setReadTimeout(1000); response = http.getResponseCode(); http.disconnect(); } catch (Exception e) { Log.e("Comprobando", "Excepcin HTTPURL: " + e.toString() + " " + host); } return response; }
From source file:net.sf.taverna.t2.renderers.RendererUtils.java
public static long getSizeInBytes(Path path) throws IOException { if (isValue(path)) return Files.size(path); if (!isReference(path)) throw new IllegalArgumentException("Path is not a value or reference"); URL url = getReference(path).toURL(); switch (url.getProtocol().toLowerCase()) { case "http": case "https": HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("HEAD"); conn.connect();//from www.j av a 2 s.c o m String contentLength = conn.getHeaderField("Content-Length"); conn.disconnect(); if (contentLength != null && !contentLength.isEmpty()) return Long.parseLong(contentLength); return -1; case "file": return FileUtils.toFile(url).length(); default: return -1; } }
From source file:Main.java
public static String accessToFlashAir(String uri) throws IOException { URL url = new URL(uri); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); String result = null;// w w w .j a v a 2s . co m try { InputStream in = new BufferedInputStream(urlConnection.getInputStream()); result = inputStreamToString(in); in.close(); } finally { urlConnection.disconnect(); } return result; }
From source file:net.ftb.data.news.RSSReader.java
public static List<NewsArticle> readRSS() { try {//from w w w . j a va2 s .co m List<NewsArticle> news = Lists.newArrayList(); DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); URL u = new URL(Locations.feedURL); HttpURLConnection conn = (HttpURLConnection) u.openConnection(); conn.connect(); if (conn.getResponseCode() != 200) { Logger.logWarn("News download failed"); AppUtils.debugConnection(conn); conn.disconnect(); return null; } Document doc = builder.parse(conn.getInputStream()); NodeList nodes = doc.getElementsByTagName("item"); for (int i = 0; i < nodes.getLength(); i++) { Element element = (Element) nodes.item(i); NewsArticle article = new NewsArticle(); article.setTitle(getTextValue(element, "title")); article.setHyperlink(getTextValue(element, "link")); article.setBody(getTextValue(element, "content:encoded")); article.setDate(getTextValue(element, "pubDate")); news.add(article); } return news; } catch (Exception ex) { Logger.logWarn("News download failed", ex); return null; } }
From source file:Main.java
public static String SimpleHttp(String urlStr) throws Exception { URL url = new URL(urlStr); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); int resCode = conn.getResponseCode(); InputStream input = null;//from w w w. j a v a2s . c o m String result = null; if (resCode == 200) { input = conn.getInputStream(); result = toString(input); input.close(); conn.disconnect(); } else { throw new Exception("connect failed"); } return result; }
From source file:HttpUtil.java
public static long getURLLastModified(String url) { URL Url;// w ww .j a v a2 s .c o m HttpURLConnection httpconn = null; long modtime; try { Url = new URL(url); httpconn = (HttpURLConnection) Url.openConnection(); modtime = httpconn.getLastModified(); } catch (Exception e) { modtime = -1; } if (httpconn != null) httpconn.disconnect(); System.out.println("URL:" + url + " LastModified:" + new Date(modtime).toString()); return modtime; }