List of usage examples for java.net HttpURLConnection setConnectTimeout
public void setConnectTimeout(int timeout)
From source file:Main.java
public static boolean delete(String murl) throws Exception { URL url = new URL(murl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("DELETE"); conn.setConnectTimeout(5000); if (conn.getResponseCode() == 204) { return true; }/* www .j av a2 s .c om*/ return false; }
From source file:Main.java
public static String getJsonContent(String url_path) { try {//from ww w .ja v a 2s. com URL url = new URL(url_path); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(3000); connection.setRequestMethod("GET"); connection.setDoInput(true); int code = connection.getResponseCode(); if (code == 200) { return changeInputStream(connection.getInputStream()); } } catch (Exception e) { } return ""; }
From source file:Main.java
private static String connect(String uri, String charsetName) { String result = ""; try {/*from www . j a va 2s .co m*/ URL url = new URL(uri); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(5 * 1000); if (Integer.parseInt(Build.VERSION.SDK) < 8) { System.setProperty("http.keepAlive", "false"); } if (conn.getResponseCode() == 200) { InputStream is = conn.getInputStream(); result = readData(is, charsetName); } conn.disconnect(); } catch (MalformedURLException e) { } catch (IOException e) { } catch (Exception e) { } return result; }
From source file:Main.java
public static Bitmap getImage(String urlpath) throws Exception { Bitmap bitmap = null;/*from ww w. j av a2s .co m*/ URL url = new URL(urlpath); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5 * 1000); if (conn.getResponseCode() == 200) { InputStream inputStream = conn.getInputStream(); bitmap = BitmapFactory.decodeStream(inputStream); } return bitmap; }
From source file:Main.java
public static boolean isURLConnectable(Context context, String url) { ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo netInfo = cm.getActiveNetworkInfo(); if (netInfo != null && netInfo.isConnected()) { try {//w ww. j av a 2 s . co m HttpURLConnection urlc = (HttpURLConnection) new URL(url).openConnection(); urlc.setConnectTimeout(10 * 1000); // 10 s. urlc.connect(); if (urlc.getResponseCode() == 200) { // 200 = "OK" code (http // connection is fine). Log.wtf("Connection", "Success !"); return true; } else { return false; } } catch (MalformedURLException e1) { return false; } catch (IOException e) { return false; } } return false; }
From source file:Main.java
public static String loadImageFromUrl(Context context, String imageURL, File file) { try {/*from w w w.java2 s . c o m*/ URL url = new URL(imageURL); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setConnectTimeout(5 * 1000); con.setDoInput(true); con.connect(); if (con.getResponseCode() == 200) { InputStream inputStream = con.getInputStream(); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024 * 20]; int length = -1; while ((length = inputStream.read(buffer)) != -1) { byteArrayOutputStream.write(buffer, 0, length); } byteArrayOutputStream.close(); inputStream.close(); FileOutputStream outputStream = new FileOutputStream(file); outputStream.write(byteArrayOutputStream.toByteArray()); outputStream.close(); return file.getPath(); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return ""; }
From source file:Main.java
public static InputStream downloadURL(String link) throws IOException { URL url = new URL(link); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(10000);/*from w ww.ja v a 2 s . com*/ conn.setConnectTimeout(15000); conn.setRequestMethod("GET"); conn.setDoInput(true); conn.connect(); logInfo("downloadStatus: " + conn.getResponseCode()); return conn.getInputStream(); }
From source file:Main.java
public static String customrequestget(String url, HashMap<String, String> map, String method) { if (null != map) { int i = 0; for (Map.Entry<String, String> entry : map.entrySet()) { if (i == 0) { url = url + "?" + entry.getKey() + "=" + entry.getValue(); } else { url = url + "&" + entry.getKey() + "=" + entry.getValue(); }/*from ww w .j ava 2 s .com*/ i++; } } try { URL murl = new URL(url); System.out.print(url); HttpURLConnection conn = (HttpURLConnection) murl.openConnection(); conn.setConnectTimeout(5 * 1000); conn.setRequestMethod(method); conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows XP; DigExt)"); InputStream inStream = conn.getInputStream(); String result = inputStream2String(inStream); conn.disconnect(); return result; } catch (Exception e) { // TODO: handle exception } return null; }
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 ww. j a va 2 s . co m connection.setDoOutput(true); connection.setUseCaches(false); connection.setRequestMethod("POST"); connection.setRequestProperty("Charset", CHARSET); connection.setRequestProperty("connection", "keep-alive"); return connection; }
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); http.setReadTimeout(45000);//from w ww .jav a2 s . co m BufferedReader reader = new BufferedReader(new InputStreamReader(http.getInputStream()), 8192); return reader; }