List of usage examples for java.net HttpURLConnection setReadTimeout
public void setReadTimeout(int timeout)
From source file:com.epic.framework.implementation.tapjoy.TapjoyURLConnection.java
/** * Performs a network request call to the specified URL and parameters. * //from w w w . j a v a2 s .c o m * @param url The base URL. * @param params The URL parameters. * @return Response from the server. */ public String connectToURL(String url, String params) { String httpResponse = null; BufferedReader rd = null; StringBuilder sb = null; String line = null; try { String requestURL = url + params; // Replaces all spaces. requestURL = requestURL.replaceAll(" ", "%20"); TapjoyLog.i(TAPJOY_URL_CONNECTION, "baseURL: " + url); TapjoyLog.i(TAPJOY_URL_CONNECTION, "requestURL: " + requestURL); // USE java.net HTTP connection instead. URL httpURL = new URL(requestURL); HttpURLConnection connection = (HttpURLConnection) httpURL.openConnection(); connection.setConnectTimeout(15000); connection.setReadTimeout(30000); httpResponse = connection.getResponseMessage(); connection.connect(); // Read the result from the server. rd = new BufferedReader(new InputStreamReader(connection.getInputStream())); sb = new StringBuilder(); while ((line = rd.readLine()) != null) { sb.append(line + '\n'); } httpResponse = sb.toString(); // OLD CODE USING APACHE API // HttpGet http = new HttpGet(requestURL); // // // Create a HttpParams object so we can set our timeout times. // HttpParams httpParameters = new BasicHttpParams(); // // // Time to wait to establish initial connection. // HttpConnectionParams.setConnectionTimeout(httpParameters, 15000); // // // Time to wait for incoming data. // HttpConnectionParams.setSoTimeout(httpParameters, 30000); // // // Create a http client with out timeout settings. // HttpClient client = new DefaultHttpClient(httpParameters); // // HttpResponse response = client.execute(http); // HttpEntity entity = response.getEntity(); // // httpResponse = EntityUtils.toString(entity); TapjoyLog.i(TAPJOY_URL_CONNECTION, "--------------------"); // TapjoyLog.i(TAPJOY_URL_CONNECTION, "response status: " + response.getStatusLine().getStatusCode()); TapjoyLog.i(TAPJOY_URL_CONNECTION, "response size: " + httpResponse.length()); TapjoyLog.i(TAPJOY_URL_CONNECTION, "response: "); TapjoyLog.i(TAPJOY_URL_CONNECTION, "" + httpResponse); TapjoyLog.i(TAPJOY_URL_CONNECTION, "--------------------"); } catch (Exception e) { TapjoyLog.e(TAPJOY_URL_CONNECTION, "Exception: " + e.toString()); } return httpResponse; }
From source file:ar.com.aleatoria.ue.rest.SecureSimpleClientHttpRequestFactory.java
/** * Template method for preparing the given {@link HttpURLConnection}. * <p>/*w w w.j a v a 2 s. com*/ * The default implementation prepares the connection for input and output, and sets the HTTP method. * * @param connection the connection to prepare * @param httpMethod the HTTP request method ({@code GET}, {@code POST}, etc.) * @throws IOException in case of I/O errors */ protected void prepareConnection(HttpURLConnection connection, String httpMethod) throws IOException { if (this.connectTimeout >= 0) { connection.setConnectTimeout(this.connectTimeout); } if (this.readTimeout >= 0) { connection.setReadTimeout(this.readTimeout); } connection.setDoInput(true); if ("GET".equals(httpMethod)) { connection.setInstanceFollowRedirects(true); } else { connection.setInstanceFollowRedirects(false); } if ("PUT".equals(httpMethod) || "POST".equals(httpMethod)) { connection.setDoOutput(true); } else { connection.setDoOutput(false); } connection.setRequestMethod(httpMethod); }
From source file:com.external.volley.toolbox.HurlStack.java
/** * Opens an {@link HttpURLConnection} with parameters. * @param url// w ww . j a v a2 s . c o m * @return an open connection * @throws IOException */ private HttpURLConnection openConnection(URL url, Request<?> request) throws IOException { OkHttpClient okHttpClient = new OkHttpClient(); HttpURLConnection connection = new OkUrlFactory(okHttpClient).open(url); int timeoutMs = request.getTimeoutMs(); connection.setConnectTimeout(timeoutMs); connection.setReadTimeout(timeoutMs); connection.setUseCaches(false); connection.setDoInput(true); connection.setRequestProperty("Charset", "UTF-8"); // ? connection.setRequestProperty("connection", "keep-alive"); // use caller-provided custom SslSocketFactory, if any, for HTTPS if ("https".equals(url.getProtocol()) && mSslSocketFactory != null) { ((HttpsURLConnection) connection).setSSLSocketFactory(mSslSocketFactory); } return connection; }
From source file:com.cydroid.coreframe.web.img.fetcher.ImageFetcher.java
public Bitmap getImage(String path) throws Exception { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(10 * 1000); conn.setConnectTimeout(10 * 1000);//from w w w.ja va2 s.com conn.setRequestMethod("GET"); InputStream is = null; if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) { is = conn.getInputStream(); } else { is = null; } if (is == null) { throw new RuntimeException("stream is null"); } else { try { byte[] data = readStream(is); if (data != null) { Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); return bitmap; } } catch (Exception e) { e.printStackTrace(); } is.close(); return null; } }
From source file:com.hackerati.android.user_sdk.volley.HHurlStack.java
/** * Opens an {@link HttpURLConnection} with parameters. * /*from w ww . ja v a 2s . c o m*/ * @param url * @return an open connection * @throws IOException */ private HttpURLConnection openConnection(final URL url, final Request<?> request) throws IOException { final HttpURLConnection connection = createConnection(url); final int timeoutMs = request.getTimeoutMs(); connection.setConnectTimeout(timeoutMs); connection.setReadTimeout(timeoutMs); connection.setUseCaches(false); connection.setDoInput(true); // use caller-provided custom SslSocketFactory, if any, for HTTPS if ("https".equals(url.getProtocol()) && mSslSocketFactory != null) { ((HttpsURLConnection) connection).setSSLSocketFactory(mSslSocketFactory); } return connection; }
From source file:com.starit.diamond.server.service.NotifyService.java
/** * http get//from www. ja v a2 s. com * * @param urlString * @return */ private String invokeURL(String urlString) { HttpURLConnection conn = null; URL url = null; try { url = new URL(urlString); conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(TIMEOUT); conn.setReadTimeout(TIMEOUT); conn.setRequestMethod("GET"); conn.connect(); InputStream urlStream = conn.getInputStream(); StringBuilder sb = new StringBuilder(); BufferedReader reader = null; try { reader = new BufferedReader(new InputStreamReader(urlStream)); String line = null; while ((line = reader.readLine()) != null) { sb.append(line); } } finally { if (reader != null) reader.close(); } return sb.toString(); } catch (Exception e) { // TODO log.error("http,url=" + urlString, e); } finally { if (conn != null) { conn.disconnect(); } } return "error"; }
From source file:com.wisc.cs407project.ImageLoader.ImageLoader.java
private Bitmap getBitmap(String url) { File f = fileCache.getFile(url); Bitmap b = decodeFile(f);//from ww w .java 2s .c om //from Local Directory BufferedReader in = null; UrlValidator validator = new UrlValidator(); if (new File(url).exists()) { b = decodeFile(new File(url)); if (b != null) return b; } //from web try { //check SD cache first if (b != null) return b; Bitmap bitmap = null; URL imageUrl = new URL(url); HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection(); conn.setConnectTimeout(30000); conn.setReadTimeout(30000); conn.setInstanceFollowRedirects(true); InputStream is = conn.getInputStream(); OutputStream os = new FileOutputStream(f); Utils.CopyStream(is, os); os.close(); conn.disconnect(); bitmap = decodeFile(f); return bitmap; } catch (Throwable ex) { ex.printStackTrace(); if (ex instanceof OutOfMemoryError) memoryCache.clear(); return null; } }
From source file:com.nxt.zyl.data.volley.toolbox.HurlStack.java
/** * Opens an {@link java.net.HttpURLConnection} with parameters. * @param url/* w ww.j a va 2 s.c o m*/ * @return an open connection * @throws java.io.IOException */ private HttpURLConnection openConnection(URL url, Request<?> request) throws IOException { HttpURLConnection connection = createConnection(url); int timeoutMs = request.getTimeoutMs(); connection.setConnectTimeout(CONNECTION_TIME_OUT_MS); connection.setReadTimeout(timeoutMs); connection.setUseCaches(false); connection.setDoInput(true); // use caller-provided custom SslSocketFactory, if any, for HTTPS if ("https".equals(url.getProtocol()) && mSslSocketFactory != null) { ((HttpsURLConnection) connection).setSSLSocketFactory(mSslSocketFactory); } return connection; }
From source file:eu.inmite.apps.smsjizdenka.service.UpdateService.java
private InputStream getIS(int addr, boolean online) throws IOException { if (!online || LOCAL_DEFINITION_TESTING) { if (addr == URL_VERSION_ID) { return getResources().getAssets().open(URL_VERSION); } else if (addr == URL_TICKETS_ID) { return getResources().getAssets().open(URL_TICKETS); } else {/*from www .j ava2 s. c om*/ throw new IOException("requested addr " + addr + " not found"); } } else { URL url; if (addr == URL_VERSION_ID) { url = new URL(Constants.DATA_URL_BASE + URL_VERSION); } else if (addr == URL_TICKETS_ID) { url = new URL(Constants.DATA_URL_BASE + URL_TICKETS); } else { throw new IOException("requested addr " + addr + " not found"); } try { HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setReadTimeout(30000); connection.setRequestMethod("GET"); connection.setDoInput(true); connection.setDoOutput(false); if (connection.getResponseCode() != 200) { connection.disconnect(); throw new IOException("status " + connection.getResponseCode() + " received"); } if (addr == URL_VERSION_ID) { DebugLog.i("Downloading definition version info..."); } else if (addr == URL_TICKETS_ID) { DebugLog.i("Downloading new tickets definition..."); } return connection.getInputStream(); } catch (SecurityException e) { throw new IOException("Internet access denied"); } } }
From source file:com.longwei.project.sso.client.util.HttpClient.java
public boolean isValidEndPoint(final URL url) { HttpURLConnection connection = null; try {/* w w w. ja va 2 s .c o m*/ connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(this.connectionTimeout); connection.setReadTimeout(this.readTimeout); connection.connect(); final int responseCode = connection.getResponseCode(); for (final int acceptableCode : this.acceptableCodes) { if (responseCode == acceptableCode) { if (log.isDebugEnabled()) { log.debug("Response code from server matched " + responseCode + "."); } return true; } } if (log.isDebugEnabled()) { log.debug("Response Code did not match any of the acceptable response codes. Code returned was " + responseCode); } } catch (final IOException e) { log.error(e.getMessage(), e); } finally { if (connection != null) { connection.disconnect(); } } return false; }