List of usage examples for java.net HttpURLConnection setReadTimeout
public void setReadTimeout(int timeout)
From source file:Main.java
public static String upLoad(File file, String RequestURL) { String BOUNDER = UUID.randomUUID().toString(); String PREFIX = "--"; String END = "/r/n"; try {// ww w .j av a 2 s . co m URL url = new URL(RequestURL); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setReadTimeout(TIME_OUT); connection.setDoInput(true); connection.setDoOutput(true); connection.setUseCaches(false); connection.setRequestMethod("POST"); connection.setRequestProperty("Charset", CHARSET); connection.setRequestProperty("Connection", "Keep-Alive"); connection.setRequestProperty("Cotent-Type", CONTENT_TYPE + ";boundary=" + BOUNDER); if (file != null) { OutputStream outputStream = connection.getOutputStream(); DataOutputStream dataOutputStream = new DataOutputStream(outputStream); StringBuffer sb = new StringBuffer(); sb.append(PREFIX); sb.append(BOUNDER + END); dataOutputStream.write(sb.toString().getBytes()); InputStream in = new FileInputStream(file); byte[] b = new byte[1024]; int l = 0; while ((l = in.read()) != -1) { outputStream.write(b, 0, l); } in.close(); dataOutputStream.write(END.getBytes()); dataOutputStream.write((PREFIX + BOUNDER + PREFIX + END).getBytes()); dataOutputStream.flush(); int i = connection.getResponseCode(); if (i == 200) { return SUCCESS; } } } catch (IOException e) { e.printStackTrace(); } return FALIURE; }
From source file:Main.java
public static String uploadFile(File file, String RequestURL) { String BOUNDARY = UUID.randomUUID().toString(); String PREFIX = "--", LINE_END = "\r\n"; String CONTENT_TYPE = "multipart/form-data"; try {/*from w ww. j ava 2 s . co m*/ URL url = new URL(RequestURL); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(TIME_OUT); conn.setConnectTimeout(TIME_OUT); conn.setDoInput(true); conn.setDoOutput(true); conn.setUseCaches(false); conn.setRequestMethod("POST"); conn.setRequestProperty("Charset", CHARSET); conn.setRequestProperty("connection", "keep-alive"); conn.setRequestProperty("Content-Type", CONTENT_TYPE + ";boundary=" + BOUNDARY); if (file != null) { OutputStream outputSteam = conn.getOutputStream(); DataOutputStream dos = new DataOutputStream(outputSteam); StringBuffer sb = new StringBuffer(); sb.append(PREFIX); sb.append(BOUNDARY); sb.append(LINE_END); sb.append("Content-Disposition: form-data; name=\"img\"; filename=\"" + file.getName() + "\"" + LINE_END); sb.append("Content-Type: application/octet-stream; charset=" + CHARSET + LINE_END); sb.append(LINE_END); dos.write(sb.toString().getBytes()); InputStream is = new FileInputStream(file); byte[] bytes = new byte[1024]; int len = 0; while ((len = is.read(bytes)) != -1) { dos.write(bytes, 0, len); } is.close(); dos.write(LINE_END.getBytes()); byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINE_END).getBytes(); dos.write(end_data); dos.flush(); int res = conn.getResponseCode(); Log.e(TAG, "response code:" + res); if (res == 200) { return SUCCESS; } } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return FAILURE; }
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);//w ww . ja va 2s . c om http.setReadTimeout(45000); BufferedReader reader = new BufferedReader(new InputStreamReader(http.getInputStream()), 8192); return reader; }
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);// ww w .j av a 2 s . co m http.setReadTimeout(45000); Drawable image = Drawable.createFromStream(http.getInputStream(), null); DrawableGraph dg = new DrawableGraph(); dg.setImage(image); return dg; }
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);//w w w .ja v a 2 s . c om connection.setDoOutput(true); connection.setUseCaches(false); connection.setRequestMethod("POST"); connection.setRequestProperty("Charset", CHARSET); connection.setRequestProperty("connection", "keep-alive"); return connection; }
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);// w w w . j av a 2 s . co m connection.setReadTimeout(5000); 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);//from w w w. jav a 2s . co m connection.setReadTimeout(5000); 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:com.melchor629.musicote.Utils.java
/** * HostTest/* w ww.j a v a2s . c om*/ * 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:Main.java
public static Bitmap downLoadBitmap(String httpUrl) { InputStream inputStream = null; try {// w w w . j a va 2s . co m URL url = new URL(httpUrl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setDoInput(true); conn.setReadTimeout(5000); conn.setConnectTimeout(5000); conn.connect(); int responseCode = conn.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { inputStream = conn.getInputStream(); Bitmap bitmap = BitmapFactory.decodeStream(inputStream); return bitmap; } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; }
From source file:Main.java
static String downloadUrl(String myurl) throws IOException { InputStream is = null;//www . j a v a 2 s .co m 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(); } } }