本文整理汇总了Java中org.apache.http.impl.nio.client.HttpAsyncClients.createDefault方法的典型用法代码示例。如果您正苦于以下问题:Java HttpAsyncClients.createDefault方法的具体用法?Java HttpAsyncClients.createDefault怎么用?Java HttpAsyncClients.createDefault使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.http.impl.nio.client.HttpAsyncClients
的用法示例。
在下文中一共展示了HttpAsyncClients.createDefault方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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);
}
示例2: 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();
}
}
示例3: 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");
}
示例4: 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;
}
示例5: 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;
}
示例6: 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();
}
}
开发者ID:AndroidStudioTranslate,项目名称:Android-Studio-Translate-Tool,代码行数:19,代码来源:AsyncClientExecuteProxy.java
示例7: 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");
}
开发者ID:AndroidStudioTranslate,项目名称:Android-Studio-Translate-Tool,代码行数:20,代码来源:AsyncClientHttpExchangeStreaming.java
示例8: transactionMarker
import org.apache.http.impl.nio.client.HttpAsyncClients; //导入方法依赖的package包/类
@Override
public void transactionMarker() throws Exception {
CloseableHttpAsyncClient httpClient = HttpAsyncClients.createDefault();
httpClient.start();
HttpHost httpHost = new HttpHost("localhost", getPort());
HttpGet httpGet = new HttpGet("/hello2");
SimpleFutureCallback callback = new SimpleFutureCallback();
Future<HttpResponse> future = httpClient.execute(httpHost, httpGet, callback);
callback.latch.await();
httpClient.close();
int responseStatusCode = future.get().getStatusLine().getStatusCode();
if (responseStatusCode != 200) {
throw new IllegalStateException(
"Unexpected response status code: " + responseStatusCode);
}
}
示例9: getCacheLoader
import org.apache.http.impl.nio.client.HttpAsyncClients; //导入方法依赖的package包/类
private static synchronized CacheLoader<String, String> getCacheLoader() {
return new CacheLoader<String, String>() {
@Override
public String load(String key) throws Exception {
log.trace("URI=[{}] 의 웹 컨텐츠를 비동기 방식으로 다운로드 받아 캐시합니다.", key);
String responseStr = "";
CloseableHttpAsyncClient httpClient = HttpAsyncClients.createDefault(); //new DefaultHttpAsyncClient();
try {
httpClient.start();
HttpGet request = new HttpGet(key);
Future<HttpResponse> future = httpClient.execute(request, null);
HttpResponse response = future.get();
responseStr = EntityUtils.toString(response.getEntity(), Charsets.UTF_8.toString());
if (log.isDebugEnabled())
log.debug("URI=[{}]로부터 웹 컨텐츠를 다운로드 받았습니다. responseStr=[{}]",
key, StringTool.ellipsisChar(responseStr, 80));
} finally {
httpClient.close();
}
return responseStr;
}
};
}
示例10: HttpPostDeliverService
import org.apache.http.impl.nio.client.HttpAsyncClients; //导入方法依赖的package包/类
public HttpPostDeliverService(final String postUrl, final int connectTimeout, final int soTimeout) {
httpClient = HttpAsyncClients.createDefault();
httpClient.start();
httpPost = new HttpPost(postUrl);
final RequestConfig requestConfig =
RequestConfig.custom().setConnectTimeout(connectTimeout).setSocketTimeout(soTimeout).build();
httpPost.setConfig(requestConfig);
httpPost.setHeader("Content-type", "application/json");
httpPost.setHeader("Content-Type", "text/html;charset=UTF-8");
}
示例11: InterruptibleHttpClient
import org.apache.http.impl.nio.client.HttpAsyncClients; //导入方法依赖的package包/类
/** An InterruptibleHttpClient using {@code HttpAsyncClients.createDefault()}
* as HttpAsyncClientProducer. */
public InterruptibleHttpClient ()
{
clientProducer = new HttpAsyncClientProducer ()
{
@Override
public CloseableHttpAsyncClient generateClient ()
{
CloseableHttpAsyncClient res = HttpAsyncClients.createDefault ();
res.start ();
return res;
}
};
}
示例12: testHttpAsyncClient
import org.apache.http.impl.nio.client.HttpAsyncClients; //导入方法依赖的package包/类
@Test
public void testHttpAsyncClient() throws InterruptedException, IOException {
CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
final CountDownLatch latch1 = new CountDownLatch(1);
final HttpGet request2 = new HttpGet("http://www.apache.org/");
httpclient.execute(request2, new FutureCallback<HttpResponse>() {
public void completed(final HttpResponse response2) {
latch1.countDown();
System.out.println(request2.getRequestLine() + "->" + response2.getStatusLine());
}
public void failed(final Exception ex) {
latch1.countDown();
System.out.println(request2.getRequestLine() + "->" + ex);
}
public void cancelled() {
latch1.countDown();
System.out.println(request2.getRequestLine() + " cancelled");
}
});
latch1.await();
}
示例13: main
import org.apache.http.impl.nio.client.HttpAsyncClients; //导入方法依赖的package包/类
public final static void main(String[] args) throws Exception {
CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
try {
// Create a local instance of cookie store
CookieStore cookieStore = new BasicCookieStore();
// Create local HTTP context
HttpClientContext localContext = HttpClientContext.create();
// Bind custom cookie store to the local context
localContext.setCookieStore(cookieStore);
HttpGet httpget = new HttpGet("http://localhost/");
System.out.println("Executing request " + httpget.getRequestLine());
httpclient.start();
// Pass local context as a parameter
Future<HttpResponse> future = httpclient.execute(httpget, localContext, null);
// Please note that it may be unsafe to access HttpContext instance
// while the request is still being executed
HttpResponse response = future.get();
System.out.println("Response: " + response.getStatusLine());
List<Cookie> cookies = cookieStore.getCookies();
for (int i = 0; i < cookies.size(); i++) {
System.out.println("Local cookie: " + cookies.get(i));
}
System.out.println("Shutting down");
} finally {
httpclient.close();
}
}
示例14: 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();
File upload = new File(args[0]);
File download = new File(args[1]);
ZeroCopyPost httpost = new ZeroCopyPost("http://localhost:8080/", upload, ContentType.create("text/plain"));
ZeroCopyConsumer<File> consumer = new ZeroCopyConsumer<File>(download) {
@Override
protected File process(final HttpResponse response, final File file, final ContentType contentType)
throws Exception {
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
throw new ClientProtocolException("Upload failed: " + response.getStatusLine());
}
return file;
}
};
Future<File> future = httpclient.execute(httpost, consumer, null);
File result = future.get();
System.out.println("Response file length: " + result.length());
System.out.println("Shutting down");
} finally {
httpclient.close();
}
System.out.println("Done");
}
示例15: 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();
HttpGet request = new HttpGet("http://www.apache.org/");
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();
}
System.out.println("Done");
}