本文整理汇总了Java中org.apache.http.params.HttpConnectionParams.setConnectionTimeout方法的典型用法代码示例。如果您正苦于以下问题:Java HttpConnectionParams.setConnectionTimeout方法的具体用法?Java HttpConnectionParams.setConnectionTimeout怎么用?Java HttpConnectionParams.setConnectionTimeout使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.http.params.HttpConnectionParams
的用法示例。
在下文中一共展示了HttpConnectionParams.setConnectionTimeout方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: 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;
}
示例3: getHttpClient
import org.apache.http.params.HttpConnectionParams; //导入方法依赖的package包/类
/**
* 从可用的HttpClient池中返回一个默认10秒的HttpClient对象,该方法是同步的。
*
* @return 可用的.HttpClient对象
*/
public static synchronized HttpClient getHttpClient() {
if (null == customerHttpClient) {
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, CHARSET);
HttpProtocolParams.setUseExpectContinue(params, true);
HttpProtocolParams.setUserAgent(params,
"Mozilla/5.0(Linux;U;Android 2.2.1;en-us;Nexus One Build.FRG83) "
+ "AppleWebKit/553.1(KHTML,like Gecko) Version/4.0 Mobile Safari/533.1");
ConnManagerParams.setTimeout(params, 10000);
HttpConnectionParams.setConnectionTimeout(params, 10000);
HttpConnectionParams.setSoTimeout(params, 10000);
SchemeRegistry schReg = new SchemeRegistry();
schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
schReg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schReg);
customerHttpClient = new DefaultHttpClient(conMgr, params);
}
return customerHttpClient;
}
示例4: createHttpClient
import org.apache.http.params.HttpConnectionParams; //导入方法依赖的package包/类
private DefaultHttpClient createHttpClient() {
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params,
HTTP.DEFAULT_CONTENT_CHARSET);
HttpProtocolParams.setUseExpectContinue(params, true);
HttpConnectionParams.setConnectionTimeout(params,
CONNETED_TIMEOUT * 1000);
HttpConnectionParams.setSoTimeout(params, CONNETED_TIMEOUT * 1000);
HttpConnectionParams.setSocketBufferSize(params, 8192);
ConnManagerParams.setMaxTotalConnections(params, 4);
SchemeRegistry schReg = new SchemeRegistry();
schReg.register(new Scheme("http", PlainSocketFactory
.getSocketFactory(), 80));
schReg.register(new Scheme("https",
SSLSocketFactory.getSocketFactory(), 443));
ClientConnectionManager connMgr = new ThreadSafeClientConnManager(
params, schReg);
return new DefaultHttpClient(connMgr, params);
}
示例5: create
import org.apache.http.params.HttpConnectionParams; //导入方法依赖的package包/类
/**
* HTTPClientオブジェクトを作成.
* @param type 通信タイプ
* @param connectionTimeout タイムアウト値(ミリ秒)。0の場合はデフォルト値を利用する。
* @return 作成したHttpClientクラスインスタンス
*/
public static HttpClient create(final String type, final int connectionTimeout) {
if (TYPE_DEFAULT.equalsIgnoreCase(type)) {
return new DefaultHttpClient();
}
SSLSocketFactory sf = null;
try {
if (TYPE_INSECURE.equalsIgnoreCase(type)) {
sf = createInsecureSSLSocketFactory();
}
} catch (Exception e) {
return null;
}
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("https", PORTHTTPS, sf));
schemeRegistry.register(new Scheme("http", PORTHTTP, PlainSocketFactory.getSocketFactory()));
HttpParams params = new BasicHttpParams();
ClientConnectionManager cm = new SingleClientConnManager(schemeRegistry);
// ClientConnectionManager cm = new
// ThreadSafeClientConnManager(schemeRegistry);
HttpClient hc = new DefaultHttpClient(cm, params);
HttpParams params2 = hc.getParams();
int timeout = TIMEOUT;
if (connectionTimeout != 0) {
timeout = connectionTimeout;
}
HttpConnectionParams.setConnectionTimeout(params2, timeout); // 接続のタイムアウト
HttpConnectionParams.setSoTimeout(params2, timeout); // データ取得のタイムアウト
return hc;
}
示例6: performRequest
import org.apache.http.params.HttpConnectionParams; //导入方法依赖的package包/类
@Override
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
throws IOException, AuthFailureError {
HttpUriRequest httpRequest = createHttpRequest(request, additionalHeaders);
addHeaders(httpRequest, additionalHeaders);
addHeaders(httpRequest, request.getHeaders());
onPrepareRequest(httpRequest);
HttpParams httpParams = httpRequest.getParams();
int timeoutMs = request.getTimeoutMs();
// TODO: Reevaluate this connection timeout based on more wide-scale
// data collection and possibly different for wifi vs. 3G.
HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
HttpConnectionParams.setSoTimeout(httpParams, timeoutMs);
return mClient.execute(httpRequest);
}
示例7: create
import org.apache.http.params.HttpConnectionParams; //导入方法依赖的package包/类
/**
* HTTPClientオブジェクトを作成.
* @param type 通信タイプ
* @return 作成したHttpClientクラスインスタンス
*/
public static HttpClient create(final String type) {
if (TYPE_DEFAULT.equalsIgnoreCase(type)) {
return new DefaultHttpClient();
}
SSLSocketFactory sf = null;
try {
if (TYPE_INSECURE.equalsIgnoreCase(type)) {
sf = createInsecureSSLSocketFactory();
}
} catch (Exception e) {
return null;
}
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("https", PORTHTTPS, sf));
schemeRegistry.register(new Scheme("http", PORTHTTP, PlainSocketFactory.getSocketFactory()));
HttpParams params = new BasicHttpParams();
ClientConnectionManager cm = new SingleClientConnManager(schemeRegistry);
// ClientConnectionManager cm = new
// ThreadSafeClientConnManager(schemeRegistry);
HttpClient hc = new DefaultHttpClient(cm, params);
HttpParams params2 = hc.getParams();
int timeout = TIMEOUT;
HttpConnectionParams.setConnectionTimeout(params2, timeout); // 接続のタイムアウト
HttpConnectionParams.setSoTimeout(params2, timeout); // データ取得のタイムアウト
return hc;
}
示例8: setTimeout
import org.apache.http.params.HttpConnectionParams; //导入方法依赖的package包/类
/**
* Set the connection and socket timeout. By default, 10 seconds.
*
* @param timeout the connect/socket timeout in milliseconds, at least 1 second
*/
public void setTimeout(int timeout) {
if (timeout < 1000) timeout = DEFAULT_SOCKET_TIMEOUT;
this.timeout = timeout;
final HttpParams httpParams = this.httpClient.getParams();
ConnManagerParams.setTimeout(httpParams, this.timeout);
HttpConnectionParams.setSoTimeout(httpParams, this.timeout);
HttpConnectionParams.setConnectionTimeout(httpParams, this.timeout);
}
示例9: setTimeout
import org.apache.http.params.HttpConnectionParams; //导入方法依赖的package包/类
/**
* Set the connection and socket timeout. By default, 10 seconds.
*
* @param timeout the connect/socket timeout in milliseconds, at least 1 second
*/
public void setTimeout(int timeout) {
if (timeout < 1000)
timeout = DEFAULT_SOCKET_TIMEOUT;
this.timeout = timeout;
final HttpParams httpParams = this.httpClient.getParams();
ConnManagerParams.setTimeout(httpParams, this.timeout);
HttpConnectionParams.setSoTimeout(httpParams, this.timeout);
HttpConnectionParams.setConnectionTimeout(httpParams, this.timeout);
}
示例10: cloneDefault
import org.apache.http.params.HttpConnectionParams; //导入方法依赖的package包/类
public static HttpParams cloneDefault() {
HttpParams p = new BasicHttpParams();
// Create and initialize HTTP parameters
HttpConnectionParams.setConnectionTimeout(p, 30*1000);//建立http连接,30秒超时
HttpConnectionParams.setSoTimeout(p, 2*60*1000);//建立http连接后,数据读取间隔,2分钟超时,下载时间超过2分钟停止
ConnManagerParams.setMaxTotalConnections(p, 100);//连接管理器最大100个连接
ConnManagerParams.setTimeout(p, 5*60*1000);//在向连接管理器要空闲连接时,线程阻塞5分钟超时
HttpProtocolParams.setVersion(p, HttpVersion.HTTP_1_1);
//Increase max connections for url:80 to 50
//connPerRoute.setMaxForRoute(new HttpRoute(new HttpHost(BBS_HOST, 80)), 5);//到bbs的路由的连接20最大20个
ConnManagerParams.setMaxConnectionsPerRoute(p, new ConnPerRouteBean(5));//每个路由默认分配的最大连接数是5个
return p;
}
示例11: getContent
import org.apache.http.params.HttpConnectionParams; //导入方法依赖的package包/类
/**
* 传入网络地址,并构造可以操作中断的HttpClientIO对象,返回字符串,此方法是同步的
*
* @param url
* @return 字符串结果
* @throws Exception
*/
public static String getContent(String url, HttpClientIO hcio) throws Exception {
StringBuilder sb = new StringBuilder();
HttpClient client = getHttpClient();
HttpParams httpParams = client.getParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 6000);
HttpConnectionParams.setSoTimeout(httpParams, 6000);
HttpResponse response = client.execute(new HttpGet(url));
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream is = entity.getContent();
// 回调
if (hcio != null) {
hcio.setInputStream(is);
hcio.setHttpEntity(entity);
}
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "UTF-8"), 1024);
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
reader.close();
}
return sb.toString();
}
示例12: 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;
}
示例13: initClient
import org.apache.http.params.HttpConnectionParams; //导入方法依赖的package包/类
private void initClient (String url) {
// initialize API URL
this.apiUrl = C.api.base + url;
String apiSid = "wang123";
if (apiSid != null && apiSid.length() > 0) {
this.apiUrl += "?sid=" + apiSid;
}
// set client timeout
httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, timeoutConnection);
HttpConnectionParams.setSoTimeout(httpParams, timeoutSocket);
// init client
httpClient = new DefaultHttpClient(httpParams);
}
示例14: addUserOnServer
import org.apache.http.params.HttpConnectionParams; //导入方法依赖的package包/类
private void addUserOnServer(final String disCode, final String phoneNum, final String password) {
//耗时操作,需要在子线程中完成
startRegUIDialog();
regThread = new Thread(new Runnable() {
@Override
public void run() {
Looper.prepare();
try {
BasicHttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
HttpConnectionParams.setSoTimeout(httpParams, 5000);
HttpClient httpclient = new DefaultHttpClient(httpParams);
//服务器地址,指向Servlet
HttpPost httpPost = new HttpPost(ServerUtil.SLRegister);
List<NameValuePair> params = new ArrayList<NameValuePair>();//将数据装入list
params.add(new BasicNameValuePair("phonenum", phoneNum));
params.add(new BasicNameValuePair("password", password));
params.add(new BasicNameValuePair("headicon",
Utility.convertBitmapToString(BitmapFactory.decodeResource(
getResources(), R.drawable.default_head_icon
))));
final UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "utf-8");//以UTF-8格式发送
httpPost.setEntity(entity);
//对提交数据进行编码
HttpResponse httpResponse = httpclient.execute(httpPost);
if(httpResponse.getStatusLine().getStatusCode()==200)//在5000毫秒之内接收到返回值
{
HttpEntity entity1 = httpResponse.getEntity();
response = EntityUtils.toString(entity1, "utf-8");//以UTF-8格式解析
Message message = handler.obtainMessage();
message.what = 0;
message.obj = response;
handler.handleMessage(message);
} else {
Message message = handler.obtainMessage();
message.what = 1;
handler.handleMessage(message);
}
} catch (Exception e) {
e.printStackTrace();
Message message = handler.obtainMessage();
message.what = 1;
handler.handleMessage(message);
}
Looper.loop();
}
});
regThread.start();
}
示例15: fetchFile
import org.apache.http.params.HttpConnectionParams; //导入方法依赖的package包/类
/**
* Fetches a file from the URI given and returns an input stream to it.
*
* @param uri the uri of the file to fetch
* @param config optional configuration overrides
* @return an InputStream containing the retrieved data
* @throws IOException on error
*/
@SuppressWarnings("deprecation")
public static InputStream fetchFile(
final URI uri,
final ClientConfiguration config) throws IOException {
HttpParams httpClientParams = new BasicHttpParams();
HttpProtocolParams.setUserAgent(
httpClientParams, getUserAgent(config, null));
HttpConnectionParams.setConnectionTimeout(
httpClientParams, getConnectionTimeout(config));
HttpConnectionParams.setSoTimeout(
httpClientParams, getSocketTimeout(config));
DefaultHttpClient httpclient = new DefaultHttpClient(httpClientParams);
if (config != null) {
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);
if (config.getProxyUsername() != null
&& config.getProxyPassword() != null) {
httpclient.getCredentialsProvider().setCredentials(
new AuthScope(proxyHost, proxyPort),
new NTCredentials(config.getProxyUsername(),
config.getProxyPassword(),
config.getProxyWorkstation(),
config.getProxyDomain()));
}
}
}
HttpResponse response = httpclient.execute(new HttpGet(uri));
if (response.getStatusLine().getStatusCode() != 200) {
throw new IOException("Error fetching file from " + uri + ": "
+ response);
}
return new HttpClientWrappingInputStream(
httpclient,
response.getEntity().getContent());
}