本文整理汇总了Java中org.apache.http.impl.nio.client.HttpAsyncClients类的典型用法代码示例。如果您正苦于以下问题:Java HttpAsyncClients类的具体用法?Java HttpAsyncClients怎么用?Java HttpAsyncClients使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
HttpAsyncClients类属于org.apache.http.impl.nio.client包,在下文中一共展示了HttpAsyncClients类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createHttpAsyncClient
import org.apache.http.impl.nio.client.HttpAsyncClients; //导入依赖的package包/类
private CloseableHttpAsyncClient createHttpAsyncClient(YunpianConf conf) throws IOReactorException {
IOReactorConfig ioReactorConfig = IOReactorConfig.custom().setIoThreadCount(Runtime.getRuntime().availableProcessors())
.setConnectTimeout(conf.getConfInt(YunpianConf.HTTP_CONN_TIMEOUT, "10000"))
.setSoTimeout(conf.getConfInt(YunpianConf.HTTP_SO_TIMEOUT, "30000")).build();
ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor(ioReactorConfig);
PoolingNHttpClientConnectionManager connManager = new PoolingNHttpClientConnectionManager(ioReactor);
ConnectionConfig connectionConfig = ConnectionConfig.custom().setMalformedInputAction(CodingErrorAction.IGNORE)
.setUnmappableInputAction(CodingErrorAction.IGNORE)
.setCharset(Charset.forName(conf.getConf(YunpianConf.HTTP_CHARSET, YunpianConf.HTTP_CHARSET_DEFAULT))).build();
connManager.setDefaultConnectionConfig(connectionConfig);
connManager.setMaxTotal(conf.getConfInt(YunpianConf.HTTP_CONN_MAXTOTAL, "100"));
connManager.setDefaultMaxPerRoute(conf.getConfInt(YunpianConf.HTTP_CONN_MAXPERROUTE, "10"));
CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom().setConnectionManager(connManager).build();
httpclient.start();
return httpclient;
}
示例2: send
import org.apache.http.impl.nio.client.HttpAsyncClients; //导入依赖的package包/类
public void send(List<Map<Object, Object>> events, AsyncSuccessCallback<ProducedEventsResult> onSuccess, AsyncFailCallback onFail, AsyncCancelledCallback onCancel) throws IOException, InterruptedException {
CloseableHttpAsyncClient httpClient = HttpAsyncClients.createDefault();
httpClient.start();
String url = String.format("%s/%s/bulk-produce", this.endpoint, this.topicId);
System.out.println(url);
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("Authorization", this.writeApiKey);
httpPost.addHeader("Content-type", this.format);
String jsonString = MAPPER.writeValueAsString(events);
HttpEntity entity = new ByteArrayEntity(jsonString.getBytes());
httpPost.setEntity(entity);
ResponseParser<ProducedEventsResult> parser = new BulkProduceEventsParser();
AsyncCallback cb = new AsyncCallback(httpClient, parser, MAPPER, onSuccess, onFail, onCancel);
httpClient.execute(httpPost, cb);
}
示例3: generateClient
import org.apache.http.impl.nio.client.HttpAsyncClients; //导入依赖的package包/类
@Override
public CloseableHttpAsyncClient generateClient ()
{
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope (AuthScope.ANY),
new UsernamePasswordCredentials(serviceUser, servicePass));
RequestConfig rqconf = RequestConfig.custom()
.setCookieSpec(CookieSpecs.DEFAULT)
.setSocketTimeout(Timeouts.SOCKET_TIMEOUT)
.setConnectTimeout(Timeouts.CONNECTION_TIMEOUT)
.setConnectionRequestTimeout(Timeouts.CONNECTION_REQUEST_TIMEOUT)
.build();
CloseableHttpAsyncClient res = HttpAsyncClients.custom ()
.setDefaultCredentialsProvider (credsProvider)
.setDefaultRequestConfig(rqconf)
.build ();
res.start ();
return res;
}
示例4: generateClient
import org.apache.http.impl.nio.client.HttpAsyncClients; //导入依赖的package包/类
@Override
public CloseableHttpAsyncClient generateClient ()
{
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope (AuthScope.ANY),
new UsernamePasswordCredentials(username, password));
RequestConfig rqconf = RequestConfig.custom()
.setCookieSpec(CookieSpecs.DEFAULT)
.setSocketTimeout(Timeouts.SOCKET_TIMEOUT)
.setConnectTimeout(Timeouts.CONNECTION_TIMEOUT)
.setConnectionRequestTimeout(Timeouts.CONNECTION_REQUEST_TIMEOUT)
.build();
CloseableHttpAsyncClient res = HttpAsyncClients.custom ()
.setDefaultCredentialsProvider (credsProvider)
.setDefaultRequestConfig(rqconf)
.build ();
res.start ();
return res;
}
示例5: main
import org.apache.http.impl.nio.client.HttpAsyncClients; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
try {
httpclient.start();
HttpHost proxy = new HttpHost("someproxy", 8080);
RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
HttpGet request = new HttpGet("https://issues.apache.org/");
request.setConfig(config);
Future<HttpResponse> future = httpclient.execute(request, null);
HttpResponse response = future.get();
System.out.println("Response: " + response.getStatusLine());
System.out.println("Shutting down");
} finally {
httpclient.close();
}
}
示例6: main
import org.apache.http.impl.nio.client.HttpAsyncClients; //导入依赖的package包/类
public static void main(final String[] args) throws Exception {
CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
try {
httpclient.start();
Future<Boolean> future = httpclient.execute(HttpAsyncMethods.createGet("http://localhost:8080/"),
new MyResponseConsumer(), null);
Boolean result = future.get();
if (result != null && result.booleanValue()) {
System.out.println("Request successfully executed");
} else {
System.out.println("Request failed");
}
System.out.println("Shutting down");
} finally {
httpclient.close();
}
System.out.println("Done");
}
示例7: main
import org.apache.http.impl.nio.client.HttpAsyncClients; //导入依赖的package包/类
public static void main(final String[] args) throws Exception {
CloseableHttpPipeliningClient httpclient = HttpAsyncClients.createPipelining();
try {
httpclient.start();
HttpHost targetHost = new HttpHost("localhost", 8080);
HttpGet[] resquests = { new HttpGet("/docs/index.html"), new HttpGet("/docs/introduction.html"),
new HttpGet("/docs/setup.html"), new HttpGet("/docs/config/index.html") };
Future<List<HttpResponse>> future = httpclient.execute(targetHost, Arrays.<HttpRequest>asList(resquests),
null);
List<HttpResponse> responses = future.get();
System.out.println(responses);
System.out.println("Shutting down");
} finally {
httpclient.close();
}
System.out.println("Done");
}
示例8: main
import org.apache.http.impl.nio.client.HttpAsyncClients; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope("localhost", 443),
new UsernamePasswordCredentials("username", "password"));
CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom().setDefaultCredentialsProvider(credsProvider)
.build();
try {
HttpGet httpget = new HttpGet("http://localhost/");
System.out.println("Executing request " + httpget.getRequestLine());
Future<HttpResponse> future = httpclient.execute(httpget, null);
HttpResponse response = future.get();
System.out.println("Response: " + response.getStatusLine());
System.out.println("Shutting down");
} finally {
httpclient.close();
}
}
示例9: main
import org.apache.http.impl.nio.client.HttpAsyncClients; //导入依赖的package包/类
public static void main(final String[] args) throws Exception {
CloseableHttpPipeliningClient httpclient = HttpAsyncClients.createPipelining();
try {
httpclient.start();
HttpHost targetHost = new HttpHost("localhost", 8080);
HttpGet[] resquests = { new HttpGet("/docs/index.html"), new HttpGet("/docs/introduction.html"),
new HttpGet("/docs/setup.html"), new HttpGet("/docs/config/index.html") };
List<MyRequestProducer> requestProducers = new ArrayList<MyRequestProducer>();
List<MyResponseConsumer> responseConsumers = new ArrayList<MyResponseConsumer>();
for (HttpGet request : resquests) {
requestProducers.add(new MyRequestProducer(targetHost, request));
responseConsumers.add(new MyResponseConsumer(request));
}
Future<List<Boolean>> future = httpclient.execute(targetHost, requestProducers, responseConsumers, null);
future.get();
System.out.println("Shutting down");
} finally {
httpclient.close();
}
System.out.println("Done");
}
示例10: main
import org.apache.http.impl.nio.client.HttpAsyncClients; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope("someproxy", 8080),
new UsernamePasswordCredentials("username", "password"));
CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom().setDefaultCredentialsProvider(credsProvider)
.build();
try {
httpclient.start();
HttpHost proxy = new HttpHost("someproxy", 8080);
RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
HttpGet httpget = new HttpGet("https://issues.apache.org/");
httpget.setConfig(config);
Future<HttpResponse> future = httpclient.execute(httpget, null);
HttpResponse response = future.get();
System.out.println("Response: " + response.getStatusLine());
System.out.println("Shutting down");
} finally {
httpclient.close();
}
}
示例11: open
import org.apache.http.impl.nio.client.HttpAsyncClients; //导入依赖的package包/类
private void open() {
if (open) {
// Ignore
return;
}
final HttpAsyncClientBuilder builder = HttpAsyncClients.custom()
.setMaxConnPerRoute(1000)
.setMaxConnTotal(1000)
.setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy())
.setThreadFactory(threadFactory);
if (credentialsProvider != null) {
builder.setDefaultCredentialsProvider(credentialsProvider);
}
httpclient = builder.build();
httpclient.start();
this.open = true;
}
示例12: get
import org.apache.http.impl.nio.client.HttpAsyncClients; //导入依赖的package包/类
public final static HttpResponse get(String url, List<NameValuePair> parameters) throws Throwable {
CloseableHttpAsyncClient client = HttpAsyncClients.createDefault();
StringBuffer sb = new StringBuffer("?");
for (NameValuePair pair : parameters) {
sb.append(pair.getName());
sb.append("=");
sb.append(URLEncoder.encode(pair.getValue(), "UTF-8"));
sb.append("&");
}
client.start();
final HttpGet httpGet = new HttpGet(url + sb.toString());
httpGet.removeHeaders("X-FORWARDED-FOR");
httpGet.setHeader("X-FORWARDED-FOR", Environment.LOCAL_IP_ADDR);
logger.debug("-> GET " + (url + sb.toString()));
Future<HttpResponse> future = client.execute(httpGet, null);
HttpResponse resp = future.get();
return resp;
}
示例13: post
import org.apache.http.impl.nio.client.HttpAsyncClients; //导入依赖的package包/类
public final static HttpResponse post(String url, List<NameValuePair> parameters) throws Throwable {
CloseableHttpAsyncClient client = HttpAsyncClients.createDefault();
client.start();
final HttpPost httpPost = new HttpPost(url);
httpPost.removeHeaders("X-FORWARDED-FOR");
httpPost.setHeader("X-FORWARDED-FOR", Environment.LOCAL_IP_ADDR);
UrlEncodedFormEntity encodedFormEntity = new UrlEncodedFormEntity(parameters, "UTF-8");
httpPost.setEntity(encodedFormEntity);
logger.debug("-> POST " + url + " Parameters " + JsonUtil.toString(parameters));
Future<HttpResponse> future = client.execute(httpPost, null);
HttpResponse resp = future.get();
return resp;
}
示例14: getHttpClient
import org.apache.http.impl.nio.client.HttpAsyncClients; //导入依赖的package包/类
/**
* Initializes and returns the httpClient with NoopHostnameVerifier
*
* @return CloseableHttpAsyncClient
*/
@Override
public CloseableHttpAsyncClient getHttpClient() {
// Trust own CA and all self-signed certs
SSLContext sslcontext = NonValidatingSSLSocketFactory.getSSLContext();
// Allow TLSv1 protocol only
SSLIOSessionStrategy sslSessionStrategy = new SSLIOSessionStrategy(sslcontext, new String[] { "TLSv1" }, null,
new NoopHostnameVerifier());
List<Header> headers = LogInsightClient.getDefaultHeaders();
asyncHttpClient = HttpAsyncClients.custom().setSSLStrategy(sslSessionStrategy).setDefaultHeaders(headers)
.build();
asyncHttpClient.start();
return asyncHttpClient;
}
示例15: newHttpAsyncClient
import org.apache.http.impl.nio.client.HttpAsyncClients; //导入依赖的package包/类
/**
* Creates an asynchronous HTTP client configuration with default timeouts.
*
* @see #newHttpAsyncClient(boolean)
*/
protected static CloseableHttpAsyncClient newHttpAsyncClient(boolean useSSL) {
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(DEFAULT_SOCKET_TIMEOUT)
.setConnectTimeout(DEFAULT_CONNECT_TIMEOUT).build();
HttpAsyncClientBuilder builder = HttpAsyncClients.custom();
if (useSSL) {
try {
SSLContext context = SSLContext.getInstance("SSL");
context.init(null, new TrustManager[]{new TrustAllX509Manager()}, new SecureRandom());
SSLIOSessionStrategy strategy = new SSLIOSessionStrategy(context,
SSLIOSessionStrategy.getDefaultHostnameVerifier());
builder.setSSLStrategy(strategy);
} catch (Exception e) {
log.error("Failed initializing SSL context! Skipped.", e);
}
}
return builder.setDefaultRequestConfig(requestConfig).build();
}