下面列出了org.apache.http.client.methods.HttpRequestBase#getMethod() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private String sendAndGetResponse(HttpRequestBase request) throws IOException {
String data = StringUtils.EMPTY;
try {
HttpResponse response = client.execute(request, null).get(30, TimeUnit.SECONDS);
int code = response.getStatusLine().getStatusCode();
if (code == 200) {
try (InputStream responseContent = response.getEntity().getContent()) {
data = IOUtils.toString(responseContent, "UTF-8");
}
} else {
LOG.error("ZeppelinHub {} {} returned with status {} ", request.getMethod(),
request.getURI(), code);
throw new IOException("Cannot perform " + request.getMethod() + " request to ZeppelinHub");
}
} catch (InterruptedException | ExecutionException | TimeoutException
| NullPointerException e) {
throw new IOException(e);
}
return data;
}
public HttpResponse performRequest(HttpRequestBase request) {
String method = request.getMethod();
HttpResponse response;
try {
response = executeGetOrHead(request);
} catch (IOException e) {
throw new HttpRequestException(String.format("Could not %s '%s'.", method, request.getURI()), e);
}
return response;
}
public HttpResponse performRequest(HttpRequestBase request) {
String method = request.getMethod();
HttpResponse response;
try {
response = executeGetOrHead(request);
} catch (IOException e) {
throw new HttpRequestException(String.format("Could not %s '%s'.", method, request.getURI()), e);
}
return response;
}
/**
* method to get the wire.
* @param method
* @param responseBody
* @param headerString
* @param responseStatus
* @param subTests
* @return
* @throws ComplianceException
*/
public static Wire getWire(HttpRequestBase method, String responseBody,
String headerString, String responseStatus,
ArrayList<String> subTests) throws ComplianceException {
StringBuffer toServer = new StringBuffer();
StringBuffer fromServer = new StringBuffer();
StringBuffer subTestsPerformed = new StringBuffer();
toServer.append(method.getRequestLine().getMethod()).append(" ");
toServer.append(method.getRequestLine().getUri()+"\n");
toServer.append(method.getRequestLine().getProtocolVersion().getProtocol());
for (org.apache.http.Header header : method.getAllHeaders()) {
toServer.append(header.getName()).append(": ").append(header.getValue()).append("\n");
}
if(method.getMethod() != "GET" && method.getMethod() != "DELETE"){
try {
HttpEntity entity = ((HttpEntityEnclosingRequest)method).getEntity();
toServer.append(EntityUtils.toString(entity));
} catch (Exception e) {
throw new ComplianceException(500, "Error in getting the request payload");
}
}
fromServer.append("\n" + "Headers : ");
fromServer.append(headerString + "\n");
fromServer.append("\n" + "Status : ");
fromServer.append(responseStatus + "\n");
fromServer.append("\n" + responseBody);
for (String subTest : subTests) {
subTestsPerformed.append(subTest).append("\n");
}
return new Wire(toServer.toString(), fromServer.toString(), subTestsPerformed.toString());
}
private ResponseInfo<T> sendRequestForCache(HttpRequestBase request) {
requestMethod = request.getMethod();
boolean isEnableCache = com.youzu.android.framework.HttpUtils.sHttpCache.isEnabled(requestMethod);
// Log.e("APP", "requestMethod:" + requestMethod + " isEnableCache:" + isEnableCache);
if (isEnableCache) {
String result = com.youzu.android.framework.HttpUtils.sHttpCache.get(requestUrl + request.toString());
if (result != null) {
return new ResponseInfo<T>(null, (T) result, true);
}
}
return null;
}
public HttpResponse performRequest(HttpRequestBase request) {
String method = request.getMethod();
HttpResponse response;
try {
response = executeGetOrHead(request);
} catch (IOException e) {
throw new HttpRequestException(String.format("Could not %s '%s'.", method, request.getURI()), e);
}
return response;
}
public HttpResponse performRequest(HttpRequestBase request) {
String method = request.getMethod();
HttpResponse response;
try {
response = executeGetOrHead(request);
} catch (IOException e) {
throw new HttpRequestException(String.format("Could not %s '%s'.", method, request.getURI()), e);
}
return response;
}
@Override
public T coerce(final HttpRequestBase request, final HttpResponse response) {
final HttpEntity entity = response.getEntity();
this.requestMethod = request.getMethod();
this.requestContentType = null == entity ? null : ContentType.get(entity).getMimeType();
this.requestAcceptHeader = request.getFirstHeader("Accept").getValue();
return null;
}
/**
* Executes the HTTPMethod with retry.
*
* @param httpClient HTTPClient
* @param httpMethod HTTPMethod
* @param retryCount No of retries
* @return response. it will return an empty string if response body is null
* @throws OnPremiseGatewayException throws {@link OnPremiseGatewayException}
*/
public static String executeHTTPMethodWithRetry(HttpClient httpClient, HttpRequestBase httpMethod, int retryCount)
throws OnPremiseGatewayException {
String result = OnPremiseGatewayConstants.EMPTY_STRING;
HttpResponse response;
int executionCount = 0;
String methodName = httpMethod.getMethod();
String uri = getURI(httpMethod);
//Add an unique identifier as a custom header for distinguishing requests from different micro gateways.
String token = ConfigManager.getConfigurationDTO().getStatus_unique_identifier();
if (StringUtils.isNotBlank(token) && !(OnPremiseGatewayConstants.API_REQUEST_UNIQUE_IDENTIFIER_HOLDER
.equals(token))) {
if (log.isDebugEnabled()) {
log.debug("Adding unique identifier as an header to the http " + methodName + " request.");
}
httpMethod.addHeader(OnPremiseGatewayConstants.APT_REQUEST_TOKEN_HEADER, token);
}
do {
try {
executionCount++;
response = httpClient.execute(httpMethod);
if (log.isDebugEnabled()) {
log.debug(
"HTTP response code for the " + methodName + " request to URL: " + uri + " is " + response);
}
result = handleResponse(response, methodName, true, executionCount, retryCount, uri);
if (!OnPremiseGatewayConstants.EMPTY_STRING.equals(result)) {
return result;
}
} catch (IOException e) {
handleExceptionWithRetry(executionCount, retryCount, methodName, uri, e);
} finally {
httpMethod.releaseConnection();
}
} while (executionCount < retryCount);
return result;
}
/**
* Executes HTTPMethod without retry
*
* @param httpClient HTTPClient
* @param httpMethod HTTPMethod
* @return response. it will return an empty string if response body is null
* @throws OnPremiseGatewayException throws {@link OnPremiseGatewayException}
*/
public static String executeHTTPMethod(HttpClient httpClient, HttpRequestBase httpMethod)
throws OnPremiseGatewayException {
String result;
HttpResponse response;
String uri = getURI(httpMethod);
String methodName = httpMethod.getMethod();
//Add an unique identifier as an custom header for distinguishing requests from different micro gateways.
String token = ConfigManager.getConfigurationDTO().getStatus_unique_identifier();
if (StringUtils.isNotBlank(token) && !(OnPremiseGatewayConstants.API_REQUEST_UNIQUE_IDENTIFIER_HOLDER
.equals(token))) {
if (log.isDebugEnabled()) {
log.debug("Adding unique identifier as an header to the http " + methodName + " request.");
}
httpMethod.addHeader(OnPremiseGatewayConstants.APT_REQUEST_TOKEN_HEADER, token);
}
try {
response = httpClient.execute(httpMethod);
if (log.isDebugEnabled()) {
log.debug("HTTP response code for the " + methodName + " request: " + uri + " is " + response);
}
result = handleResponse(response, methodName, false, 0, 0, uri);
} catch (IOException e) {
throw new OnPremiseGatewayException(methodName + " request failed for URI: " + uri, e);
} finally {
httpMethod.releaseConnection();
}
return result;
}
protected String executeRequest(HttpRequestBase request) throws ElasticsearchClientException
{
Validate.notNull(httpClient, "Has the ElasticsearchClient been initialized?");
try
{
HttpResponse response = httpClient.execute(request);
int statusCode = response.getStatusLine().getStatusCode();
String content = readContent(response.getEntity());
log.debug(String.format(
"Response with status code %d and content: %s", statusCode, content));
// some PUT requests return 200, some 201 :-O
if (statusCode != 200 && statusCode != 201)
{
throw new ElasticsearchClientException(request.getMethod(), statusCode, content);
}
return content;
}
catch (IOException e)
{
throw new ElasticsearchClientException(e);
}
finally
{
request.releaseConnection();
}
}
private IOException asIOException(HttpRequestBase request, HttpResponse response) throws IOException {
return new IOException(request.getMethod() + " failed, error code " + response.getStatusLine().getStatusCode()
+ " and response: " + EntityUtils.toString(response.getEntity()));
}
private IOException asIOException(HttpRequestBase request, HttpResponse response) throws IOException {
return new IOException(request.getMethod() + " failed, error code " + response.getStatusLine().getStatusCode()
+ " and response: " + EntityUtils.toString(response.getEntity()));
}