List of usage examples for java.net HttpURLConnection setConnectTimeout
public void setConnectTimeout(int timeout)
From source file:com.tnc.android.graphite.utils.GraphiteConnection.java
public static DrawableGraph getGraph(String serverUrl, String ps) throws Exception { URL url = new URL(serverUrl + GRAPH_PARAM_STRING + ps); HttpURLConnection http = (HttpURLConnection) url.openConnection(); http.setConnectTimeout(45000); http.setReadTimeout(45000);//from w w w. j av a 2 s. co m Drawable image = Drawable.createFromStream(http.getInputStream(), null); DrawableGraph dg = new DrawableGraph(); dg.setImage(image); return dg; }
From source file:Main.java
/** * This method checks if the Network available on the device or not. * /*from www .j a v a 2 s . c om*/ * @param context * @return true if network available, false otherwise */ public static Boolean isNetworkAvailable(Context context) { boolean connected = false; final ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); final NetworkInfo netInfo = cm.getActiveNetworkInfo(); if (netInfo != null && netInfo.isConnectedOrConnecting()) { connected = true; } else if (netInfo != null && netInfo.isConnected() && cm.getActiveNetworkInfo().isAvailable()) { connected = true; } else if (netInfo != null && netInfo.isConnected()) { try { URL url = new URL("http://www.google.com"); HttpURLConnection urlc = (HttpURLConnection) url.openConnection(); urlc.setConnectTimeout(3000); urlc.connect(); if (urlc.getResponseCode() == 200) { connected = true; } } catch (MalformedURLException e1) { e1.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } else if (cm != null) { final NetworkInfo[] netInfoAll = cm.getAllNetworkInfo(); for (NetworkInfo ni : netInfoAll) { System.out.println("get network type :::" + ni.getTypeName()); if ((ni.getTypeName().equalsIgnoreCase("WIFI") || ni.getTypeName().equalsIgnoreCase("MOBILE")) && ni.isConnected() && ni.isAvailable()) { connected = true; if (connected) { break; } } } } return connected; }
From source file:Main.java
public static String requestData(String address) throws Exception { URL url = new URL(address); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(5000); connection.setReadTimeout(5000);//from w w w . j av a 2s . c o m String data = null; InputStream is = null; if (connection.getResponseCode() == 200) { is = connection.getInputStream(); data = readFromStream(is); } if (is != null) { is.close(); } return data; }
From source file:Main.java
public static String requestData(String address) throws IOException { URL url = new URL(address); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(5000); connection.setReadTimeout(5000);/* w w w . j av a 2s. com*/ String data = null; InputStream is = null; if (connection.getResponseCode() == 200) { is = connection.getInputStream(); data = readFromStream(is); } if (is != null) { is.close(); } return data; }
From source file:Main.java
static public String download_json(String url_addr) { StringBuilder result = new StringBuilder(); try {// ww w . java2s . c o m URL url = new URL(url_addr); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); if (conn != null) { conn.setConnectTimeout(10000); conn.setUseCaches(false); if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) { BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); for (;;) { String line = br.readLine(); if (line == null) break; result.append(line + '\n'); } br.close(); } else { conn.disconnect(); return null; } conn.disconnect(); } } catch (Exception e) { Log.e(TAG, e.toString()); return null; } return result.toString(); }
From source file:Main.java
public static boolean isAvailable(String urlString) throws IOException { try {// w w w .j a va 2 s. co m StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); URL url = new URL(urlString); HttpURLConnection urlc = (HttpURLConnection) url.openConnection(); urlc.setRequestProperty("Connection", "close"); urlc.setConnectTimeout(1000); urlc.connect(); if (urlc.getResponseCode() == 200) { return true; } else { return false; } } catch (Exception e) { e.printStackTrace(); } return false; }
From source file:Authentication.DinserverAuth.java
public static boolean Login(String nick, String pass, Socket socket) throws IOException { boolean logged = false; JSONObject authRequest = new JSONObject(); authRequest.put("username", nick); authRequest.put("auth_id", pass); String query = "http://127.0.0.1:5000/token"; URL url = new URL(query); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(5000); conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); conn.setDoOutput(true);/* w ww . j a v a2 s . c o m*/ conn.setDoInput(true); conn.setRequestMethod("POST"); OutputStream os = conn.getOutputStream(); os.write(authRequest.toString().getBytes(Charset.forName("UTF-8"))); os.close(); String output = new String(); StringBuilder sb = new StringBuilder(); int HttpResult = conn.getResponseCode(); if (HttpResult == HttpURLConnection.HTTP_OK) { output = IOUtils.toString(new InputStreamReader(conn.getInputStream(), Charset.forName("UTF-8"))); } else { System.out.println(conn.getResponseMessage()); } JSONObject jsonObject = new JSONObject(output); logged = jsonObject.getBoolean("ok"); conn.disconnect(); if (logged) { if (DinserverMaster.addUser(nick, socket)) { System.out.println("User " + nick + " logged in"); } else { logged = false; } } return logged; }
From source file:com.uksf.mf.core.utility.ClassNames.java
/** * Checks if connection to URL can be established * @param url url to check//w ww.j a va 2s . c o m * @return connection state * @throws IOException error */ private static boolean checkConnection(URL url) throws IOException { HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(1500); connection.setReadTimeout(1500); connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729)"); connection.connect(); return connection.getResponseCode() == 404; }
From source file:Main.java
/** * Given a string url, connects and returns response code * * @param urlString string to fetch * @param readTimeOutMs read time out * @param connectionTimeOutMs connection time out * @param urlRedirect should use urlRedirect * @param useCaches should use cache * @return httpResponseCode http response code * @throws IOException/* w ww .j a v a 2s . c om*/ */ public static int checkUrlWithOptions(String urlString, int readTimeOutMs, int connectionTimeOutMs, boolean urlRedirect, boolean useCaches) throws IOException { URL url = new URL(urlString); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setReadTimeout(readTimeOutMs /* milliseconds */); connection.setConnectTimeout(connectionTimeOutMs /* milliseconds */); connection.setRequestMethod("GET"); connection.setInstanceFollowRedirects(urlRedirect); connection.setUseCaches(useCaches); // Starts the query connection.connect(); int responseCode = connection.getResponseCode(); connection.disconnect(); return responseCode; }
From source file:Main.java
@SuppressWarnings("unused") public static void selectServer2() { for (String host : host_arr) { HttpURLConnection conn = null; try {/*from w w w . j a v a2 s.c o m*/ conn = (HttpURLConnection) new URL(host).openConnection(); conn.setConnectTimeout(6000); int result = conn.getResponseCode(); String re = conn.getResponseMessage(); if (result == HttpURLConnection.HTTP_OK) { HOST = host;// break; } else { } } catch (MalformedURLException e) { } catch (IOException e) { } finally { if (conn != null) { conn.disconnect(); conn = null; } } } }