本文整理汇总了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());
}
}