List of usage examples for java.net HttpURLConnection connect
public abstract void connect() throws IOException;
From source file:Main.java
public static boolean hasActiveInternetConnection(Context context) { if (isNetworkAvailable(context)) { try {// w w w . j a v a 2s .c o m HttpURLConnection connection = (HttpURLConnection) (new URL( "http://clients3.google.com/generate_204")).openConnection(); connection.setRequestProperty("User-Agent", "Test"); connection.setRequestProperty("Connection", "close"); connection.setReadTimeout(1500); connection.connect(); return (connection.getResponseCode() == 204 && connection.getContentLength() == 0); } catch (IOException e) { Log.e("ERROR", "Error checking internet connection"); } } else Log.e("ERROR", "No network available"); return false; }
From source file:org.cytoscape.app.internal.net.server.ScreenOriginsBeforeResponseTest.java
private static HttpURLConnection connectToURL(String urlString, String method, String origin) throws Exception { final URL url = new URL(urlString); final HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(method); connection.setDoOutput(true);/*from w ww . java 2 s . com*/ if (origin != null) connection.setRequestProperty("Origin", origin); connection.setConnectTimeout(1000); connection.connect(); return connection; }
From source file:Main.java
public static URI unredirect(URI uri) throws IOException { if (!REDIRECTOR_DOMAINS.contains(uri.getHost())) { return uri; }//from w ww. ja v a 2 s . c om 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.rumblefish.friendlymusic.api.WebRequest.java
public static Bitmap getBitmapAtURL(URL url) { InputStream inStream = null;//from w w w .ja v a2 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:itdelatrisu.opsu.downloads.BloodcatServer.java
/** * Returns a JSON object from a URL./*from ww w. java 2 s. com*/ * @param url the remote URL * @return the JSON object * @author Roland Illig (http://stackoverflow.com/a/4308662) */ public static JSONObject readJsonFromUrl(URL url) throws IOException { // open connection HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(Download.CONNECTION_TIMEOUT); conn.setReadTimeout(Download.READ_TIMEOUT); conn.setUseCaches(false); try { conn.connect(); } catch (SocketTimeoutException e) { ErrorHandler.error("Connection to server timed out.", e, false); throw e; } if (Thread.interrupted()) return null; // read JSON JSONObject json = null; try (InputStream in = conn.getInputStream()) { BufferedReader rd = new BufferedReader(new InputStreamReader(in)); StringBuilder sb = new StringBuilder(); int c; while ((c = rd.read()) != -1) sb.append((char) c); json = new JSONObject(sb.toString()); } catch (SocketTimeoutException e) { ErrorHandler.error("Connection to server timed out.", e, false); throw e; } catch (JSONException e1) { ErrorHandler.error("Failed to create JSON object.", e1, true); } return json; }
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 w w . j a v a 2 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:Main.java
static String downloadUrl(String myurl) throws IOException { InputStream is = null;//ww w .ja va2 s . com try { URL url = new URL(myurl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestProperty("Authorization", "Basic cm9vdDpvcmllbnRkYg=="); conn.setReadTimeout(10000); conn.setConnectTimeout(15000); conn.setRequestMethod("GET"); conn.setDoInput(true); //start conn.connect(); int response = conn.getResponseCode(); Log.e("The response is: ", "" + response); is = conn.getInputStream(); //converte inputStream in stringa String contentAsString = readIt(is); return contentAsString; } catch (IOException e) { Log.e("HTTP", e.getMessage()); return null; } finally { if (is != null) { is.close(); } } }
From source file:com.wx.kernel.util.HttpKit.java
/** * Send GET request// ww w . ja v a2 s . c om */ public static String get(String url, Map<String, String> queryParas, Map<String, String> headers) { HttpURLConnection conn = null; try { conn = getHttpConnection(buildUrlWithQueryString(url, queryParas), GET, headers); conn.connect(); return readResponseString(conn); } catch (Exception e) { throw new RuntimeException(e); } finally { if (conn != null) { conn.disconnect(); } } }
From source file:org.pixmob.fm2.util.HttpUtils.java
/** * Download a file.//from w w w. j a v a 2 s . co m */ public static void downloadToFile(Context context, String uri, Set<String> cookies, File outputFile) throws IOException { final HttpURLConnection conn = newRequest(context, uri, cookies); try { conn.connect(); final int sc = conn.getResponseCode(); if (sc != HttpURLConnection.HTTP_OK) { throw new IOException("Cannot download file: " + uri + "; statusCode=" + sc); } final InputStream input = getInputStream(conn); IOUtils.writeToFile(input, outputFile); } finally { conn.disconnect(); } }
From source file:com.ibuildapp.romanblack.CataloguePlugin.imageloader.Utils.java
public static String downloadFile(Context context, String url, String md5) { final int BYTE_ARRAY_SIZE = 8024; final int CONNECTION_TIMEOUT = 30000; final int READ_TIMEOUT = 30000; try {// w w w. j a v a2 s. com for (int i = 0; i < 3; i++) { URL fileUrl = new URL(URLDecoder.decode(url)); HttpURLConnection connection = (HttpURLConnection) fileUrl.openConnection(); connection.setConnectTimeout(CONNECTION_TIMEOUT); connection.setReadTimeout(READ_TIMEOUT); connection.connect(); int status = connection.getResponseCode(); if (status >= HttpStatus.SC_BAD_REQUEST) { connection.disconnect(); continue; } BufferedInputStream bufferedInputStream = new BufferedInputStream(connection.getInputStream()); File file = new File(StaticData.getCachePath(context) + md5); if (!file.exists()) { new File(StaticData.getCachePath(context)).mkdirs(); file.createNewFile(); } FileOutputStream fileOutputStream = new FileOutputStream(file, false); int byteCount; byte[] buffer = new byte[BYTE_ARRAY_SIZE]; while ((byteCount = bufferedInputStream.read(buffer, 0, BYTE_ARRAY_SIZE)) != -1) fileOutputStream.write(buffer, 0, byteCount); bufferedInputStream.close(); fileOutputStream.flush(); fileOutputStream.close(); return file.getAbsolutePath(); } } catch (Exception e) { e.printStackTrace(); return null; } return null; }