本文整理匯總了Java中com.ning.http.client.AsyncHttpClient.executeRequest方法的典型用法代碼示例。如果您正苦於以下問題:Java AsyncHttpClient.executeRequest方法的具體用法?Java AsyncHttpClient.executeRequest怎麽用?Java AsyncHttpClient.executeRequest使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.ning.http.client.AsyncHttpClient
的用法示例。
在下文中一共展示了AsyncHttpClient.executeRequest方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: performRequestAsync
import com.ning.http.client.AsyncHttpClient; //導入方法依賴的package包/類
protected <T extends Result> Future<T> performRequestAsync(AsyncHttpClient client, Request request, final Class<T> clazz) {
return client.executeRequest(request, new AsyncCompletionHandler<T>() {
@Override
public T onCompleted(Response response) throws Exception {
LOGGER.debug("Rate limit remaining: {}" + response.getHeader(RATELIMIT_REMAINING_HEADER));
T result = gson.fromJson(response.getContentType(), clazz);
validateResult(result);
return result;
}
});
}
示例2: performRequestAsync
import com.ning.http.client.AsyncHttpClient; //導入方法依賴的package包/類
@Override
protected <T extends Result> Future<T> performRequestAsync(final AsyncHttpClient client, final Request request, final Class<T> clazz) {
final String md5 = getMD5ForRequest(request);
final File file = new File("src/test/resources/json/" + md5 + ".json");
if (file.exists() && (file.lastModified() > new Date().getTime() - CACHE_TIME)) {
LOGGER.info("Found cached JSON file " + md5 + ".json, returning contents...");
ExecutorService executorService = Executors.newSingleThreadExecutor();
Callable<T> task = new Callable<T>() {
public T call() throws Exception {
return getResultFromJson(new Scanner(new FileInputStream(file)).useDelimiter("\\A").next(), clazz);
}
};
return executorService.submit(task);
}
return client.executeRequest(request, new AsyncCompletionHandler<T>() {
@Override
public T onCompleted(Response response) throws Exception {
LOGGER.info("Writing new JSON cache file " + md5 + ".json...");
String json = response.getResponseBody();
PrintWriter writer = new PrintWriter(file, "UTF-8");
writer.write(json);
writer.close();
return getResultFromJson(json, clazz);
}
});
}
示例3: HTTPAsyncPost
import com.ning.http.client.AsyncHttpClient; //導入方法依賴的package包/類
public HTTPAsyncPost(AsyncHttpClient client, HTTPLogObject http_log) {
RequestBuilder builder = new RequestBuilder("POST");
url_request = http_log.getURLRequest();
HTTPUtils util = new HTTPUtils();
builder = util.buildDefaultHeaders(builder, http_log);
// Add additional POST headers, if specified
builder = util.buildCustomPOSTHeaders(builder, http_log);
builder.addHeader("User-Agent", http_log.getUseragent());
String one_time_payload = POSTPayloadCache.instance.fetchPayload(http_log.getRequest());
logger.debug("Here is the randomized payload: " + one_time_payload);
builder.setBody(one_time_payload);
Request request = builder.setUrl(http_log.getURLRequest()).build();
AsyncHandler<Response> asyncHandler = new AsyncHandler<Response>() {
private final Response.ResponseBuilder builder = new Response.ResponseBuilder();
public STATE onBodyPartReceived(final HttpResponseBodyPart content)
throws Exception {
builder.accumulate(content);
return STATE.CONTINUE;
}
public STATE onStatusReceived(final HttpResponseStatus status)
throws Exception {
builder.accumulate(status);
return STATE.CONTINUE;
}
public STATE onHeadersReceived(final HttpResponseHeaders headers)
throws Exception {
builder.accumulate(headers);
return STATE.CONTINUE;
}
public Response onCompleted() throws Exception {
logger.debug("POST - " + url_request + " - "
+ builder.build().getStatusCode());
return builder.build();
}
@Override
public void onThrowable(Throwable t) {
logger.error(t.getMessage());
}
};
try {
client.executeRequest(request, asyncHandler);
} catch (IOException e) {
logger.error(e.getMessage());
}
}
示例4: HTTPAsyncGet
import com.ning.http.client.AsyncHttpClient; //導入方法依賴的package包/類
public HTTPAsyncGet(AsyncHttpClient client, HTTPLogObject http_log) {
RequestBuilder builder = new RequestBuilder("GET");
url_request = http_log.getURLRequest();
HTTPUtils util = new HTTPUtils();
builder = util.buildDefaultHeaders(builder, http_log);
// Add additional GET headers, if specified
builder = util.buildCustomGETHeaders(builder, http_log);
builder.addHeader("User-Agent", http_log.getUseragent());
Request request = builder.setUrl(url_request).build();
AsyncHandler<Response> asyncHandler = new AsyncHandler<Response>() {
private final Response.ResponseBuilder builder = new Response.ResponseBuilder();
public STATE onBodyPartReceived(final HttpResponseBodyPart content)
throws Exception {
builder.accumulate(content);
return STATE.CONTINUE;
}
public STATE onStatusReceived(final HttpResponseStatus status)
throws Exception {
builder.accumulate(status);
return STATE.CONTINUE;
}
public STATE onHeadersReceived(final HttpResponseHeaders headers)
throws Exception {
builder.accumulate(headers);
return STATE.CONTINUE;
}
public Response onCompleted() throws Exception {
logger.debug("GET - " + url_request + " - "
+ builder.build().getStatusCode());
return builder.build();
}
@Override
public void onThrowable(Throwable t) {
logger.error(t.getMessage());
}
};
try {
client.executeRequest(request, asyncHandler);
} catch (IOException e) {
logger.error(e.getMessage());
}
}