下面列出了怎么用org.apache.http.client.ResponseHandler的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* @return the RAW XML document of ItemLookup (Large Response) from Amazon
* product advertisement API
* @throws InvalidKeyException
* @throws NoSuchAlgorithmException
* @throws ClientProtocolException
* @throws IOException
*/
public String getXMLLargeResponse() throws InvalidKeyException,
NoSuchAlgorithmException, ClientProtocolException, IOException {
String responseBody = "";
String signedurl = signInput();
try {
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(signedurl);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
responseBody = httpclient.execute(httpget, responseHandler);
// responseBody now contains the contents of the page
// System.out.println(responseBody);
httpclient.getConnectionManager().shutdown();
} catch (Exception e) {
System.out.println("Exception" + " " + itemID + " " + e.getClass());
}
return responseBody;
}
public ApacheHttpRestClient() {
this.httpClient = HttpClients.createDefault();
//TODO lambda
this.responseHandler = new ResponseHandler<String>() {
@Override
public String handleResponse(
final HttpResponse response) throws IOException {
int status = response.getStatusLine().getStatusCode();
if (status >= 200 && status < 300) {
HttpEntity entity = response.getEntity();
return entity != null ? EntityUtils.toString(entity) : null;
}
else {
throw new ClientProtocolException("Unexpected response status: " + status);
}
}
};
}
private String getText(String redirectLocation) {
HttpGet httpget = new HttpGet(redirectLocation);
// Create a response handler
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = "";
try {
responseBody = httpclient.execute(httpget, responseHandler);
} catch (Exception e) {
e.printStackTrace();
responseBody = null;
} finally {
httpget.abort();
httpclient.getConnectionManager().shutdown();
}
return responseBody;
}
/**
*
* @param mch_id mch_id
* @param request request
* @param clazz clazz
* @param sign_type 数据返回验证签名类型
* @param key 数据返回验证签名key
* @since 2.8.5
* @return result
*/
public static <T> T keyStoreExecuteXmlResult(String mch_id,HttpUriRequest request,Class<T> clazz,String sign_type,String key){
String uriId = loggerRequest(request);
ResponseHandler<T> responseHandler = XmlResponseHandler.createResponseHandler(clazz,sign_type,key);
if(responseHandler instanceof LocalResponseHandler){
LocalResponseHandler lrh = (LocalResponseHandler) responseHandler;
lrh.setUriId(uriId);
}
try {
T t = httpClient_mchKeyStore.get(mch_id).execute(request,responseHandler,HttpClientContext.create());
if(resultErrorHandler != null){
resultErrorHandler.doHandle(uriId, request, t);
}
return t;
} catch (Exception e) {
e.printStackTrace();
logger.error(e.getMessage());
}
return null;
}
/**
* UnixHttpClient can execute the HttpUriRequest with the given
* response handler and context.
* @throws IOException If something goes wrong.
*/
@Test
public void executesUriRequestWithHandlerAndContext() throws IOException {
final HttpUriRequest req = Mockito.mock(HttpUriRequest.class);
final ResponseHandler<String> handler = Mockito.mock(
ResponseHandler.class
);
final HttpContext context = Mockito.mock(HttpContext.class);
final HttpClient decorated = Mockito.mock(HttpClient.class);
Mockito.when(
decorated.execute(req, handler, context)
).thenReturn("executed");
final HttpClient unix = new UnixHttpClient(() -> decorated);
MatcherAssert.assertThat(
unix.execute(req, handler, context), Matchers.equalTo("executed")
);
Mockito.verify(
decorated, Mockito.times(1)
).execute(req, handler, context);
}
/**
* UnixHttpClient can execute the HttpRequest with the given host,
* response handler and context.
* @throws IOException If something goes wrong.
*/
@Test
public void executesRequestWithHostHandlerAndContext() throws IOException {
final HttpHost host = new HttpHost("127.0.0.1");
final HttpRequest req = Mockito.mock(HttpRequest.class);
final ResponseHandler<String> handler = Mockito.mock(
ResponseHandler.class
);
final HttpContext context = Mockito.mock(HttpContext.class);
final HttpClient decorated = Mockito.mock(HttpClient.class);
Mockito.when(
decorated.execute(host, req, handler, context)
).thenReturn("executed");
final HttpClient unix = new UnixHttpClient(() -> decorated);
MatcherAssert.assertThat(
unix.execute(host, req, handler, context),
Matchers.equalTo("executed")
);
Mockito.verify(
decorated, Mockito.times(1)
).execute(host, req, handler, context);
}
private void sendBulk(final String requestBody, final String inventoryName) {
try {
HttpPost post = new HttpPost(bulkUri);
StringEntity body = new StringEntity(requestBody);
logger.trace(String.format("%s:\n%s", inventoryName, requestBody));
body.setChunked(false);
post.setEntity(body);
ResponseHandler<Void> rspHandler = new ResponseHandler<Void>() {
@Override
public Void handleResponse(HttpResponse rsp) throws ClientProtocolException, IOException {
if (rsp.getStatusLine().getStatusCode() != HttpStatus.SC_OK && rsp.getStatusLine().getStatusCode() != HttpStatus.SC_CREATED) {
logger.warn(String.format("Failed to do bulk operation on[%s] , because: \nstatus line: %s\nresponse body: %s\nrequest body: %s",
inventoryName, rsp.getStatusLine(), EntityUtils.toString(rsp.getEntity()), requestBody));
} else {
logger.trace(String.format("Successfully did bulk operation on[%s], %s", inventoryName, EntityUtils.toString(rsp.getEntity())));
}
return null;
}
};
httpClient.execute(post, rspHandler);
} catch (Exception e) {
logger.warn(String.format("Failed to do bulk operation on inventory[%s]", inventoryName), e);
}
}
public synchronized static final <T> Response<T> syncUpdate(Context ctx, Uri uri,
String username, String password,
Bundle form,
Bundle files,
ResponseHandler<Response<T>> handler) {
Log.i(TAG, "syncUpdate()");
Map<String, String> data = new HashMap<String, String>();
Cursor c = null;
Response<T> response = Response.empty();
// Should have at least one field that need to be updated
if (form != null) {
Iterator<String> keys = form.keySet().iterator();
while (keys.hasNext()) {
String key = keys.next();
data.put(key, form.getString(key));
Log.d(TAG, "key: " + key + " --> value: " + form.getString(key));
}
}
try {
URI target = iriToURI(ctx, uri);
response = apiPost(target, username, password, data, handler);
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
public static String[] getHosts() {
try {
DefaultHttpClient client = new DefaultHttpClient();
client.getCredentialsProvider().setCredentials(new AuthScope(host_name, getAdminPort()),
new UsernamePasswordCredentials("admin", "admin"));
HttpGet get = new HttpGet("http://" + host_name + ":" + admin_port + "/manage/v2/hosts?format=json");
HttpResponse response = client.execute(get);
ResponseHandler<String> handler = new BasicResponseHandler();
String body = handler.handleResponse(response);
JsonNode actualObj = new ObjectMapper().readTree(body);
JsonNode nameNode = actualObj.path("host-default-list").path("list-items");
List<String> hosts = nameNode.findValuesAsText("nameref");
String[] s = new String[hosts.size()];
hosts.toArray(s);
return s;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private String executeGetRequest(String url) throws IOException {
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(15000)
.setSocketTimeout(15000)
.build();
HttpGet method = new HttpGet(url);
method.setConfig(requestConfig);
try (CloseableHttpClient client = HttpClients.createDefault()) {
ResponseHandler<String> responseHandler = new BasicResponseHandler();
return client.execute(method, responseHandler);
}
}
FileGroupsClient(
ResponseHandler<ChunkServer.FileGroups> filesGroupsHandler,
ResponseHandler<List<ICloud.MBSFileAuthToken>> mbsFileAuthTokenListHandler,
Headers headers) {
this.filesGroupsHandler = Objects.requireNonNull(filesGroupsHandler);
this.mbsFileAuthTokenListHandler = Objects.requireNonNull(mbsFileAuthTokenListHandler);
this.headers = Objects.requireNonNull(headers);
}
private String executeRequest(HttpUriRequest request) throws IOException {
try (CloseableHttpClient client = HttpClients.createDefault()) {
ResponseHandler<String> responseHandler = new BasicResponseHandler();
return client.execute(request, responseHandler);
}
}
public <T> T post(String url, Object body, Map<String, String> headers,
ResponseHandler<T> responseHandler, int... statusCodes) throws IOException {
HttpUriRequest request = newPost(url, body);
for (Entry<String, String> entry : headers.entrySet()) {
request.addHeader(entry.getKey(), entry.getValue());
}
return httpClient.execute(request, new StatusCodeCheckerResponseHandler<>(responseHandler, statusCodes));
}
@Override
public <T> T execute(final HttpUriRequest request,
final ResponseHandler<? extends T> responseHandler) throws IOException,
ClientProtocolException {
Subsegment subsegment = recorder.beginSubsegment(determineTarget(request).getHostName());
return wrapHttpSupplier(subsegment, () -> {
if (null != subsegment) {
TracedHttpClient.addRequestInformation(subsegment, request, TracedHttpClient.getUrl(request));
}
TracedResponseHandler<? extends T> wrappedHandler = new TracedResponseHandler<>(responseHandler);
T response = wrappedClient.execute(request, wrappedHandler);
return response;
});
}
@SuppressWarnings("unchecked")
private void givenThatGetWillSucceedWithOk() throws IOException {
new Expectations() {{
mockDelegate.get(anyString, (ResponseHandler<Integer>) any, HTTP_OK, HTTP_NOT_FOUND);
result = HTTP_OK;
}};
}
public <T> T post(String url, Object body, Map<String, String> headers,
ResponseHandler<T> responseHandler, int... statusCodes) throws IOException {
HttpUriRequest request = newPost(url, body);
for (Entry<String, String> entry : headers.entrySet()) {
request.addHeader(entry.getKey(), entry.getValue());
}
return httpClient.execute(request, new StatusCodeCheckerResponseHandler<>(responseHandler, statusCodes));
}
@SuppressWarnings({"rawtypes", "unchecked"})
private void givenThePushWillFailAndEventuallySucceed(final int retries) throws IOException {
new Expectations() {{
int fail = retries;
mockDelegate.post(anyString, null, (Map<String, String>) any, (ResponseHandler) any, 200);
minTimes = fail; maxTimes = fail;
result = new HttpResponseException(HTTP_INTERNAL_ERROR, "error");
mockDelegate.post(anyString, null, (Map<String, String>) any, (ResponseHandler) any, 200);
minTimes = 1; maxTimes = 1;
}};
}
@Override
public <T> T execute(
final HttpHost target,
final HttpRequest request,
final ResponseHandler<? extends T> responseHandler)
throws IOException, ClientProtocolException {
return this.execute(target, request, responseHandler, null);
}
protected ResponseHandler<StreamResponseMessage> createResponseHandler() {
return new ResponseHandler<StreamResponseMessage>() {
public StreamResponseMessage handleResponse(final HttpResponse httpResponse) throws IOException {
StatusLine statusLine = httpResponse.getStatusLine();
if (log.isLoggable(Level.FINE))
log.fine("Received HTTP response: " + statusLine);
// Status
UpnpResponse responseOperation =
new UpnpResponse(statusLine.getStatusCode(), statusLine.getReasonPhrase());
// Message
StreamResponseMessage responseMessage = new StreamResponseMessage(responseOperation);
// Headers
responseMessage.setHeaders(new UpnpHeaders(HeaderUtil.get(httpResponse)));
// Body
HttpEntity entity = httpResponse.getEntity();
if (entity == null || entity.getContentLength() == 0) return responseMessage;
if (responseMessage.isContentTypeMissingOrText()) {
if (log.isLoggable(Level.FINE))
log.fine("HTTP response message contains text entity");
responseMessage.setBody(UpnpMessage.BodyType.STRING, EntityUtils.toString(entity));
} else {
if (log.isLoggable(Level.FINE))
log.fine("HTTP response message contains binary entity");
responseMessage.setBody(UpnpMessage.BodyType.BYTES, EntityUtils.toByteArray(entity));
}
return responseMessage;
}
};
}
/**
* delete 方法
* @param url delete 的 url
* @param heads Http Head 参数
* */
public static String delete(String url,Map<String,String> heads) throws IOException {
try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
HttpDelete httpDelete = new HttpDelete(url);
if(heads!=null){
Set<String> keySet=heads.keySet();
for(String s:keySet){
httpDelete.addHeader(s,heads.get(s));
}
}
System.out.println("Executing request " + httpDelete.getRequestLine());
// Create a custom response handler
ResponseHandler < String > responseHandler = response -> {
int status = response.getStatusLine().getStatusCode();
if (status >= 200 && status < 300) {
HttpEntity entity = response.getEntity();
return entity != null ? EntityUtils.toString(entity) : null;
} else {
throw new ClientProtocolException("Unexpected response status: " + status);
}
};
String responseBody = httpclient.execute(httpDelete, responseHandler);
System.out.println("----------------------------------------");
System.out.println(responseBody);
return responseBody;
}
}
public static Object[] enter(final Object arg0, final Object arg1, final Object arg2) {
final HttpRequest request = arg0 instanceof HttpRequest ? (HttpRequest)arg0 : arg1 instanceof HttpRequest ? (HttpRequest)arg1 : null;
if (request == null)
return null;
if (request.getHeaders("amz-sdk-invocation-id").length > 0) {
// skip embedded Apache HttpClient in AWS SDK Client, because it breaks
// request signature and AWS SDK gets traced by the aws-sdk rule
return null;
}
final LocalSpanContext context = LocalSpanContext.get(COMPONENT_NAME);
if (context != null) {
context.increment();
return null;
}
final Tracer tracer = GlobalTracer.get();
final Span span = tracer
.buildSpan(request.getRequestLine().getMethod())
.withTag(Tags.COMPONENT, COMPONENT_NAME)
.withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT)
.withTag(Tags.HTTP_METHOD, request.getRequestLine().getMethod())
.withTag(Tags.HTTP_URL, request.getRequestLine().getUri()).start();
for (final ApacheClientSpanDecorator decorator : Configuration.spanDecorators)
decorator.onRequest(request, arg0 instanceof HttpHost ? (HttpHost)arg0 : null, span);
LocalSpanContext.set(COMPONENT_NAME, span, null);
tracer.inject(span.context(), Builtin.HTTP_HEADERS, new HttpHeadersInjectAdapter(request));
if (arg1 instanceof ResponseHandler)
return new Object[] {WrapperProxy.wrap(arg1, new TracingResponseHandler<>((ResponseHandler<?>)arg1, span))};
if (arg2 instanceof ResponseHandler)
return new Object[] {null, WrapperProxy.wrap(arg2, new TracingResponseHandler<>((ResponseHandler<?>)arg2, span))};
return null;
}
protected String doHttpGet(String url) throws ClientProtocolException,
IOException {
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
try {
String responseBody = httpclient.execute(httpget, responseHandler);
return responseBody;
} finally {
httpclient.getConnectionManager().shutdown();
}
}
public static <T> T _executeRequest(String httpPool,Map<String, String> headers,String url, String entity,String action,ResponseHandler<T> responseHandler) throws Exception {
T response = null;
if (entity == null){
if(action == null)
response = HttpRequestUtil.httpPostforString(httpPool,url, null, headers, responseHandler);
else if(action == ClientUtil.HTTP_POST )
response = HttpRequestUtil.httpPostforString(httpPool,url, null, headers, responseHandler);
else if( action == ClientUtil.HTTP_PUT)
response = HttpRequestUtil.httpPutforString(httpPool,url, null, headers, responseHandler);
else if(action == ClientUtil.HTTP_GET)
response = HttpRequestUtil.httpGetforString(httpPool,url, headers, responseHandler);
else if(action == ClientUtil.HTTP_DELETE)
response = HttpRequestUtil.httpDelete(httpPool,url, null, headers, responseHandler);
else if(action == ClientUtil.HTTP_HEAD)
response = HttpRequestUtil.httpHead(httpPool,url, null, headers, responseHandler);
else
throw new IllegalArgumentException("not support http action:"+action+",url:"+url);
}
else
{
if(action == ClientUtil.HTTP_POST )
response = HttpRequestUtil.sendJsonBody(httpPool,entity, url, headers, responseHandler);
else if( action == ClientUtil.HTTP_PUT)
{
response = HttpRequestUtil.putJson(httpPool,entity, url, headers, responseHandler);
}
else if(action == ClientUtil.HTTP_DELETE)
response = HttpRequestUtil.httpDelete(httpPool,url, entity,null, headers, responseHandler);
else
throw new IllegalArgumentException("not support http action:"+action+",url:"+url);
}
return response;
}
@Override
public <T> T execute(final HttpUriRequest request,
final ResponseHandler<? extends T> responseHandler) throws IOException,
ClientProtocolException {
final HttpContext context = new BasicHttpContext();
return execute(request, responseHandler, context);
}
/**
* @param entity
* @param action get,post,put,delete
* @return
* @throws ElasticSearchException
*/
public <T> T executeHttp(String url, String entity,String action,ResponseHandler<T> responseHandler) throws Exception {
// return _executeHttp( url, entity, action, responseHandler);
Integer slowDslThreshold = elasticSearchClient.slowDslThreshold();
if(slowDslThreshold == null) {
return RestSearchExecutorUtil.__executeHttp( httpPool, (Map<String, String>)null, url, entity, action, responseHandler);
}
else{
long start = System.currentTimeMillis();
try {
return RestSearchExecutorUtil.__executeHttp( httpPool, (Map<String, String>)null, url, entity, action, responseHandler);
}
finally {
long end = System.currentTimeMillis();
long time = end - start;
if (time > slowDslThreshold.intValue()) {
if (elasticSearchClient.getSlowDslCallback() == null) {
if(logger.isWarnEnabled()) {
logger.warn("Slow request[{}] action[{}] took time:{} ms > slowDslThreshold[{} ms], use DSL[{}]", url,action, time, slowDslThreshold.intValue(), RestSearchExecutorUtil.chunkEntity(entity));
}
}else {
SlowDsl slowDsl = new SlowDsl();
slowDsl.setUrl(url);
slowDsl.setAction(action);
slowDsl.setTime(time);
slowDsl.setSlowDslThreshold(slowDslThreshold);
slowDsl.setEntity(entity);
slowDsl.setStartTime(new Date(start));
slowDsl.setEndTime(new Date(end));
elasticSearchClient.getSlowDslCallback().slowDslHandle( slowDsl);
}
}
}
}
}
public static <T> T __executeHttp(String httpPool,Map<String, String> headers,String url, String entity,String action,ResponseHandler<T> responseHandler) throws Exception{
T response = null;
if (entity == null) {
if (action == null)
response = HttpRequestUtil.httpPostforString(httpPool, url, null, headers, responseHandler);
else if (action == ClientUtil.HTTP_POST)
response = HttpRequestUtil.httpPostforString(httpPool, url, null, headers, responseHandler);
else if (action == ClientUtil.HTTP_PUT)
response = HttpRequestUtil.httpPutforString(httpPool, url, null, headers, responseHandler);
else if (action == ClientUtil.HTTP_GET)
response = HttpRequestUtil.httpGetforString(httpPool, url, headers, responseHandler);
else if (action == ClientUtil.HTTP_DELETE)
response = HttpRequestUtil.httpDelete(httpPool, url, null, headers, responseHandler);
else if (action == ClientUtil.HTTP_HEAD)
response = HttpRequestUtil.httpHead(httpPool, url, null, headers, responseHandler);
else
throw new IllegalArgumentException("not support http action:" + action+",url:"+url);
} else {
if (action == ClientUtil.HTTP_POST)
response = HttpRequestUtil.sendJsonBody(httpPool, entity, url, headers, responseHandler);
else if (action == ClientUtil.HTTP_PUT) {
response = HttpRequestUtil.putJson(httpPool, entity, url, headers, responseHandler);
} else if (action == ClientUtil.HTTP_DELETE)
response = HttpRequestUtil.httpDelete(httpPool, url, entity, null, headers, responseHandler);
else
throw new IllegalArgumentException("not support http action:" + action+",url:"+url);
}
return response;
}
@Override
public <T> T execute(HttpUriRequest request, ResponseHandler<? extends T> responseHandler, HttpContext context) throws IOException, ClientProtocolException {
HttpRequestWrapper wrap = HttpRequestWrapper.wrap(request);
adviseRobotsTxt(wrap.getURI());
wrap.setURI(applyPHP(wrap.getURI()));
return client.execute(wrap, responseHandler, context);
}
private <T> T handleResponse(final HttpResponse response, final ResponseHandler<T> handler) throws ClientProtocolException, IOException {
try {
return handler.handleResponse(response);
} finally {
try {
final HttpEntity entity = response.getEntity();
final InputStream content = entity.getContent();
if (content != null) {
content.close();
}
} catch (final Exception ignore) {
}
}
}
protected ResponseHandler<StreamResponseMessage> createResponseHandler() {
return new ResponseHandler<StreamResponseMessage>() {
public StreamResponseMessage handleResponse(final HttpResponse httpResponse) throws IOException {
StatusLine statusLine = httpResponse.getStatusLine();
if (log.isLoggable(Level.FINE))
log.fine("Received HTTP response: " + statusLine);
// Status
UpnpResponse responseOperation =
new UpnpResponse(statusLine.getStatusCode(), statusLine.getReasonPhrase());
// Message
StreamResponseMessage responseMessage = new StreamResponseMessage(responseOperation);
// Headers
responseMessage.setHeaders(new UpnpHeaders(HeaderUtil.get(httpResponse)));
// Body
HttpEntity entity = httpResponse.getEntity();
if (entity == null || entity.getContentLength() == 0) return responseMessage;
if (responseMessage.isContentTypeMissingOrText()) {
if (log.isLoggable(Level.FINE))
log.fine("HTTP response message contains text entity");
responseMessage.setBody(UpnpMessage.BodyType.STRING, EntityUtils.toString(entity));
} else {
if (log.isLoggable(Level.FINE))
log.fine("HTTP response message contains binary entity");
responseMessage.setBody(UpnpMessage.BodyType.BYTES, EntityUtils.toByteArray(entity));
}
return responseMessage;
}
};
}
private <T> T executeRequest(HttpUriRequest request, ResponseHandler<T> handler) {
try {
return client.execute(request, handler);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}