本文整理汇总了Java中org.apache.http.params.HttpConnectionParams.setTcpNoDelay方法的典型用法代码示例。如果您正苦于以下问题:Java HttpConnectionParams.setTcpNoDelay方法的具体用法?Java HttpConnectionParams.setTcpNoDelay怎么用?Java HttpConnectionParams.setTcpNoDelay使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.http.params.HttpConnectionParams
的用法示例。
在下文中一共展示了HttpConnectionParams.setTcpNoDelay方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: get
import org.apache.http.params.HttpConnectionParams; //导入方法依赖的package包/类
public static HttpClient get() {
HttpParams httpParams = new BasicHttpParams();
ConnManagerParams.setTimeout(httpParams, 3000);
ConnManagerParams.setMaxConnectionsPerRoute(httpParams, new ConnPerRouteBean(30));
ConnManagerParams.setMaxTotalConnections(httpParams, 30);
HttpClientParams.setRedirecting(httpParams, true);
HttpProtocolParams.setUseExpectContinue(httpParams, true);
HttpConnectionParams.setStaleCheckingEnabled(httpParams, false);
HttpConnectionParams.setSoTimeout(httpParams, 2000);
HttpConnectionParams.setConnectionTimeout(httpParams, 2000);
HttpConnectionParams.setTcpNoDelay(httpParams, true);
HttpConnectionParams.setSocketBufferSize(httpParams, 8192);
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme(IDataSource.SCHEME_HTTP_TAG, PlainSocketFactory.getSocketFactory(), 80));
try {
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(null, null);
SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
schemeRegistry.register(new Scheme(IDataSource.SCHEME_HTTPS_TAG, sf, 443));
} catch (Exception ex) {
ex.printStackTrace();
}
return new DefaultHttpClient(new ThreadSafeClientConnManager(httpParams, schemeRegistry), httpParams);
}
示例2: createHttpClient
import org.apache.http.params.HttpConnectionParams; //导入方法依赖的package包/类
/**
* 设置默认请求参数,并返回HttpClient
*
* @return HttpClient
*/
private HttpClient createHttpClient() {
HttpParams mDefaultHttpParams = new BasicHttpParams();
//设置连接超时
HttpConnectionParams.setConnectionTimeout(mDefaultHttpParams, 15000);
//设置请求超时
HttpConnectionParams.setSoTimeout(mDefaultHttpParams, 15000);
HttpConnectionParams.setTcpNoDelay(mDefaultHttpParams, true);
HttpProtocolParams.setVersion(mDefaultHttpParams, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(mDefaultHttpParams, HTTP.UTF_8);
//持续握手
HttpProtocolParams.setUseExpectContinue(mDefaultHttpParams, true);
HttpClient mHttpClient = new DefaultHttpClient(mDefaultHttpParams);
return mHttpClient;
}
示例3: createHttpParams
import org.apache.http.params.HttpConnectionParams; //导入方法依赖的package包/类
private HttpParams createHttpParams() {
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, "UTF-8");
HttpProtocolParams.setUseExpectContinue(params, false);
ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRouteBean(3));
ConnManagerParams.setMaxTotalConnections(params, 3);
ConnManagerParams.setTimeout(params, 1000);
HttpConnectionParams.setConnectionTimeout(params, 30000);
HttpConnectionParams.setSoTimeout(params, 30000);
HttpConnectionParams.setStaleCheckingEnabled(params, false);
HttpConnectionParams.setTcpNoDelay(params, true);
HttpConnectionParams.setSocketBufferSize(params, 8192);
HttpClientParams.setRedirecting(params, false);
return params;
}
示例4: createHttpParams
import org.apache.http.params.HttpConnectionParams; //导入方法依赖的package包/类
private static HttpParams createHttpParams() {
final HttpParams params = new BasicHttpParams();
// 设置是否启用旧连接检查,默认是开启的。关闭这个旧连接检查可以提高一点点性能,但是增加了I/O错误的风险(当服务端关闭连接时)。
// 开启这个选项则在每次使用老的连接之前都会检查连接是否可用,这个耗时大概在15-30ms之间
HttpConnectionParams.setStaleCheckingEnabled(params, false);
HttpConnectionParams.setConnectionTimeout(params, TIMEOUT);// 设置链接超时时间
HttpConnectionParams.setSoTimeout(params, TIMEOUT);// 设置socket超时时间
HttpConnectionParams.setSocketBufferSize(params, SOCKET_BUFFER_SIZE);// 设置缓存大小
HttpConnectionParams.setTcpNoDelay(params, true);// 是否不使用延迟发送(true为不延迟)
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); // 设置协议版本
HttpProtocolParams.setUseExpectContinue(params, true);// 设置异常处理机制
HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);// 设置编码
HttpClientParams.setRedirecting(params, false);// 设置是否采用重定向
ConnManagerParams.setTimeout(params, TIMEOUT);// 设置超时
ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRouteBean(MAX_CONNECTIONS));// 多线程最大连接数
ConnManagerParams.setMaxTotalConnections(params, 10); // 多线程总连接数
return params;
}
示例5: setDefaultHttpParams
import org.apache.http.params.HttpConnectionParams; //导入方法依赖的package包/类
/**
* Saves the default set of HttpParams in the provided parameter.
* These are:
* <ul>
* <li>{@link CoreProtocolPNames#PROTOCOL_VERSION}: 1.1</li>
* <li>{@link CoreProtocolPNames#HTTP_CONTENT_CHARSET}: ISO-8859-1</li>
* <li>{@link CoreConnectionPNames#TCP_NODELAY}: true</li>
* <li>{@link CoreConnectionPNames#SOCKET_BUFFER_SIZE}: 8192</li>
* <li>{@link CoreProtocolPNames#USER_AGENT}: Apache-HttpClient/<release> (java 1.5)</li>
* </ul>
*/
public static void setDefaultHttpParams(HttpParams params) {
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, HTTP.DEF_CONTENT_CHARSET.name());
HttpConnectionParams.setTcpNoDelay(params, true);
HttpConnectionParams.setSocketBufferSize(params, 8192);
// determine the release version from packaged version info
final VersionInfo vi = VersionInfo.loadVersionInfo
("org.apache.http.client", DefaultHttpClient.class.getClassLoader());
final String release = (vi != null) ?
vi.getRelease() : VersionInfo.UNAVAILABLE;
HttpProtocolParams.setUserAgent(params,
"Apache-HttpClient/" + release + " (java 1.5)");
}
示例6: initSSLWithHttpClinet
import org.apache.http.params.HttpConnectionParams; //导入方法依赖的package包/类
public static JSONObject initSSLWithHttpClinet(String path)
throws ClientProtocolException, IOException {
HTTPSTrustManager.allowAllSSL();
JSONObject jsonObject = null;
int timeOut = 30 * 1000;
HttpParams param = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(param, timeOut);
HttpConnectionParams.setSoTimeout(param, timeOut);
HttpConnectionParams.setTcpNoDelay(param, true);
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory .getSocketFactory(), 80));
registry.register(new Scheme("https", TrustAllSSLSocketFactory .getDefault(), 443));
ClientConnectionManager manager = new ThreadSafeClientConnManager( param, registry);
DefaultHttpClient client = new DefaultHttpClient(manager, param);
HttpGet request = new HttpGet(path);
// HttpGet request = new HttpGet("https://www.alipay.com/");
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
BufferedReader reader = new BufferedReader(new InputStreamReader( entity.getContent()));
StringBuilder result = new StringBuilder();
String line = "";
while ((line = reader.readLine()) != null) {
result.append(line);
try {
jsonObject = new JSONObject(line);
} catch (JSONException e) {
e.printStackTrace();
Log.e("HTTPS TEST", e.toString());
}
}
Log.e("HTTPS TEST", result.toString());
return jsonObject;
}
示例7: doRefer
import org.apache.http.params.HttpConnectionParams; //导入方法依赖的package包/类
protected <T> T doRefer(Class<T> serviceType, URL url) throws RpcException {
if (connectionMonitor == null) {
connectionMonitor = new ConnectionMonitor();
}
// TODO more configs to add
PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager();
// 20 is the default maxTotal of current PoolingClientConnectionManager
connectionManager.setMaxTotal(url.getParameter(Constants.CONNECTIONS_KEY, 20));
connectionManager.setDefaultMaxPerRoute(url.getParameter(Constants.CONNECTIONS_KEY, 20));
connectionMonitor.addConnectionManager(connectionManager);
// BasicHttpContext localContext = new BasicHttpContext();
DefaultHttpClient httpClient = new DefaultHttpClient(connectionManager);
httpClient.setKeepAliveStrategy(new ConnectionKeepAliveStrategy() {
public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
HeaderElementIterator it = new BasicHeaderElementIterator(response.headerIterator(HTTP.CONN_KEEP_ALIVE));
while (it.hasNext()) {
HeaderElement he = it.nextElement();
String param = he.getName();
String value = he.getValue();
if (value != null && param.equalsIgnoreCase("timeout")) {
return Long.parseLong(value) * 1000;
}
}
// TODO constant
return 30 * 1000;
}
});
HttpParams params = httpClient.getParams();
// TODO currently no xml config for Constants.CONNECT_TIMEOUT_KEY so we directly reuse Constants.TIMEOUT_KEY for now
HttpConnectionParams.setConnectionTimeout(params, url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT));
HttpConnectionParams.setSoTimeout(params, url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT));
HttpConnectionParams.setTcpNoDelay(params, true);
HttpConnectionParams.setSoKeepalive(params, true);
ApacheHttpClient4Engine engine = new ApacheHttpClient4Engine(httpClient/*, localContext*/);
ResteasyClient client = new ResteasyClientBuilder().httpEngine(engine).build();
clients.add(client);
client.register(RpcContextFilter.class);
for (String clazz : Constants.COMMA_SPLIT_PATTERN.split(url.getParameter(Constants.EXTENSION_KEY, ""))) {
if (!StringUtils.isEmpty(clazz)) {
try {
client.register(Thread.currentThread().getContextClassLoader().loadClass(clazz.trim()));
} catch (ClassNotFoundException e) {
throw new RpcException("Error loading JAX-RS extension class: " + clazz.trim(), e);
}
}
}
// TODO protocol
ResteasyWebTarget target = client.target("http://" + url.getHost() + ":" + url.getPort() + "/" + getContextPath(url));
return target.proxy(serviceType);
}
示例8: doRefer
import org.apache.http.params.HttpConnectionParams; //导入方法依赖的package包/类
protected <T> T doRefer(Class<T> serviceType, URL url) throws RpcException {
if (connectionMonitor == null) {
connectionMonitor = new ConnectionMonitor();
}
// TODO more configs to add
PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager();
// 20 is the default maxTotal of current PoolingClientConnectionManager
connectionManager.setMaxTotal(url.getParameter(Constants.CONNECTIONS_KEY, 20));
connectionManager.setDefaultMaxPerRoute(url.getParameter(Constants.CONNECTIONS_KEY, 20));
connectionMonitor.addConnectionManager(connectionManager);
// BasicHttpContext localContext = new BasicHttpContext();
DefaultHttpClient httpClient = new DefaultHttpClient(connectionManager);
httpClient.setKeepAliveStrategy(new ConnectionKeepAliveStrategy() {
public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
HeaderElementIterator it = new BasicHeaderElementIterator(response.headerIterator(HTTP.CONN_KEEP_ALIVE));
while (it.hasNext()) {
HeaderElement he = it.nextElement();
String param = he.getName();
String value = he.getValue();
if (value != null && param.equalsIgnoreCase("timeout")) {
return Long.parseLong(value) * 1000;
}
}
// TODO constant
return 30 * 1000;
}
});
HttpParams params = httpClient.getParams();
// TODO currently no xml config for Constants.CONNECT_TIMEOUT_KEY so we directly reuse Constants.TIMEOUT_KEY for now
HttpConnectionParams.setConnectionTimeout(params, url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT));
HttpConnectionParams.setSoTimeout(params, url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT));
HttpConnectionParams.setTcpNoDelay(params, true);
HttpConnectionParams.setSoKeepalive(params, true);
ApacheHttpClient4Engine engine = new ApacheHttpClient4Engine(httpClient/* , localContext */);
ResteasyClient client = new ResteasyClientBuilder().httpEngine(engine).build();
clients.add(client);
client.register(RpcContextFilter.class);
for (String clazz : Constants.COMMA_SPLIT_PATTERN.split(url.getParameter(Constants.EXTENSION_KEY, ""))) {
if (!StringUtils.isEmpty(clazz)) {
try {
client.register(Thread.currentThread().getContextClassLoader().loadClass(clazz.trim()));
} catch (ClassNotFoundException e) {
throw new RpcException("Error loading JAX-RS extension class: " + clazz.trim(), e);
}
}
}
// dubbo 服务多版本
String version = url.getParameter(Constants.VERSION_KEY);
String versionPath = "";
if (StringUtils.isNotEmpty(version)) {
versionPath = version + "/";
}
// TODO protocol
ResteasyWebTarget target = client.target("http://" + url.getHost() + ":" + url.getPort() + "/" + versionPath + getContextPath(url));
return target.proxy(serviceType);
}
示例9: createHttpClient
import org.apache.http.params.HttpConnectionParams; //导入方法依赖的package包/类
/**
* Creates a new HttpClient object using the specified AWS
* ClientConfiguration to configure the client.
*
* @param config
* Client configuration options (ex: proxy settings, connection
* limits, etc).
*
* @return The new, configured HttpClient.
*/
@SuppressWarnings("deprecation")
public HttpClient createHttpClient(ClientConfiguration config) {
/* Set HTTP client parameters */
HttpParams httpClientParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpClientParams, config.getConnectionTimeout());
HttpConnectionParams.setSoTimeout(httpClientParams, config.getSocketTimeout());
HttpConnectionParams.setStaleCheckingEnabled(httpClientParams, true);
HttpConnectionParams.setTcpNoDelay(httpClientParams, true);
int socketSendBufferSizeHint = config.getSocketBufferSizeHints()[0];
int socketReceiveBufferSizeHint = config.getSocketBufferSizeHints()[1];
if (socketSendBufferSizeHint > 0 || socketReceiveBufferSizeHint > 0) {
HttpConnectionParams.setSocketBufferSize(httpClientParams,
Math.max(socketSendBufferSizeHint, socketReceiveBufferSizeHint));
}
PoolingClientConnectionManager connectionManager = ConnectionManagerFactory
.createPoolingClientConnManager(config, httpClientParams);
SdkHttpClient httpClient = new SdkHttpClient(connectionManager, httpClientParams);
if(config.getMaxErrorRetry() > 0)
httpClient.setHttpRequestRetryHandler(SdkHttpRequestRetryHandler.Singleton);
// httpClient.setRedirectStrategy(new LocationHeaderNotRequiredRedirectStrategy());
try {
Scheme http = new Scheme("http", PlainSocketFactory.getSocketFactory(), 80);
SSLSocketFactory sf = new SSLSocketFactory(SSLContext.getDefault(),
SSLSocketFactory.STRICT_HOSTNAME_VERIFIER);
Scheme https = new Scheme("https", sf, 443);
SchemeRegistry sr = connectionManager.getSchemeRegistry();
sr.register(http);
sr.register(https);
} catch (NoSuchAlgorithmException e) {
throw new SCSClientException("Unable to access default SSL context", e);
}
// /*
// * If SSL cert checking for endpoints has been explicitly disabled,
// * register a new scheme for HTTPS that won't cause self-signed certs to
// * error out.
// */
// if (System.getProperty(DISABLE_CERT_CHECKING_SYSTEM_PROPERTY) != null) {
Scheme sch = new Scheme("https", 443, new TrustingSocketFactory());
httpClient.getConnectionManager().getSchemeRegistry().register(sch);
// }
/* Set proxy if configured */
String proxyHost = config.getProxyHost();
int proxyPort = config.getProxyPort();
if (proxyHost != null && proxyPort > 0) {
// AmazonHttpClient.log.info("Configuring Proxy. Proxy Host: " + proxyHost + " " + "Proxy Port: " + proxyPort);
HttpHost proxyHttpHost = new HttpHost(proxyHost, proxyPort);
httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxyHttpHost);
String proxyUsername = config.getProxyUsername();
String proxyPassword = config.getProxyPassword();
String proxyDomain = config.getProxyDomain();
String proxyWorkstation = config.getProxyWorkstation();
if (proxyUsername != null && proxyPassword != null) {
httpClient.getCredentialsProvider().setCredentials(
new AuthScope(proxyHost, proxyPort),
new NTCredentials(proxyUsername, proxyPassword, proxyWorkstation, proxyDomain));
}
}
return httpClient;
}
示例10: freeze
import org.apache.http.params.HttpConnectionParams; //导入方法依赖的package包/类
/**
* Initialize the connection.
*/
synchronized void freeze() {
if (frozen) {
return;
}
if (userAgent == null) {
setDefaultUserAgent();
}
serviceMap = new HashMap<String, ServiceEndpoint>();
if (log.isDebugEnabled()) {
StringBuilder buf = new StringBuilder();
buf.append("Creating MwsConnection {");
buf.append("applicationName:");
buf.append(applicationName);
buf.append(",applicationVersion:");
buf.append(applicationVersion);
buf.append(",awsAccessKeyId:");
buf.append(awsAccessKeyId);
buf.append(",uri:");
buf.append(endpoint.toString());
buf.append(",userAgent:");
buf.append(userAgent);
buf.append(",connectionTimeout:");
buf.append(connectionTimeout);
if (proxyHost != null && proxyPort != 0) {
buf.append(",proxyUsername:");
buf.append(proxyUsername);
buf.append(",proxyHost:");
buf.append(proxyHost);
buf.append(",proxyPort:");
buf.append(proxyPort);
}
buf.append("}");
log.debug(buf);
}
BasicHttpParams httpParams = new BasicHttpParams();
httpParams.setParameter(CoreProtocolPNames.USER_AGENT, userAgent);
HttpConnectionParams
.setConnectionTimeout(httpParams, connectionTimeout);
HttpConnectionParams.setSoTimeout(httpParams, socketTimeout);
HttpConnectionParams.setStaleCheckingEnabled(httpParams, true);
HttpConnectionParams.setTcpNoDelay(httpParams, true);
connectionManager = getConnectionManager();
httpClient = new DefaultHttpClient(connectionManager, httpParams);
httpContext = new BasicHttpContext();
if (proxyHost != null && proxyPort != 0) {
String scheme = MwsUtl.usesHttps(endpoint) ? "https" : "http";
HttpHost hostConfiguration = new HttpHost(proxyHost, proxyPort, scheme);
httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, hostConfiguration);
if (proxyUsername != null && proxyPassword != null) {
Credentials credentials = new UsernamePasswordCredentials(proxyUsername, proxyPassword);
CredentialsProvider cprovider = new BasicCredentialsProvider();
cprovider.setCredentials(new AuthScope(proxyHost, proxyPort), credentials);
httpContext.setAttribute(ClientContext.CREDS_PROVIDER, cprovider);
}
}
headers = Collections.unmodifiableMap(headers);
frozen = true;
}
示例11: setDefaultHttpParams
import org.apache.http.params.HttpConnectionParams; //导入方法依赖的package包/类
/**
* Saves the default set of HttpParams in the provided parameter.
* These are:
* <ul>
* <li>{@link org.apache.http.params.CoreProtocolPNames#PROTOCOL_VERSION}:
* 1.1</li>
* <li>{@link org.apache.http.params.CoreProtocolPNames#HTTP_CONTENT_CHARSET}:
* ISO-8859-1</li>
* <li>{@link org.apache.http.params.CoreConnectionPNames#TCP_NODELAY}:
* true</li>
* <li>{@link org.apache.http.params.CoreConnectionPNames#SOCKET_BUFFER_SIZE}:
* 8192</li>
* <li>{@link org.apache.http.params.CoreProtocolPNames#USER_AGENT}:
* Apache-HttpClient (Java 1.5)</li>
* </ul>
*/
public static void setDefaultHttpParams(final HttpParams params) {
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, HTTP.DEF_CONTENT_CHARSET.name());
HttpConnectionParams.setTcpNoDelay(params, true);
HttpConnectionParams.setSocketBufferSize(params, 8192);
HttpProtocolParams.setUserAgent(params, VersionInfo.getUserAgent("Apache-HttpClient",
"org.apache.http.client", DefaultHttpClient.class));
}