下面列出了怎么用org.apache.http.impl.conn.SchemeRegistryFactory的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* create a proxy client
*
* @return either a client or null if none is configured
* @throws KeyManagementException
* @throws NumberFormatException
* if that port could not be parsed.
* @throws NoSuchAlgorithmException
*/
private static HttpClient createProxyClient(PlayProfile profile)
throws KeyManagementException, NoSuchAlgorithmException {
if (profile.getProxyAddress() == null) {
return null;
}
PoolingClientConnectionManager connManager = new PoolingClientConnectionManager(
SchemeRegistryFactory.createDefault());
connManager.setMaxTotal(100);
connManager.setDefaultMaxPerRoute(30);
DefaultHttpClient client = new DefaultHttpClient(connManager);
client.getConnectionManager().getSchemeRegistry()
.register(Utils.getMockedScheme());
HttpHost proxy = new HttpHost(profile.getProxyAddress(),
profile.getProxyPort());
client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
if (profile.getProxyUser() != null && profile.getProxyPassword() != null) {
client.getCredentialsProvider().setCredentials(
new AuthScope(proxy),
new UsernamePasswordCredentials(profile.getProxyUser(), profile
.getProxyPassword()));
}
return client;
}
public ClientBuilder() {
enableGZip = true;
name = "hosebird-client-" + clientNum.getAndIncrement();
ThreadFactory threadFactory = new ThreadFactoryBuilder()
.setDaemon(true)
.setNameFormat("hosebird-client-io-thread-%d")
.build();
executorService = Executors.newSingleThreadExecutor(threadFactory);
ThreadFactory rateTrackerThreadFactory = new ThreadFactoryBuilder()
.setDaemon(true)
.setNameFormat("hosebird-client-rateTracker-thread-%d")
.build();
ScheduledExecutorService scheduledExecutor = Executors.newScheduledThreadPool(1, rateTrackerThreadFactory);
rateTracker = new BasicRateTracker(30000, 100, true, scheduledExecutor);
reconnectionManager = new BasicReconnectionManager(5);
socketTimeoutMillis = 60000;
connectionTimeoutMillis = 4000;
schemeRegistry = SchemeRegistryFactory.createDefault();
}
/**
* Connection manager to allow concurrent connections.
*
* @return {@link ClientConnectionManager} instance
*/
public static ClientConnectionManager getConnectionManager() {
PoolingClientConnectionManager connManager = new PoolingClientConnectionManager(
SchemeRegistryFactory.createDefault());
connManager.setMaxTotal(100);
connManager.setDefaultMaxPerRoute(30);
return connManager;
}
/**
* Connection manager to allow concurrent connections.
*
* @return {@link ClientConnectionManager} instance
*/
public static ClientConnectionManager getConnectionManager() {
PoolingClientConnectionManager connManager = new PoolingClientConnectionManager(
SchemeRegistryFactory.createDefault());
connManager.setMaxTotal(100);
connManager.setDefaultMaxPerRoute(30);
return connManager;
}
/**
* Get a proxy client, if it is configured.
*
* @return either a client or null
* @throws IOException
* if reading the config file fails
* @throws KeyManagementException
* @throws NumberFormatException
* if that port could not be parsed.
* @throws NoSuchAlgorithmException
*/
public HttpClient getProxyClient() throws IOException, KeyManagementException,
NoSuchAlgorithmException, NumberFormatException {
File cfgfile = new File(root, NETCFG);
if (cfgfile.exists()) {
Properties cfg = new Properties();
cfg.load(new FileInputStream(cfgfile));
String ph = cfg.getProperty(PROXYHOST, null);
String pp = cfg.getProperty(PROXYPORT, null);
String pu = cfg.getProperty(PROXYUSER, null);
String pw = cfg.getProperty(PROXYPASS, null);
if (ph == null || pp == null) {
return null;
}
PoolingClientConnectionManager connManager = new PoolingClientConnectionManager(
SchemeRegistryFactory.createDefault());
connManager.setMaxTotal(100);
connManager.setDefaultMaxPerRoute(30);
DefaultHttpClient client = new DefaultHttpClient(connManager);
client.getConnectionManager().getSchemeRegistry().register(Utils.getMockedScheme());
HttpHost proxy = new HttpHost(ph, Integer.parseInt(pp));
client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
if (pu != null && pw != null) {
client.getCredentialsProvider().setCredentials(new AuthScope(proxy),
new UsernamePasswordCredentials(pu, pw));
}
return client;
}
return null;
}
/**
* Connection manager to allow concurrent connections.
*
* @return {@link ClientConnectionManager} instance
*/
public static ClientConnectionManager getConnectionManager() {
PoolingClientConnectionManager connManager = new PoolingClientConnectionManager(
SchemeRegistryFactory.createDefault());
connManager.setMaxTotal(100);
connManager.setDefaultMaxPerRoute(30);
return connManager;
}
@Before
public void setup() throws Exception {
mockAuth = mock(Authentication.class);
mockParams = mock(HttpParams.class);
defaultSchemeRegistry = SchemeRegistryFactory.createDefault();
request = new HttpGet("http://hi");
}