本文整理匯總了Java中org.apache.http.impl.nio.client.CloseableHttpAsyncClient.execute方法的典型用法代碼示例。如果您正苦於以下問題:Java CloseableHttpAsyncClient.execute方法的具體用法?Java CloseableHttpAsyncClient.execute怎麽用?Java CloseableHttpAsyncClient.execute使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.http.impl.nio.client.CloseableHttpAsyncClient
的用法示例。
在下文中一共展示了CloseableHttpAsyncClient.execute方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: send
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient; //導入方法依賴的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.CloseableHttpAsyncClient; //導入方法依賴的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.CloseableHttpAsyncClient; //導入方法依賴的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: main
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient; //導入方法依賴的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();
}
}
示例5: main
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient; //導入方法依賴的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();
}
}
示例6: get
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient; //導入方法依賴的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;
}
示例7: post
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient; //導入方法依賴的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;
}
示例8: main
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient; //導入方法依賴的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
示例9: main
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient; //導入方法依賴的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
示例10: main
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient; //導入方法依賴的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();
}
}
開發者ID:AndroidStudioTranslate,項目名稱:Android-Studio-Translate-Tool,代碼行數:21,代碼來源:AsyncClientAuthentication.java
示例11: main
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient; //導入方法依賴的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();
}
}
開發者ID:AndroidStudioTranslate,項目名稱:Android-Studio-Translate-Tool,代碼行數:25,代碼來源:AsyncClientProxyAuthentication.java
示例12: transactionMarker
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient; //導入方法依賴的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);
}
}
示例13: getCacheLoader
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient; //導入方法依賴的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;
}
};
}
示例14: testHttpAsyncClient
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient; //導入方法依賴的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();
}
示例15: main
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient; //導入方法依賴的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();
}
}