List of usage examples for org.apache.http.params CoreConnectionPNames CONNECTION_TIMEOUT
String CONNECTION_TIMEOUT
To view the source code for org.apache.http.params CoreConnectionPNames CONNECTION_TIMEOUT.
Click Source Link
From source file:com.google.wireless.speed.speedometer.measurements.PingTask.java
/** * Use the HTTP Head method to emulate ping. The measurement from this method can be * substantially (2x) greater than the first two methods and inaccurate. This is because, * depending on the implementing of the destination web server, either a quick HTTP * response is replied or some actual heavy lifting will be done in preparing the response * *//*from w w w . j ava 2 s .c o m*/ private MeasurementResult executeHttpPingTask() throws MeasurementError { long pingStartTime = 0; long pingEndTime = 0; ArrayList<Double> rrts = new ArrayList<Double>(); PingDesc pingTask = (PingDesc) this.measurementDesc; String errorMsg = ""; MeasurementResult result = null; try { long totalPingDelay = 0; HttpClient client = AndroidHttpClient.newInstance(Util.prepareUserAgent(this.parent)); HttpHead headMethod = new HttpHead("http://" + targetIp); headMethod.addHeader(new BasicHeader("Connection", "close")); headMethod.setParams(new BasicHttpParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 1000)); int timeOut = (int) (1000 * (double) pingTask.pingTimeoutSec / Config.PING_COUNT_PER_MEASUREMENT); HttpConnectionParams.setConnectionTimeout(headMethod.getParams(), timeOut); for (int i = 0; i < Config.PING_COUNT_PER_MEASUREMENT; i++) { pingStartTime = System.currentTimeMillis(); HttpResponse response = client.execute(headMethod); pingEndTime = System.currentTimeMillis(); rrts.add((double) (pingEndTime - pingStartTime)); this.progress = 100 * i / Config.PING_COUNT_PER_MEASUREMENT; broadcastProgressForUser(progress); } Log.i(SpeedometerApp.TAG, "HTTP get ping succeeds"); double packetLoss = 1 - ((double) rrts.size() / (double) Config.PING_COUNT_PER_MEASUREMENT); result = constructResult(rrts, packetLoss, Config.PING_COUNT_PER_MEASUREMENT); } catch (MalformedURLException e) { Log.e(SpeedometerApp.TAG, e.getMessage()); errorMsg += e.getMessage() + "\n"; } catch (IOException e) { Log.e(SpeedometerApp.TAG, e.getMessage()); errorMsg += e.getMessage() + "\n"; } if (result != null) { return result; } else { Log.i(SpeedometerApp.TAG, "HTTP get ping fails"); throw new MeasurementError(errorMsg); } }
From source file:com.mobiperf.speedometer.measurements.PingTask.java
/** * Use the HTTP Head method to emulate ping. The measurement from this method can be substantially * (2x) greater than the first two methods and inaccurate. This is because, depending on the * implementing of the destination web server, either a quick HTTP response is replied or some * actual heavy lifting will be done in preparing the response * *///from w w w.j ava 2 s.c o m private MeasurementResult executeHttpPingTask() throws MeasurementError { long pingStartTime = 0; long pingEndTime = 0; ArrayList<Double> rrts = new ArrayList<Double>(); PingDesc pingTask = (PingDesc) this.measurementDesc; String errorMsg = ""; MeasurementResult result = null; try { long totalPingDelay = 0; HttpClient client = AndroidHttpClient.newInstance(Util.prepareUserAgent(this.parent)); HttpHead headMethod; headMethod = new HttpHead("http://" + targetIp); headMethod.addHeader(new BasicHeader("Connection", "close")); headMethod.setParams(new BasicHttpParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 1000)); int timeOut = (int) (1000 * (double) pingTask.pingTimeoutSec / Config.PING_COUNT_PER_MEASUREMENT); HttpConnectionParams.setConnectionTimeout(headMethod.getParams(), timeOut); for (int i = 0; i < Config.PING_COUNT_PER_MEASUREMENT; i++) { pingStartTime = System.currentTimeMillis(); HttpResponse response = client.execute(headMethod); pingEndTime = System.currentTimeMillis(); rrts.add((double) (pingEndTime - pingStartTime)); this.progress = 100 * i / Config.PING_COUNT_PER_MEASUREMENT; broadcastProgressForUser(progress); } Logger.i("HTTP get ping succeeds"); double packetLoss = 1 - ((double) rrts.size() / (double) Config.PING_COUNT_PER_MEASUREMENT); result = constructResult(rrts, packetLoss, Config.PING_COUNT_PER_MEASUREMENT); // close the client to avoid memory leak ((AndroidHttpClient) client).close(); } catch (MalformedURLException e) { Logger.e(e.getMessage()); errorMsg += e.getMessage() + "\n"; } catch (IOException e) { Logger.e(e.getMessage()); errorMsg += e.getMessage() + "\n"; } if (result != null) { return result; } else { Logger.i("HTTP get ping fails"); throw new MeasurementError(errorMsg); } }
From source file:org.ellis.yun.search.test.httpclient.HttpClientTest.java
@SuppressWarnings("deprecation") @Test/*from w w w .j a va 2s . c om*/ public void testSSLConnection() throws Exception { Scheme http = new Scheme("http", PlainSocketFactory.getSocketFactory(), 80); SSLSocketFactory ssf = new SSLSocketFactory(SSLContext.getInstance("TLS")); ssf.setHostnameVerifier(SSLSocketFactory.STRICT_HOSTNAME_VERIFIER); Scheme https = new Scheme("https", ssf, 443); SchemeRegistry sr = new SchemeRegistry(); sr.register(http); sr.register(https); TrustManager easyTrustManager = new X509TrustManager() { public void checkClientTrusted(java.security.cert.X509Certificate[] arg0, String arg1) { System.out.println("checkClientTrusted"); } public void checkServerTrusted(java.security.cert.X509Certificate[] arg0, String arg1) { System.out.println("checkServerTrusted"); } public java.security.cert.X509Certificate[] getAcceptedIssuers() { System.out.println("getAcceptedIssuers"); return null; } }; SSLContext sslcontext = SSLContext.getInstance("TLS"); sslcontext.init(null, new TrustManager[] { easyTrustManager }, null); SSLSocketFactory sf = new SSLSocketFactory(sslcontext); SSLSocket socket = (SSLSocket) sf.createSocket(); socket.setEnabledCipherSuites(new String[] { "SSL_RSA_WITH_RC4_128_MD5" }); HttpParams params = new BasicHttpParams(); params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 1000); sf.connectSocket(socket, "119.29.234.42", 443, null, -1, params); }
From source file:org.dasein.cloud.tier3.APIHandler.java
private @Nonnull HttpClient getClient(URI uri) throws InternalException, CloudException { ProviderContext ctx = provider.getContext(); if (ctx == null) { throw new NoContextException(); }/*from w w w . j a v a2 s. c o m*/ boolean ssl = uri.getScheme().startsWith("https"); HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); // noinspection deprecation HttpProtocolParams.setContentCharset(params, Consts.UTF_8.toString()); HttpProtocolParams.setUserAgent(params, "Dasein Cloud"); params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000); params.setParameter(CoreConnectionPNames.SO_TIMEOUT, 300000); Properties p = ctx.getCustomProperties(); if (p != null) { String proxyHost = p.getProperty("proxyHost"); String proxyPort = p.getProperty("proxyPort"); if (proxyHost != null) { int port = 0; if (proxyPort != null && proxyPort.length() > 0) { port = Integer.parseInt(proxyPort); } params.setParameter(ConnRoutePNames.DEFAULT_PROXY, new HttpHost(proxyHost, port, ssl ? "https" : "http")); } } return new DefaultHttpClient(); }
From source file:com.googlecode.jsonrpc4j.JsonRpcHttpAsyncClient.java
private void initialize() { if (initialized.getAndSet(true)) { return;/*w ww. java 2 s. co m*/ } // HTTP parameters for the client final HttpParams params = new BasicHttpParams(); params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, Integer.getInteger("com.googlecode.jsonrpc4j.async.socket.timeout", 30000)); params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, Integer.getInteger("com.googlecode.jsonrpc4j.async.connect.timeout", 30000)); params.setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, Integer.getInteger("com.googlecode.jsonrpc4j.async.socket.buffer", 8 * 1024)); params.setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, Boolean.valueOf(System.getProperty("com.googlecode.jsonrpc4j.async.tcp.nodelay", "true"))); params.setParameter(CoreProtocolPNames.USER_AGENT, "jsonrpc4j/1.0"); // Create client-side I/O reactor final ConnectingIOReactor ioReactor; try { IOReactorConfig config = new IOReactorConfig(); config.setIoThreadCount(Integer.getInteger("com.googlecode.jsonrpc4j.async.reactor.threads", 1)); ioReactor = new DefaultConnectingIOReactor(config); } catch (IOReactorException e) { throw new RuntimeException("Exception initializing asynchronous Apache HTTP Client", e); } // Create a default SSLSetupHandler that accepts any certificate if (sslContext == null) { try { sslContext = SSLContext.getDefault(); } catch (Exception e) { throw new RuntimeException(e); } } // Create HTTP connection pool BasicNIOConnFactory nioConnFactory = new BasicNIOConnFactory(sslContext, null, params); pool = new BasicNIOConnPool(ioReactor, nioConnFactory, params); // Limit total number of connections to 500 by default pool.setDefaultMaxPerRoute(Integer.getInteger("com.googlecode.jsonrpc4j.async.max.inflight.route", 500)); pool.setMaxTotal(Integer.getInteger("com.googlecode.jsonrpc4j.async.max.inflight.total", 500)); // Run the I/O reactor in a separate thread Thread t = new Thread(new Runnable() { public void run() { try { // Create client-side HTTP protocol handler HttpAsyncRequestExecutor protocolHandler = new HttpAsyncRequestExecutor(); // Create client-side I/O event dispatch IOEventDispatch ioEventDispatch = new DefaultHttpClientIODispatch(protocolHandler, sslContext, params); // Ready to go! ioReactor.execute(ioEventDispatch); } catch (InterruptedIOException ex) { System.err.println("Interrupted"); } catch (IOException e) { System.err.println("I/O error: " + e.getMessage()); } } }, "jsonrpc4j HTTP IOReactor"); // Start the client thread t.setDaemon(true); t.start(); // Create HTTP protocol processing chain HttpProcessor httpproc = new ImmutableHttpProcessor(new HttpRequestInterceptor[] { // Use standard client-side protocol interceptors new RequestContent(), new RequestTargetHost(), new RequestConnControl(), new RequestUserAgent(), new RequestExpectContinue() }); // Create HTTP requester requester = new HttpAsyncRequester(httpproc, new DefaultConnectionReuseStrategy(), params); }
From source file:com.sangupta.jerry.http.WebRequest.java
/** * Specify the connection time out to the given value. * * @param timeout/*from w w w.java2 s .co m*/ * the timeout value * * @return this very {@link WebRequest} */ public WebRequest connectTimeout(int timeout) { return config(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout); }
From source file:com.drive.student.xutils.HttpUtils.java
/** * * HttpClient??get?./* www .j a v a2 s .c o m*/ */ public HttpResponse sendHttpGet(String urlpath, boolean isGzip) { HttpClient httpclient = new DefaultHttpClient(); try { HttpGet request = new HttpGet(urlpath); buildHttpHeaderForGet(request);// if (isGzip) { request.addHeader("accept-encoding", "gzip"); } httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, HttpUtils.DEFAULT_CONN_TIMEOUT); // httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, HttpUtils.DEFAULT_SO_TIMEOUT); // ? return httpclient.execute(request, httpContext); } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:org.dasein.cloud.skeleton.RESTMethod.java
private @Nonnull HttpClient getClient(URI uri) throws InternalException, CloudException { ProviderContext ctx = provider.getContext(); if (ctx == null) { throw new NoContextException(); }// www. j av a 2s.c o m boolean ssl = uri.getScheme().startsWith("https"); HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); //noinspection deprecation HttpProtocolParams.setContentCharset(params, HTTP.UTF_8); HttpProtocolParams.setUserAgent(params, "Dasein Cloud"); params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000); params.setParameter(CoreConnectionPNames.SO_TIMEOUT, 300000); Properties p = ctx.getCustomProperties(); if (p != null) { String proxyHost = p.getProperty("proxyHost"); String proxyPort = p.getProperty("proxyPort"); if (proxyHost != null) { int port = 0; if (proxyPort != null && proxyPort.length() > 0) { port = Integer.parseInt(proxyPort); } params.setParameter(ConnRoutePNames.DEFAULT_PROXY, new HttpHost(proxyHost, port, ssl ? "https" : "http")); } } return new DefaultHttpClient(params); }
From source file:com.drive.student.xutils.HttpUtils.java
/** * HttpClient??get?./* w w w .j a v a 2 s . com*/ */ public HttpResponse sendHttpGet(String url, Map params, boolean isGzip) { HttpClient httpclient = new DefaultHttpClient(); httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, HttpUtils.DEFAULT_CONN_TIMEOUT); // httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, HttpUtils.DEFAULT_SO_TIMEOUT); // ? /* HTTPGet */ String paramStr = ""; if (params != null) { for (Object o : params.entrySet()) { Entry entry = (Entry) o; Object key = entry.getKey(); Object val = entry.getValue(); paramStr += paramStr = "&" + key + "=" + val; } } if (!paramStr.equals("")) { paramStr = paramStr.replaceFirst("&", "?"); url += paramStr; } HttpGet request = new HttpGet(url); buildHttpHeaderForGet(request);// if (isGzip) { request.addHeader("accept-encoding", "gzip"); } try { /* ??? */ HttpResponse response = httpclient.execute(request, httpContext); /* ??200 ok */ if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { return response; } } catch (Exception e) { e.printStackTrace(); } return null; }