package edu.cdut.robin;
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;
public class CustomerHttpClient
{
private static final String CHARSET = HTTP.UTF_8;
private static HttpClient customerHttpClient;
private CustomerHttpClient()
{
}
public static synchronized HttpClient getHttpClient()
{
if (null == customerHttpClient)
{
HttpParams params = new BasicHttpParams();
/* 設(shè)置一些基本參數(shù) */
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");
/* 超時(shí)設(shè)置 */
/* 從連接池中取連接的超時(shí)時(shí)間 */
ConnManagerParams.setTimeout(params, 1000);
/* 連接超時(shí) */
HttpConnectionParams.setConnectionTimeout(params, 2000);
/* 請(qǐng)求超時(shí) */
HttpConnectionParams.setSoTimeout(params, 4000);
/* 設(shè)置我們的HttpClient支持HTTP和HTTPS兩種模式 */
SchemeRegistry schReg = new SchemeRegistry();
schReg.register(new Scheme("http", PlainSocketFactory
.getSocketFactory(), 80));
schReg.register(new Scheme("https", SSLSocketFactory
.getSocketFactory(), 443));
/* 使用線程安全的連接管理來(lái)創(chuàng)建HttpClient */
ClientConnectionManager conMgr = new ThreadSafeClientConnManager(
params, schReg);
customerHttpClient = new DefaultHttpClient(conMgr, params);
}
return customerHttpClient;
}
}