List of usage examples for java.net HttpURLConnection disconnect
public abstract void disconnect();
From source file:main.java.vasolsim.common.GenericUtils.java
/** * Returns if a given http based address can be connected to * * @param address the address//from w ww . java2 s . com * * @return if teh connection can be made */ public static boolean canConnectToAddress(String address) { if (!isValidAddress(address)) return false; try { HttpURLConnection connection = (HttpURLConnection) (new URL(address).openConnection()); connection.setRequestMethod("HEAD"); connection.connect(); boolean success = (connection.getResponseCode() == HttpURLConnection.HTTP_OK); connection.disconnect(); return success; } catch (Exception e) { return false; } }
From source file:Main.java
public static URI unredirect(URI uri) throws IOException { if (!REDIRECTOR_DOMAINS.contains(uri.getHost())) { return uri; }/* ww w . j av a 2 s . co 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.headswilllol.basiclauncher.Launcher.java
public static int getFileSize(URL url) { HttpURLConnection conn = null; try {/*from w w w . j a v a 2s.c o m*/ conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("HEAD"); // joke's on you if the server doesn't specify conn.getInputStream(); return conn.getContentLength(); } catch (Exception e) { return -1; } finally { if (conn != null) conn.disconnect(); } }
From source file:Main.java
private static void downloadResource(String from, String to) { OutputStream outputStream = null; BufferedInputStream inputStream = null; HttpURLConnection connection = null; URL url;//from w w w. j a v a2 s .co m byte[] buffer = new byte[1024]; try { url = new URL(from); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setDoOutput(true); connection.connect(); outputStream = new FileOutputStream(to); inputStream = new BufferedInputStream(url.openStream()); int read; while ((read = inputStream.read(buffer)) > 0) { outputStream.write(buffer, 0, read); } } catch (IOException e) { e.printStackTrace(); } finally { if (outputStream != null) try { outputStream.close(); } catch (IOException e) { } if (inputStream != null) try { inputStream.close(); } catch (IOException e) { } if (connection != null) connection.disconnect(); } }
From source file:edu.stanford.epadd.launcher.Splash.java
private static boolean isURLAlive(String url) throws IOException { try {/*from w ww. ja v a2s . com*/ // attempt to fetch the page // throws a connect exception if the server is not even running // so catch it and return false // since "index" may auto load default archive, attach it to session, and redirect to "info" page, // we need to maintain the session across the pages. // see "Maintaining the session" at http://stackoverflow.com/questions/2793150/how-to-use-java-net-urlconnection-to-fire-and-handle-http-requests CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL)); out.println("Testing for already running ePADD by probing " + url); HttpURLConnection u = (HttpURLConnection) new URL(url).openConnection(); if (u.getResponseCode() == 200) { u.disconnect(); out.println("ePADD is already running!"); return true; } u.disconnect(); } catch (ConnectException ce) { } out.println("Good, ePADD is not already running"); return false; }
From source file:lapispaste.Main.java
private static void paster(Map<String, String> pastemap, StringBuffer pdata) { try {/*from ww w . j a va 2s. c o m*/ URL url = new URL("http://paste.linux-sevenler.org/index.php"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // set connection 'writeable' conn.setDoOutput(true); conn.setDoInput(true); conn.setRequestMethod("POST"); // construct the data... String data; String pdataString = pdata.toString(); data = URLEncoder.encode("language", "ISO-8859-9") + "=" + URLEncoder.encode(pastemap.get("format"), "ISO-8859-9"); data += "&" + URLEncoder.encode("source", "ISO-8859-9") + "=" + URLEncoder.encode(pdataString, "ISO-8859-9"); data += "&" + URLEncoder.encode("submit", "ISO-8859-9") + "=" + URLEncoder.encode(" Kaydet ", "ISO-8859-9"); OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); wr.write(data); wr.flush(); // get new url where the paste is conn.getInputStream(); System.out.println(conn.getURL()); conn.disconnect(); } catch (IOException ex) { ex.printStackTrace(); } }
From source file:com.raphfrk.craftproxyclient.net.auth.AuthManager.java
@SuppressWarnings("unchecked") public static void authServer16(String hash) throws IOException { URL url;/*from w w w . ja va2 s . c o m*/ String username; String accessToken; try { if (loginDetails == null) { throw new IOException("Not logged in"); } try { username = URLEncoder.encode(getUsername(), "UTF-8"); accessToken = URLEncoder.encode(getAccessToken(), "UTF-8"); hash = URLEncoder.encode(hash, "UTF-8"); } catch (UnsupportedEncodingException e) { throw new IOException("Username/password encoding error", e); } String urlString; urlString = sessionServer16 + "user=" + username + "&sessionId=" + accessToken + "&serverId=" + hash; url = new URL(urlString); } catch (MalformedURLException e) { throw new IOException("Auth server URL error", e); } HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setInstanceFollowRedirects(false); con.setReadTimeout(5000); con.setConnectTimeout(5000); con.connect(); if (con.getResponseCode() != 200) { throw new IOException("Auth server rejected username and password"); } BufferedReader reader = new BufferedReader( new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8)); try { String reply = reader.readLine(); if (!"OK".equals(reply)) { throw new IOException("Auth server replied (" + reply + ")"); } } finally { reader.close(); con.disconnect(); } }
From source file:edu.stanford.epadd.launcher.Splash.java
private static boolean killRunningServer(String url) throws IOException { try {//from ww w . j a v a 2s.c om // attempt to fetch the page // throws a connect exception if the server is not even running // so catch it and return false // String http = url + "exit.jsp?message=Shutdown%20request%20from%20a%20different%20instance%20of%20ePADD"; // version num spaces and brackets screw up the URL connection // actually this should not be a HTTP connection, it can be simply any connection out.println("Sending a kill request to " + url); HttpURLConnection u = (HttpURLConnection) new URL(url).openConnection(); u.connect(); if (u.getResponseCode() == 200) { u.disconnect(); return true; } u.disconnect(); } catch (ConnectException ce) { err.println("Warning: unable to kill running server: " + ce); } return false; }
From source file:com.binil.pushnotification.ServerUtil.java
/** * Issue a POST request to the server./*from w ww. j a v a 2 s . c o m*/ * * @param endpoint POST address. * @param params request parameters. * @throws java.io.IOException propagated from POST. */ private static void post(String endpoint, Map<String, Object> params) throws IOException, JSONException { URL url = new URL(endpoint); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod("POST"); urlConnection.setRequestProperty("Cache-Control", "no-cache"); urlConnection.setRequestProperty("Content-Type", "application/json;charset=utf-8"); urlConnection.setRequestProperty("Accept-Encoding", "gzip,deflate"); urlConnection.setRequestProperty("Accept", "*/*"); urlConnection.setDoOutput(true); JSONObject json = new JSONObject(params); String body = json.toString(); urlConnection.setFixedLengthStreamingMode(body.length()); try { OutputStream os = urlConnection.getOutputStream(); os.write(body.getBytes("UTF-8")); os.close(); } catch (Exception e) { e.printStackTrace(); } finally { int status = urlConnection.getResponseCode(); String connectionMsg = urlConnection.getResponseMessage(); urlConnection.disconnect(); if (status != HttpURLConnection.HTTP_OK) { Log.wtf(TAG, connectionMsg); throw new IOException("Post failed with error code " + status); } } }
From source file:oauth.signpost.basic.DefaultOAuthProvider.java
@Override protected void closeConnection(HttpRequest request, HttpResponse response) { HttpURLConnection connection = (HttpURLConnection) request.unwrap(); if (connection != null) { connection.disconnect(); }/*from w w w . j a v a 2s . co m*/ }