List of usage examples for java.net HttpURLConnection setConnectTimeout
public void setConnectTimeout(int timeout)
From source file:Main.java
public static String readTextFromURL(String urlString) throws IOException { HttpURLConnection urlConnection = null; URL url = new URL(urlString); urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod("GET"); urlConnection.setReadTimeout(10000 /* milliseconds */); urlConnection.setConnectTimeout(15000 /* milliseconds */); urlConnection.setDoOutput(true);/* w w w . j a v a2 s. c o m*/ urlConnection.connect(); BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream())); char[] buffer = new char[1024]; String jsonString = new String(); StringBuilder sb = new StringBuilder(); String line; while ((line = br.readLine()) != null) { sb.append(line + "\n"); } br.close(); return sb.toString(); }
From source file:com.example.admin.processingboilerplate.JsonIO.java
public static StringBuilder loadString(String url) { StringBuilder sb = new StringBuilder(); try {/*from www. j a va2 s . c o m*/ URLConnection uc = (new URL(url)).openConnection(); HttpURLConnection con = url.startsWith("https") ? (HttpsURLConnection) uc : (HttpURLConnection) uc; try { con.setReadTimeout(6000); con.setConnectTimeout(6000); con.setRequestMethod("GET"); con.setDoInput(true); con.setRequestProperty("User-Agent", USER_AGENT); sb = load(con); } catch (IOException e) { Log.e("loadJson", e.getMessage(), e); } finally { con.disconnect(); } } catch (IOException e) { Log.e("loadJson", e.getMessage(), e); } return sb; }
From source file:Main.java
/** * Make an HTTP request to the given URL and return a String as the response. *//*from www . ja v a2 s. co m*/ private static String makeHttpRequest(URL url) throws IOException { String jsonResponse = ""; // If the URL is null, then return early. if (url == null) { return jsonResponse; } HttpURLConnection urlConnection = null; InputStream inputStream = null; try { urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setReadTimeout(10000 /* milliseconds */); urlConnection.setConnectTimeout(15000 /* milliseconds */); urlConnection.setRequestMethod("GET"); urlConnection.connect(); /* If the request was successful (response code 200), then read the input stream and parse the response. */ if (urlConnection.getResponseCode() == 200) { inputStream = urlConnection.getInputStream(); jsonResponse = readFromStream(inputStream); } else { //handle exception } } catch (IOException e) { //handle exception } finally { if (urlConnection != null) { urlConnection.disconnect(); } if (inputStream != null) { inputStream.close(); } } return jsonResponse; }
From source file:com.abid_mujtaba.bitcoin.tracker.network.Client.java
private static String get(String url_string) throws ClientException { HttpURLConnection connection = null; // NOTE: fetchImage is set up to use HTTP not HTTPS try {/*from w ww.j ava2 s . c om*/ URL url = new URL(url_string); connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(CONNECTION_TIMEOUT); connection.setReadTimeout(READ_TIMEOUT); InputStream is = new BufferedInputStream(connection.getInputStream()); int response_code; if ((response_code = connection.getResponseCode()) != 200) { throw new ClientException("Error code returned by response: " + response_code); } return InputStreamToString(is); } catch (SocketTimeoutException e) { throw new ClientException("Socket timed out.", e); } catch (IOException e) { throw new ClientException("IO Exception raised while attempting to GET response.", e); } finally { if (connection != null) { connection.disconnect(); } } }
From source file:Main.java
static byte[] httpRequest(String url, byte[] requestBytes, int connectionTimeOutMs, int readTimeOutMs) { byte[] bArr = null; if (!(url == null || requestBytes == null)) { InputStream inputStream = null; HttpURLConnection urlConnection = null; try {//w w w . ja v a 2 s. co m urlConnection = (HttpURLConnection) new URL(url).openConnection(); urlConnection.setDoOutput(true); urlConnection.setConnectTimeout(connectionTimeOutMs); urlConnection.setReadTimeout(readTimeOutMs); urlConnection.setFixedLengthStreamingMode(requestBytes.length); OutputStream outputStream = urlConnection.getOutputStream(); outputStream.write(requestBytes); outputStream.close(); if (urlConnection.getResponseCode() != 200) { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { } } if (urlConnection != null) { urlConnection.disconnect(); } } else { inputStream = urlConnection.getInputStream(); ByteArrayOutputStream result = new ByteArrayOutputStream(); byte[] buffer = new byte[16384]; while (true) { int chunkSize = inputStream.read(buffer); if (chunkSize < 0) { break; } else if (chunkSize > 0) { result.write(buffer, 0, chunkSize); } } bArr = result.toByteArray(); if (inputStream != null) { try { inputStream.close(); } catch (IOException e2) { } } if (urlConnection != null) { urlConnection.disconnect(); } } } catch (IOException e3) { if (inputStream != null) { try { inputStream.close(); } catch (IOException e4) { } } if (urlConnection != null) { urlConnection.disconnect(); } } catch (Throwable th) { if (inputStream != null) { try { inputStream.close(); } catch (IOException e5) { } } if (urlConnection != null) { urlConnection.disconnect(); } } } return bArr; }
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 ww w . j a v a2 s. c o m*/ if (origin != null) connection.setRequestProperty("Origin", origin); connection.setConnectTimeout(1000); connection.connect(); return connection; }
From source file:com.mnt.base.util.HttpUtil.java
private static void setTimeout(HttpURLConnection conn) { if (connectionTimeout > 0) { conn.setConnectTimeout(connectionTimeout); }/* w w w .j a va2 s. c o m*/ if (readTimeout > 0) { conn.setReadTimeout(readTimeout); } }
From source file:circleplus.app.http.AbstractHttpApi.java
private static HttpURLConnection getHttpURLConnection(URL url, int requestMethod, boolean acceptJson) throws IOException { if (D)//from www .j a v a2 s . co m Log.d(TAG, "execute method: " + requestMethod + " url: " + url.toString() + " accept JSON ?= " + acceptJson); String method; boolean isPost; switch (requestMethod) { case REQUEST_METHOD_GET: method = "GET"; isPost = false; break; case REQUEST_METHOD_POST: method = "POST"; isPost = true; break; default: method = "GET"; isPost = false; break; } HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(TIMEOUT); conn.setConnectTimeout(TIMEOUT); conn.setRequestProperty("Content-Type", JSON_UTF8_CONTENT_TYPE); if (isPost && acceptJson) { conn.setRequestProperty("Accept", JSON_CONTENT_TYPE); } conn.setRequestMethod(method); /* setDoOutput(true) equals setRequestMethod("POST") */ conn.setDoOutput(isPost); conn.setChunkedStreamingMode(0); // Starts the query conn.connect(); return conn; }
From source file:Main.java
private static InputStream getInputStreamFromUrl_V9(Context paramContext, String paramString) { if (confirmDownload(paramContext, paramString)) { try {//from w ww . j a va 2 s . c o m URL localURL = new URL(paramString); HttpURLConnection localHttpURLConnection2 = (HttpURLConnection) localURL.openConnection(); localHttpURLConnection2.setRequestProperty("Accept-Charset", "UTF-8"); localHttpURLConnection2.setReadTimeout(30000); localHttpURLConnection2.setConnectTimeout(30000); localHttpURLConnection2.setRequestMethod("GET"); localHttpURLConnection2.setDoInput(true); localHttpURLConnection2.connect(); return localHttpURLConnection2.getInputStream(); } catch (Throwable localThrowable) { localThrowable.printStackTrace(); return null; } } else { return null; } }
From source file:Main.java
public static String getResponse(String urlParam) { try {/*from w ww. j ava 2 s . c o m*/ URL url = new URL(urlParam); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE6.0; Windows NT 5.1; SV1)"); conn.setConnectTimeout(5000); conn.setRequestMethod("GET"); if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) { InputStream inputStream = conn.getInputStream(); return inputStream2String(inputStream); } } catch (IOException e) { e.printStackTrace(); return null; } return null; }