List of usage examples for org.apache.http.params HttpConnectionParams setSoTimeout
public static void setSoTimeout(HttpParams httpParams, int i)
From source file:gtfsrt.provider.util.GtfsrtProviderImpl.java
/** * This method read the GTFS-RT feed available on the TriMet website, * and create another GTFS-RT feed with some modified trip updates. *///from w ww.ja v a 2 s.c o m private void refreshGTFSRealtime() throws IOException { /* The FeedMessage.Builder is what we will use to build up our GTFS-RT feed */ FeedMessage.Builder tripUpdates = GtfsRealtimeLibrary.createFeedMessageBuilder(); try { /* Read the TriMet GTFS-RT feed */ HttpGet httpget = new HttpGet( "http://developer.trimet.org/ws/V1/TripUpdate/appID/C725DEA31C7A28B860DC29BBA"); HttpParams httpParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_CONNECTION); HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_SOCKET); DefaultHttpClient httpclient = new DefaultHttpClient(); httpclient.setParams(httpParams); HttpResponse response = httpclient.execute(httpget); if (response.getStatusLine().getStatusCode() != 200) return; HttpEntity entity = response.getEntity(); if (entity == null) return; InputStream is = entity.getContent(); if (is != null) { // Decode message FeedMessage feedMessage = FeedMessage.parseFrom(is); List<FeedEntity> feedEntityList = feedMessage.getEntityList(); // Header tripUpdates.setHeader(feedMessage.getHeader()); for (FeedEntity feedEntity : feedEntityList) { if (feedEntity.hasTripUpdate()) { TripUpdate update = feedEntity.getTripUpdate(); TripDescriptor trip = update.getTrip(); /* Deleting trip updates already contained in the original feed */ if (trips.containsKey(trip.getTripId())) continue; /** * Create a new feed entity to wrap the trip update and add it to the * GTFS-realtime trip updates feed. */ FeedEntity.Builder tripUpdateEntity = FeedEntity.newBuilder(); tripUpdateEntity.setId(feedEntity.getId()); tripUpdateEntity.setTripUpdate(update); tripUpdates.addEntity(tripUpdateEntity); } } Trip t; TripDescriptor.Builder tripDescriptor; TripUpdate.Builder tripUpdate; FeedEntity.Builder tripUpdateEntity; for (String s : trips.keySet()) { t = trips.get(s); tripDescriptor = TripDescriptor.newBuilder(); tripDescriptor.setTripId(s); tripUpdate = addDelayForTrip(tripDescriptor, t.getDelay(), t.getNb_sequences()); // update.getStopTimeUpdateCount()) ; tripUpdateEntity = FeedEntity.newBuilder(); tripUpdateEntity.setId(tripDescriptor.getTripId()); tripUpdateEntity.setTripUpdate(tripUpdate); tripUpdates.addEntity(tripUpdateEntity); } } } catch (Exception e) { System.err.println("Error while loading GTFS RT feed"); } /* Build out the final GTFS-realtime feed messages and save them */ _gtfsRealtimeProvider.setTripUpdates(tripUpdates.build()); }
From source file:www.image.ImageManager.java
/** * Downloads a file/*from w w w . j a va 2 s.com*/ * @param url * @return * @throws IOException */ public Bitmap fetchImage(String url) throws IOException { Bitmap mbitmap = null; try { HttpGet get = new HttpGet(url); HttpConnectionParams.setConnectionTimeout(get.getParams(), CONNECTION_TIMEOUT_MS); HttpConnectionParams.setSoTimeout(get.getParams(), SOCKET_TIMEOUT_MS); HttpResponse response = null; response = mClient.execute(get); HttpEntity entity = response.getEntity(); BufferedInputStream bis = new BufferedInputStream(entity.getContent(), 8 * 1024); mbitmap = scaleBitmap(bis, 120, 120); bis.close(); if (response.getStatusLine().getStatusCode() != 200) { mbitmap = mFailBitmap; } } catch (ClientProtocolException e) { throw new IOException("Invalid client protocol."); } catch (Exception e) { e.printStackTrace(); } return mbitmap; }
From source file:com.LaunchKeyManager.http.AsyncHttpClient.java
/** * Creates a new AsyncHttpClient./* w ww . ja va 2 s. c om*/ */ public AsyncHttpClient() { BasicHttpParams httpParams = new BasicHttpParams(); ConnManagerParams.setTimeout(httpParams, socketTimeout); ConnManagerParams.setMaxConnectionsPerRoute(httpParams, new ConnPerRouteBean(maxConnections)); ConnManagerParams.setMaxTotalConnections(httpParams, DEFAULT_MAX_CONNECTIONS); HttpConnectionParams.setSoTimeout(httpParams, socketTimeout); HttpConnectionParams.setConnectionTimeout(httpParams, socketTimeout); HttpConnectionParams.setTcpNoDelay(httpParams, true); HttpConnectionParams.setSocketBufferSize(httpParams, DEFAULT_SOCKET_BUFFER_SIZE); HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1); HttpProtocolParams.setUserAgent(httpParams, String.format("android-async-http/%s (http://loopj.com/android-async-http)", VERSION)); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); schemeRegistry.register(new Scheme("https", this.workAroundReverseDnsBugInHoneycombAndEarlier(), 443)); // schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(httpParams, schemeRegistry); httpContext = new SyncBasicHttpContext(new BasicHttpContext()); httpClient = new DefaultHttpClient(cm, httpParams); httpClient.addRequestInterceptor(new HttpRequestInterceptor() { @Override public void process(HttpRequest request, HttpContext context) { if (!request.containsHeader(HEADER_ACCEPT_ENCODING)) { request.addHeader(HEADER_ACCEPT_ENCODING, ENCODING_GZIP); } for (String header : clientHeaderMap.keySet()) { request.addHeader(header, clientHeaderMap.get(header)); } } }); httpClient.addResponseInterceptor(new HttpResponseInterceptor() { @Override public void process(HttpResponse response, HttpContext context) { final HttpEntity entity = response.getEntity(); if (entity == null) { return; } final Header encoding = entity.getContentEncoding(); if (encoding != null) { for (HeaderElement element : encoding.getElements()) { if (element.getName().equalsIgnoreCase(ENCODING_GZIP)) { response.setEntity(new InflatingEntity(response.getEntity())); break; } } } } }); httpClient.setHttpRequestRetryHandler(new RetryHandler(DEFAULT_MAX_RETRIES)); threadPool = (ThreadPoolExecutor) Executors.newCachedThreadPool(); requestMap = new WeakHashMap<Context, List<WeakReference<Future<?>>>>(); clientHeaderMap = new HashMap<String, String>(); }
From source file:com.up.testjavasdkdemo.ssltest.Http.java
public Http(Context context) { BasicHttpParams httpParams = new BasicHttpParams(); ConnManagerParams.setTimeout(httpParams, socketTimeout); ConnManagerParams.setMaxConnectionsPerRoute(httpParams, new ConnPerRouteBean(maxConnections)); ConnManagerParams.setMaxTotalConnections(httpParams, 10); HttpConnectionParams.setSoTimeout(httpParams, socketTimeout); HttpConnectionParams.setConnectionTimeout(httpParams, socketTimeout); HttpConnectionParams.setTcpNoDelay(httpParams, true); HttpConnectionParams.setSocketBufferSize(httpParams, DEFAULT_SOCKET_BUFFER_SIZE); HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(httpParams, schemeRegistry); httpContext = new SyncBasicHttpContext(new BasicHttpContext()); httpClient = new DefaultHttpClient(cm, httpParams); httpClient.addRequestInterceptor(new HttpRequestInterceptor() { public void process(HttpRequest request, HttpContext context) { if (!request.containsHeader(HEADER_ACCEPT_ENCODING)) { request.addHeader(HEADER_ACCEPT_ENCODING, ENCODING_GZIP); }// ww w.j av a 2 s . c o m for (String header : clientHeaderMap.keySet()) { request.addHeader(header, clientHeaderMap.get(header)); } } }); httpClient.addResponseInterceptor(new HttpResponseInterceptor() { public void process(HttpResponse response, HttpContext context) { final HttpEntity entity = response.getEntity(); if (entity == null) { return; } final Header encoding = entity.getContentEncoding(); if (encoding != null) { for (HeaderElement element : encoding.getElements()) { if (element.getName().equalsIgnoreCase(ENCODING_GZIP)) { response.setEntity(new InflatingEntity(response.getEntity())); break; } } } } }); clientHeaderMap = new HashMap<String, String>(); }
From source file:com.lightbox.android.network.HttpHelper.java
/** * Create an HttpClient.//from ww w .j a v a2s . c om * @return a properly set HttpClient */ private static DefaultHttpClient createHttpClient() { HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme(STRING_HTTP, PlainSocketFactory.getSocketFactory(), 80)); schemeRegistry.register(new Scheme(STRING_HTTPS, SSLSocketFactory.getSocketFactory(), 443)); HttpConnectionParams.setConnectionTimeout(params, HTTP_TIMEOUT); HttpConnectionParams.setSoTimeout(params, HTTP_TIMEOUT); ConnManagerParams.setTimeout(params, HTTP_TIMEOUT); HttpConnectionParams.setSocketBufferSize(params, SOCKET_BUFFER_SIZE); HttpProtocolParams.setContentCharset(params, HTTP.UTF_8); HttpProtocolParams.setHttpElementCharset(params, HTTP.UTF_8); HttpProtocolParams.setUserAgent(params, String.format(USER_AGENT_FORMAT_STRING, AndroidUtils.getApplicationLabel(), AndroidUtils.getVersionCode(), android.os.Build.VERSION.RELEASE, android.os.Build.MODEL)); DefaultHttpClient client = new DefaultHttpClient(new ThreadSafeClientConnManager(params, schemeRegistry), params); enableGzipCompression(client); return client; }
From source file:com.my.cloudcontact.http.FinalHttp.java
public FinalHttp() { BasicHttpParams httpParams = new BasicHttpParams(); ConnManagerParams.setTimeout(httpParams, socketTimeout); ConnManagerParams.setMaxConnectionsPerRoute(httpParams, new ConnPerRouteBean(maxConnections)); ConnManagerParams.setMaxTotalConnections(httpParams, 10); HttpConnectionParams.setSoTimeout(httpParams, socketTimeout); HttpConnectionParams.setConnectionTimeout(httpParams, socketTimeout); HttpConnectionParams.setTcpNoDelay(httpParams, true); HttpConnectionParams.setSocketBufferSize(httpParams, DEFAULT_SOCKET_BUFFER_SIZE); HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(httpParams, schemeRegistry); httpContext = new SyncBasicHttpContext(new BasicHttpContext()); httpClient = new DefaultHttpClient(cm, httpParams); httpClient.addRequestInterceptor(new HttpRequestInterceptor() { public void process(HttpRequest request, HttpContext context) { if (!request.containsHeader(HEADER_ACCEPT_ENCODING)) { request.addHeader(HEADER_ACCEPT_ENCODING, ENCODING_GZIP); }/*from w w w . j av a 2 s . c om*/ // initClientHeader(requestHeader); // for (String header : clientHeaderMap.keySet()) { // request.addHeader(header, clientHeaderMap.get(header)); // } } }); httpClient.addResponseInterceptor(new HttpResponseInterceptor() { public void process(HttpResponse response, HttpContext context) { final HttpEntity entity = response.getEntity(); if (entity == null) { return; } final Header encoding = entity.getContentEncoding(); if (encoding != null) { for (HeaderElement element : encoding.getElements()) { if (element.getName().equalsIgnoreCase(ENCODING_GZIP)) { response.setEntity(new InflatingEntity(response.getEntity())); break; } } } } }); httpClient.setHttpRequestRetryHandler(new RetryHandler(maxRetries)); clientHeaderMap = new HashMap<String, String>(); }
From source file:net.bither.http.BaseHttpResponse.java
private DefaultHttpClient getThreadSafeHttpClient() { if (getHttpType() == HttpType.BitherApi) { PersistentCookieStore persistentCookieStore = PersistentCookieStore.getInstance(); if (persistentCookieStore.getCookies() == null || persistentCookieStore.getCookies().size() == 0) { CookieFactory.initCookie();/*ww w . ja va 2 s .co m*/ } } DefaultHttpClient httpClient = new DefaultHttpClient(); ClientConnectionManager mgr = httpClient.getConnectionManager(); HttpParams params = httpClient.getParams(); HttpConnectionParams.setConnectionTimeout(params, HttpSetting.HTTP_CONNECTION_TIMEOUT); HttpConnectionParams.setSoTimeout(params, HttpSetting.HTTP_SO_TIMEOUT); httpClient = new DefaultHttpClient(new ThreadSafeClientConnManager(params, mgr.getSchemeRegistry()), params); setCookieStore(httpClient); return httpClient; }
From source file:free.yhc.netmbuddy.model.NetLoader.java
private HttpClient newHttpClient(String proxyAddr) { if (isValidProxyAddr(proxyAddr)) { // TODO/*from w w w . ja v a2s.c o m*/ // Not supported yet. eAssert(false); return null; } HttpClient hc = new DefaultHttpClient(); HttpParams params = hc.getParams(); HttpConnectionParams.setConnectionTimeout(params, Policy.NETWORK_CONN_TIMEOUT); HttpConnectionParams.setSoTimeout(params, Policy.NETWORK_CONN_TIMEOUT); HttpProtocolParams.setUserAgent(hc.getParams(), Policy.HTTP_UASTRING); params.setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109); return hc; }
From source file:com.mygame.myfellowship.http.FinalHttp.java
public FinalHttp() { BasicHttpParams httpParams = new BasicHttpParams(); ConnManagerParams.setTimeout(httpParams, socketTimeout); ConnManagerParams.setMaxConnectionsPerRoute(httpParams, new ConnPerRouteBean(maxConnections)); ConnManagerParams.setMaxTotalConnections(httpParams, 10); HttpConnectionParams.setSoTimeout(httpParams, socketTimeout); HttpConnectionParams.setConnectionTimeout(httpParams, socketTimeout); HttpConnectionParams.setTcpNoDelay(httpParams, true); HttpConnectionParams.setSocketBufferSize(httpParams, DEFAULT_SOCKET_BUFFER_SIZE); HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(httpParams, schemeRegistry); httpContext = new SyncBasicHttpContext(new BasicHttpContext()); httpClient = new DefaultHttpClient(cm, httpParams); httpClient.setParams(httpParams);// ww w . jav a 2 s . c o m httpClient.addRequestInterceptor(new HttpRequestInterceptor() { public void process(HttpRequest request, HttpContext context) { if (!request.containsHeader(HEADER_ACCEPT_ENCODING)) { request.addHeader(HEADER_ACCEPT_ENCODING, ENCODING_GZIP); } // initClientHeader(requestHeader); // for (String header : clientHeaderMap.keySet()) { // request.addHeader(header, clientHeaderMap.get(header)); // } } }); httpClient.addResponseInterceptor(new HttpResponseInterceptor() { public void process(HttpResponse response, HttpContext context) { final HttpEntity entity = response.getEntity(); if (entity == null) { return; } final Header encoding = entity.getContentEncoding(); if (encoding != null) { for (HeaderElement element : encoding.getElements()) { if (element.getName().equalsIgnoreCase(ENCODING_GZIP)) { response.setEntity(new InflatingEntity(response.getEntity())); break; } } } } }); httpClient.setHttpRequestRetryHandler(new RetryHandler(maxRetries)); clientHeaderMap = new HashMap<String, String>(); }
From source file:com.dwdesign.tweetings.util.httpclient.HttpClientImpl.java
public HttpClientImpl(final HttpClientConfiguration conf) { this.conf = conf; final SchemeRegistry registry = new SchemeRegistry(); //final SSLSocketFactory factory = conf.isSSLErrorIgnored() ? TRUST_ALL_SSL_SOCKET_FACTORY : SSLSocketFactory // .getSocketFactory(); final SSLSocketFactory factory = TRUST_ALL_SSL_SOCKET_FACTORY; registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); registry.register(new Scheme("https", factory, 443)); final HttpParams params = new BasicHttpParams(); final ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(params, registry); final DefaultHttpClient client = new DefaultHttpClient(cm, params); final HttpParams client_params = client.getParams(); HttpConnectionParams.setConnectionTimeout(client_params, conf.getHttpConnectionTimeout()); HttpConnectionParams.setSoTimeout(client_params, conf.getHttpReadTimeout()); if (conf.getHttpProxyHost() != null && !conf.getHttpProxyHost().equals("")) { final HttpHost proxy = new HttpHost(conf.getHttpProxyHost(), conf.getHttpProxyPort()); client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); if (conf.getHttpProxyUser() != null && !conf.getHttpProxyUser().equals("")) { if (logger.isDebugEnabled()) { logger.debug("Proxy AuthUser: " + conf.getHttpProxyUser()); logger.debug("Proxy AuthPassword: " + z_T4JInternalStringUtil.maskString(conf.getHttpProxyPassword())); }//from ww w. java2 s . c o m client.getCredentialsProvider().setCredentials( new AuthScope(conf.getHttpProxyHost(), conf.getHttpProxyPort()), new UsernamePasswordCredentials(conf.getHttpProxyUser(), conf.getHttpProxyPassword())); } } this.client = client; }