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.ljt.openapi.demo.util.HttpUtil.java
/** * HTTP POST?// w w w . ja v a 2s. co m * * @param host * @param path * @param connectTimeout * @param headers * @param querys * @param bodys * @param signHeaderPrefixList * @param appKey * @param appSecret * @return * @throws Exception */ public static Response httpPost(String host, String path, int connectTimeout, Map<String, String> headers, Map<String, String> querys, Map<String, String> bodys, List<String> signHeaderPrefixList, String appKey, String appSecret) throws Exception { if (headers == null) { headers = new HashMap<String, String>(); } headers.put(HttpHeader.HTTP_HEADER_CONTENT_TYPE, ContentType.CONTENT_TYPE_FORM); headers = initialBasicHeader(HttpMethod.POST, path, headers, querys, bodys, signHeaderPrefixList, appKey, appSecret); HttpClient httpClient = wrapClient(host); httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, getTimeout(connectTimeout)); HttpPost post = new HttpPost(initUrl(host, path, querys)); for (Map.Entry<String, String> e : headers.entrySet()) { post.addHeader(e.getKey(), MessageDigestUtil.utf8ToIso88591(e.getValue())); } UrlEncodedFormEntity formEntity = buildFormEntity(bodys); if (formEntity != null) { post.setEntity(formEntity); } return convert(httpClient.execute(post)); }
From source file:com.dgwave.osrs.OsrsClient.java
/** * Create HttpParams from configuration//from www . j a v a 2 s . c o m * @return HttpParams */ private HttpParams getHttpParams() { HttpParams params = new BasicHttpParams(); params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 60000); params.setParameter(CoreConnectionPNames.SO_TIMEOUT, 60000); return params; }
From source file:com.android.wako.net.AsyncHttpGet.java
@Override boolean process() { try {// w w w . j av a 2 s .c om if (parameter != null && parameter.size() > 0) { StringBuilder bulider = new StringBuilder(); for (RequestParameter p : parameter) { if (bulider.length() != 0) { bulider.append("&"); } bulider.append(Utils.encode(p.getName())); bulider.append("="); bulider.append(Utils.encode(p.getValue())); } url += "?" + bulider.toString(); } if (LogUtil.IS_LOG) LogUtil.d(TAG, "AsyncHttpGet request to url :" + url); request = new HttpGet(url); /* * if(Constants.isGzip){ request.addHeader("Accept-Encoding", * "gzip"); }else{ request.addHeader("Accept-Encoding", "default"); * } */ // httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connectTimeout); // ? httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, readTimeout); HttpResponse response = httpClient.execute(request); int statusCode = response.getStatusLine().getStatusCode(); if (LogUtil.IS_LOG) LogUtil.d(TAG, "statusCode=" + statusCode); if (statusCode == HttpStatus.SC_OK) { InputStream is = response.getEntity().getContent(); BufferedInputStream bis = new BufferedInputStream(is); bis.mark(2); // ?? byte[] header = new byte[2]; int result = bis.read(header); // reset?? bis.reset(); // ?GZIP? int headerData = getShort(header); // Gzip ? ? 0x1f8b if (result != -1 && headerData == 0x1f8b) { if (LogUtil.IS_LOG) LogUtil.d(TAG, " use GZIPInputStream "); is = new GZIPInputStream(bis); } else { if (LogUtil.IS_LOG) LogUtil.d(TAG, " not use GZIPInputStream"); is = bis; } InputStreamReader reader = new InputStreamReader(is, "utf-8"); char[] data = new char[100]; int readSize; StringBuffer sb = new StringBuffer(); while ((readSize = reader.read(data)) > 0) { sb.append(data, 0, readSize); } ret = sb.toString(); bis.close(); reader.close(); return true; } else { mRetStatus = ResStatus.Error_Code; RequestException exception = new RequestException(RequestException.IO_EXCEPTION, "??,??" + statusCode); } if (LogUtil.IS_LOG) LogUtil.d(TAG, "AsyncHttpGet request to url :" + url + " finished !"); } catch (IllegalArgumentException e) { mRetStatus = ResStatus.Error_IllegalArgument; e.printStackTrace(); RequestException exception = new RequestException(RequestException.IO_EXCEPTION, Constants.ERROR_MESSAGE); if (LogUtil.IS_LOG) LogUtil.d(TAG, "AsyncHttpGet request to url :" + url + " onFail " + e.getMessage()); } catch (org.apache.http.conn.ConnectTimeoutException e) { mRetStatus = ResStatus.Error_Connect_Timeout; e.printStackTrace(); RequestException exception = new RequestException(RequestException.SOCKET_TIMEOUT_EXCEPTION, Constants.ERROR_MESSAGE); if (LogUtil.IS_LOG) LogUtil.d(TAG, "AsyncHttpGet request to url :" + url + " onFail " + e.getMessage()); } catch (java.net.SocketTimeoutException e) { mRetStatus = ResStatus.Error_Socket_Timeout; e.printStackTrace(); RequestException exception = new RequestException(RequestException.SOCKET_TIMEOUT_EXCEPTION, Constants.ERROR_MESSAGE); if (LogUtil.IS_LOG) LogUtil.d(TAG, "AsyncHttpGet request to url :" + url + " onFail " + e.getMessage()); } catch (UnsupportedEncodingException e) { mRetStatus = ResStatus.Error_Unsupport_Encoding; e.printStackTrace(); RequestException exception = new RequestException(RequestException.UNSUPPORTED_ENCODEING_EXCEPTION, "?"); if (LogUtil.IS_LOG) LogUtil.d(TAG, "AsyncHttpGet request to url :" + url + " UnsupportedEncodingException " + e.getMessage()); } catch (org.apache.http.conn.HttpHostConnectException e) { mRetStatus = ResStatus.Error_HttpHostConnect; e.printStackTrace(); RequestException exception = new RequestException(RequestException.CONNECT_EXCEPTION, ""); if (LogUtil.IS_LOG) LogUtil.d(TAG, "AsyncHttpGet request to url :" + url + " HttpHostConnectException " + e.getMessage()); } catch (ClientProtocolException e) { mRetStatus = ResStatus.Error_Client_Protocol; e.printStackTrace(); RequestException exception = new RequestException(RequestException.CLIENT_PROTOL_EXCEPTION, "??"); e.printStackTrace(); if (LogUtil.IS_LOG) LogUtil.d(TAG, "AsyncHttpGet request to url :" + url + " ClientProtocolException " + e.getMessage()); } catch (IOException e) { mRetStatus = ResStatus.Error_IOException; e.printStackTrace(); RequestException exception = new RequestException(RequestException.IO_EXCEPTION, "??"); e.printStackTrace(); if (LogUtil.IS_LOG) LogUtil.d(TAG, "AsyncHttpGet request to url :" + url + " IOException " + e.getMessage()); } catch (Exception e) { mRetStatus = ResStatus.Error_IOException; e.printStackTrace(); } return false; }
From source file:com.example.aliyundemo.ocr.util.HttpUtil.java
/** * Http POST /*from w ww.j a v a 2 s .c om*/ * * @param url http://host+path+query * @param headers Http * @param body * @param appKey APP KEY * @param appSecret APP * @param timeout * @param signHeaderPrefixList ???Header? * @return * @throws Exception */ public static HttpResponse httpPost(String url, Map<String, String> headers, String body, String appKey, String appSecret, int timeout, List<String> signHeaderPrefixList) throws Exception { headers = initialBasicHeader(headers, appKey, appSecret, HttpMethod.POST, url, null, signHeaderPrefixList); //HttpClient httpClient = new DefaultHttpClient(); HttpClient httpClient = new SSLClient(); httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, getTimeout(timeout)); HttpPost post = new HttpPost(url); for (Map.Entry<String, String> e : headers.entrySet()) { post.addHeader(e.getKey(), MessageDigestUtil.utf8ToIso88591(e.getValue())); } if (StringUtils.isNotBlank(body)) { post.setEntity(new StringEntity(body, Constants.ENCODING)); } return httpClient.execute(post); }
From source file:com.kurento.kmf.content.internal.StreamingProxy.java
/** * After constructor method; it created the HTTP client using configuration * parameters {@link ContentApiConfiguration}. * //from w w w .j a v a 2 s.com * @see ContentApiConfiguration */ @PostConstruct public void afterPropertiesSet() { HttpParams params = new BasicHttpParams(); params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, configuration.getProxyConnectionTimeout()); params.setParameter(CoreConnectionPNames.SO_TIMEOUT, configuration.getProxySocketTimeout()); // Thread-safe configuration (using PoolingClientConnectionManager) SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory())); schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory())); PoolingClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry); cm.setMaxTotal(configuration.getProxyMaxConnections()); cm.setDefaultMaxPerRoute(configuration.getProxyMaxConnectionsPerRoute()); httpClient = new DefaultHttpClient(cm, params); }
From source file:com.alibaba.openapi.client.rpc.AlibabaClientReactor.java
protected HttpParams getHttpParams() { HttpParams params = new BasicHttpParams(); params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 5000) .setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000) .setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, policy.getContentCharset()); HttpConnectionParams.setTcpNoDelay(params, true); HttpConnectionParams.setSocketBufferSize(params, 8192); //HttpConnectionParams.setSoTimeout(params, 0); HttpProtocolParams.setUserAgent(params, "OceanClient/0.1"); return params; }
From source file:org.apache.droids.protocol.http.DroidsHttpClient.java
@Override protected HttpParams createHttpParams() { HttpParams params = new BasicHttpParams(); params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); params.setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, HTTP.DEFAULT_CONTENT_CHARSET); params.setParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false); params.setParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false); params.setIntParameter(CoreConnectionPNames.MAX_HEADER_COUNT, 256); params.setIntParameter(CoreConnectionPNames.MAX_LINE_LENGTH, 5 * 1024); params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 20000); params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 20000); params.setParameter(CoreConnectionPNames.TCP_NODELAY, false); //params.setLongParameter(MAX_BODY_LENGTH, 512 * 1024); return params; }
From source file:com.ryan.ryanreader.cache.CacheManager.java
private CacheManager(final Context context) { if (!isAlreadyInitialized.compareAndSet(false, true)) { throw new RuntimeException("Attempt to initialize the cache twice."); }/*from www. j a v a2 s .co m*/ this.context = context; dbManager = new CacheDbManager(context); requestHandler = new RequestHandlerThread(); // TODo put somewhere else -- make request specific, no restart needed on prefs change! final HttpParams params = new BasicHttpParams(); params.setParameter(CoreProtocolPNames.USER_AGENT, Constants.ua(context)); params.setParameter(CoreConnectionPNames.SO_TIMEOUT, 20000); // TODO remove hardcoded params, put in network prefs params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 20000); params.setParameter(CoreConnectionPNames.MAX_HEADER_COUNT, 100); params.setParameter(ClientPNames.HANDLE_REDIRECTS, true); params.setParameter(ClientPNames.MAX_REDIRECTS, 2); params.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 50); params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRoute() { public int getMaxForRoute(HttpRoute route) { return 25; } }); final SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); final ThreadSafeClientConnManager connManager = new ThreadSafeClientConnManager(params, schemeRegistry); final DefaultHttpClient defaultHttpClient = new DefaultHttpClient(connManager, params); defaultHttpClient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(3, true)); defaultHttpClient.addResponseInterceptor(new HttpResponseInterceptor() { public void process(final HttpResponse response, final HttpContext context) throws HttpException, IOException { final HttpEntity entity = response.getEntity(); final Header encHeader = entity.getContentEncoding(); if (encHeader == null) return; for (final HeaderElement elem : encHeader.getElements()) { if ("gzip".equalsIgnoreCase(elem.getName())) { response.setEntity(new GzipDecompressingEntity(entity)); return; } } } }); downloadQueue = new PrioritisedDownloadQueue(defaultHttpClient); requestHandler.start(); for (int i = 0; i < 5; i++) { // TODO remove constant --- customizable final CacheDownloadThread downloadThread = new CacheDownloadThread(downloadQueue, true); downloadThreads.add(downloadThread); } }
From source file:osh.busdriver.mielegateway.MieleGatewayDispatcher.java
/** * CONSTRUCTOR//w w w . jav a 2 s . c o m * @param logger * @param address * @throws MalformedURLException */ public MieleGatewayDispatcher(String gatewayHostAndPort, String username, String password, OSHGlobalLogger logger) { super(); this.homebusUrl = "http://" + gatewayHostAndPort + "/homebus/?language=en"; this.httpCredsProvider = new BasicCredentialsProvider(); this.httpCredsProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new UsernamePasswordCredentials(username, password)); this.httpcontext = new BasicHttpContext(); this.httpcontext.setAttribute(ClientContext.CREDS_PROVIDER, httpCredsProvider); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory())); ClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry); this.httpclient = new DefaultHttpClient(cm); this.httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); // Default to HTTP 1.1 (connection persistence) this.httpclient.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, "UTF-8"); this.httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 1000); this.httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 1000); this.logger = logger; //this.parser = new MieleGatewayParser("http://" + device + "/homebus"); this.deviceData = new HashMap<Integer, MieleDeviceHomeBusData>(); new Thread(this, "MieleGatewayDispatcher for " + gatewayHostAndPort).start(); }
From source file:com.offbytwo.jenkins.client.JenkinsHttpClient.java
/** * Create an unauthenticated Jenkins HTTP client * * @param uri/*ww w .jav a 2s .c o m*/ * Location of the jenkins server (ex. http://localhost:8080) */ public JenkinsHttpClient(URI uri) { this(uri, HttpClientBuilder.create()); this.context = uri.getPath(); if (!context.endsWith("/")) { context += "/"; } this.uri = uri; this.mapper = getDefaultMapper(); HttpParams httpParams = new BasicHttpParams(); httpParams.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, SO_TIMEOUT_IN_MILLISECONDS); httpParams.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, CONNECTION_TIMEOUT_IN_MILLISECONDS); this.httpResponseValidator = new HttpResponseValidator(); LOGGER.debug("uri={}", uri.toString()); }