List of usage examples for org.apache.http.params HttpConnectionParams setTcpNoDelay
public static void setTcpNoDelay(HttpParams httpParams, boolean z)
From source file:com.example.hnreader.Download.java
/** * Make Http request to the url and return webpage as string; * * @param url/*from ww w. j av a2 s. co m*/ * @return */ private String getPage(String url) { HttpParams httpParameters = new BasicHttpParams(); int timeoutSocket = 5000; HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutSocket); HttpConnectionParams.setTcpNoDelay(httpParameters, true); HttpResponse response; StringBuilder str = new StringBuilder(); HttpClient client = new DefaultHttpClient(httpParameters); HttpGet request = new HttpGet(url); try { response = client.execute(request); InputStream in = response.getEntity().getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); String line; while ((line = reader.readLine()) != null) { str.append(line); } in.close(); } catch (Exception e) { return ""; } return str.toString(); }
From source file:me.nytyr.simplebitmapcache.http.HttpBitmapGetter.java
@Override public Bitmap get(final String URL) { // Making HTTP request try {// w w w .java 2 s .co m final HttpParams httpParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParams, httpTimeout); HttpConnectionParams.setSoTimeout(httpParams, httpTimeout); HttpConnectionParams.setTcpNoDelay(httpParams, true); ConnManagerParams.setTimeout(httpParams, httpTimeout); DefaultHttpClient httpClient = new DefaultHttpClient(httpParams); HttpGet httpPost = new HttpGet(URL); HttpResponse httpResponse = httpClient.execute(httpPost); StatusLine statusLine = httpResponse.getStatusLine(); int statusCode = statusLine.getStatusCode(); HttpEntity httpEntity = httpResponse.getEntity(); final FlushedInputStream is = new FlushedInputStream(httpEntity.getContent()); if (statusCode != 200) { Log.e(TAG, "Error downloading image. Status code > " + statusCode); return null; } return BitmapFactory.decodeStream(is); } catch (Exception e) { e.printStackTrace(); return null; } }
From source file:com.aliyun.oss.common.comm.HttpClientFactory.java
public HttpClient createHttpClient(ClientConfiguration config) { HttpParams httpClientParams = new BasicHttpParams(); HttpProtocolParams.setUserAgent(httpClientParams, config.getUserAgent()); HttpConnectionParams.setConnectionTimeout(httpClientParams, config.getConnectionTimeout()); HttpConnectionParams.setSoTimeout(httpClientParams, config.getSocketTimeout()); HttpConnectionParams.setStaleCheckingEnabled(httpClientParams, true); HttpConnectionParams.setTcpNoDelay(httpClientParams, true); PoolingClientConnectionManager connManager = createConnectionManager(config, httpClientParams); DefaultHttpClient httpClient = new DefaultHttpClient(connManager, httpClientParams); if (System.getProperty("com.aliyun.oss.disableCertChecking") != null) { Scheme sch = new Scheme("https", 443, getSSLSocketFactory()); httpClient.getConnectionManager().getSchemeRegistry().register(sch); }//from w w w. jav a2 s.c om String proxyHost = config.getProxyHost(); int proxyPort = config.getProxyPort(); if (proxyHost != null && proxyPort > 0) { HttpHost proxy = new HttpHost(proxyHost, proxyPort); httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); String proxyUsername = config.getProxyUsername(); String proxyPassword = config.getProxyPassword(); if (proxyUsername != null && proxyPassword != null) { String proxyDomain = config.getProxyDomain(); String proxyWorkstation = config.getProxyWorkstation(); httpClient.getCredentialsProvider().setCredentials(new AuthScope(proxyHost, proxyPort), new NTCredentials(proxyUsername, proxyPassword, proxyWorkstation, proxyDomain)); } } return httpClient; }
From source file:com.msopentech.thali.utilities.universal.CreateClientBuilder.java
/** * * @param host/*from w w w.j ava 2 s. c om*/ * @param port * @param serverPublicKey If null then the server won't be validated * @return */ public HttpClient CreateApacheClient(String host, int port, PublicKey serverPublicKey, KeyStore clientKeyStore, char[] clientKeyStorePassPhrase, Proxy proxy) throws UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException { // sslSocketFactory HttpParams params = new BasicHttpParams(); HttpProtocolParams.setUseExpectContinue(params, false); // Work around for bug in TJWS, it doesn't properly // support ExpectContinue HttpConnectionParams.setSoTimeout(params, timeout); HttpConnectionParams.setConnectionTimeout(params, timeout); HttpConnectionParams.setTcpNoDelay(params, Boolean.TRUE); params.setParameter(ClientPNames.DEFAULT_HOST, new HttpHost(host, port, "https")); HttpKeyHttpClient httpKeyHttpClient = new HttpKeyHttpClient(serverPublicKey, clientKeyStore, clientKeyStorePassPhrase, proxy, params); return httpKeyHttpClient; }
From source file:com.book.jtm.chap03.HttpClientDemo.java
private static HttpParams defaultHttpParams() { HttpParams mDefaultParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(mDefaultParams, 10000); HttpConnectionParams.setSoTimeout(mDefaultParams, 15000); HttpConnectionParams.setTcpNoDelay(mDefaultParams, true); // ?false//from w w w. java 2 s .c o m HttpConnectionParams.setStaleCheckingEnabled(mDefaultParams, false); // ??? HttpProtocolParams.setVersion(mDefaultParams, HttpVersion.HTTP_1_1); // ?? HttpProtocolParams.setUseExpectContinue(mDefaultParams, true); return mDefaultParams; }
From source file:cn.keke.travelmix.HttpClientHelper.java
public static HttpClient getNewHttpClient() { try {/*from w w w. j a v a 2s . c o m*/ SSLSocketFactory sf = new EasySSLSocketFactory(); // TODO test, if SyncBasicHttpParams is needed HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, HTTP.UTF_8); HttpProtocolParams.setUseExpectContinue(params, false); HttpProtocolParams.setHttpElementCharset(params, HTTP.UTF_8); HttpConnectionParams.setConnectionTimeout(params, 10000); HttpConnectionParams.setSocketBufferSize(params, 8192); HttpConnectionParams.setLinger(params, 1); HttpConnectionParams.setStaleCheckingEnabled(params, false); HttpConnectionParams.setSoReuseaddr(params, true); HttpConnectionParams.setTcpNoDelay(params, true); HttpClientParams.setCookiePolicy(params, CookiePolicy.IGNORE_COOKIES); HttpClientParams.setAuthenticating(params, false); HttpClientParams.setRedirecting(params, false); SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory())); registry.register(new Scheme("https", 443, sf)); ThreadSafeClientConnManager ccm = new ThreadSafeClientConnManager(registry, 20, TimeUnit.MINUTES); ccm.setMaxTotal(100); ccm.setDefaultMaxPerRoute(20); return new DefaultHttpClient(ccm, params); } catch (Exception e) { LOG.warn("Failed to create custom http client. Default http client is created", e); return new DefaultHttpClient(); } }
From source file:com.bai.android.data.connection.MapLocationsConnection.java
@Override protected JSONArray doInBackground(String... args) { InputStream is = null;// w w w . j av a 2s.c o m String serverResponse = null; JSONObject json = new JSONObject(); StringEntity entity = null; mapVersion = args[0]; String getDBVersionUri = args[1]; String getLocationsUri = args[2]; HttpParams httpParameters = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParameters, 30000); HttpConnectionParams.setSoTimeout(httpParameters, 15000); HttpConnectionParams.setTcpNoDelay(httpParameters, true); HttpClient httpClient = new DefaultHttpClient(httpParameters); HttpPost httpPost = new HttpPost(getDBVersionUri); try { json.put("pass", "your identification password"); entity = new StringEntity(json.toString()); } catch (JSONException e) { e.printStackTrace(); return null; } catch (UnsupportedEncodingException e) { e.printStackTrace(); return null; } entity.setContentType("application/json;charset=UTF-8"); httpPost.setEntity(entity); try { HttpResponse response = httpClient.execute(httpPost); HttpEntity responseEntity = response.getEntity(); is = responseEntity.getContent(); } catch (Exception e) { return null; } //convert response to string try { BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf8"), 8); serverResponse = reader.readLine(); if (serverResponse.indexOf("404 Not Found") != -1) return null; is.close(); } catch (Exception e) { e.printStackTrace(); return null; } try { JSONArray dbversionArr = new JSONArray(serverResponse); if (dbversionArr != null && dbversionArr.length() > 0) { JSONObject dbversionArrObj = dbversionArr.getJSONObject(0); String currentDbVersion = dbversionArrObj.getString("version"); if (mapVersion == null || !currentDbVersion.equalsIgnoreCase(mapVersion)) { mapVersion = currentDbVersion; } else { //NOTHING TO DO - SAME VERSION return null; } } else { return null; } } catch (JSONException e) { e.printStackTrace(); return null; } // DB VERSION CHECKED AND DIFFENRET - GET LOCATIONS httpPost = new HttpPost(getLocationsUri); httpPost.setEntity(entity); try { HttpResponse response = httpClient.execute(httpPost); HttpEntity responseEntity = response.getEntity(); is = responseEntity.getContent(); } catch (Exception e) { e.printStackTrace(); return null; } //convert response to string try { BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf8"), 8); serverResponse = reader.readLine(); if (serverResponse.indexOf("404 Not Found") != -1) return null; is.close(); } catch (Exception e) { e.printStackTrace(); return null; } try { return new JSONArray(serverResponse); } catch (Exception e) { e.printStackTrace(); return null; } }
From source file:com.weiboa.data.WeiboOAuthConnect.java
public WeiboOAuthConnect(SharedPreferences sp) { super(sp);//from w ww. ja v a2 s . co m // initialize http connection HttpParams parameters = new BasicHttpParams(); HttpProtocolParams.setVersion(parameters, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(parameters, HTTP.DEFAULT_CONTENT_CHARSET); HttpProtocolParams.setUseExpectContinue(parameters, false); HttpConnectionParams.setTcpNoDelay(parameters, true); HttpConnectionParams.setSocketBufferSize(parameters, 8192); SchemeRegistry schReg = new SchemeRegistry(); schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); ClientConnectionManager tsccm = new ThreadSafeClientConnManager(parameters, schReg); mClient = new DefaultHttpClient(tsccm, parameters); mConsumer = new CommonsHttpOAuthConsumer(com.weiboa.oauth.OAuthKeys.CONSUMER_KEY, com.weiboa.oauth.OAuthKeys.CONSUMER_SECRET); loadSavedKeys(sp); }
From source file:ch.cyberduck.core.http.HTTP4Session.java
/** * Create new HTTP client with default configuration and custom trust manager. * * @return A new instance of a default HTTP client. *///from w w w. j av a 2s . c om protected AbstractHttpClient http() { if (null == http) { final HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, org.apache.http.HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, getEncoding()); HttpProtocolParams.setUserAgent(params, getUserAgent()); AuthParams.setCredentialCharset(params, "ISO-8859-1"); HttpConnectionParams.setTcpNoDelay(params, true); HttpConnectionParams.setSoTimeout(params, timeout()); HttpConnectionParams.setSocketBufferSize(params, 8192); HttpClientParams.setRedirecting(params, true); HttpClientParams.setAuthenticating(params, true); SchemeRegistry registry = new SchemeRegistry(); // Always register HTTP for possible use with proxy registry.register(new Scheme("http", host.getPort(), PlainSocketFactory.getSocketFactory())); if ("https".equals(this.getHost().getProtocol().getScheme())) { org.apache.http.conn.ssl.SSLSocketFactory factory = new SSLSocketFactory( new CustomTrustSSLProtocolSocketFactory(this.getTrustManager()).getSSLContext(), org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); registry.register(new Scheme(host.getProtocol().getScheme(), host.getPort(), factory)); } if (Preferences.instance().getBoolean("connection.proxy.enable")) { final Proxy proxy = ProxyFactory.instance(); if ("https".equals(this.getHost().getProtocol().getScheme())) { if (proxy.isHTTPSProxyEnabled(host)) { ConnRouteParams.setDefaultProxy(params, new HttpHost(proxy.getHTTPSProxyHost(host), proxy.getHTTPSProxyPort(host))); } } if ("http".equals(this.getHost().getProtocol().getScheme())) { if (proxy.isHTTPProxyEnabled(host)) { ConnRouteParams.setDefaultProxy(params, new HttpHost(proxy.getHTTPProxyHost(host), proxy.getHTTPProxyPort(host))); } } } ClientConnectionManager manager = new SingleClientConnManager(registry); http = new DefaultHttpClient(manager, params); this.configure(http); } return http; }