@Override public String toString() { return myRequest.toString(); }
@Override public String toString() { return myRequest.toString(); }
private boolean contentExists(HttpRequestBase requestBase, ThrowingFunction<Pair<HttpRequestBase, Integer>, Boolean, IOException> badStatusHandler) { return httpClient.executeRequest(requestBase, r -> true, badStatusHandler, re -> { LOGGER.warn("Exception while checking if resource " + re.getLeft().toString() + " exists", re.getRight()); return false; }); } }
public void handleErrorResponse(HttpResponse response, HttpRequestBase request) { int status = response.getStatusLine().getStatusCode(); Header header = response.getFirstHeader("Err"); String errMsg = (header != null && header.getValue() != null) ? header.getValue() : ""; if (401 == status) { logger.info(">>WARNING: Unauthorized!msg:{},request:{}", errMsg, request.toString()); } else if (403 == status) { logger.info(">>WARNING: Forbidden!msg:{},request:{}", errMsg, request.toString()); } else if (555 == status) { logger.info(">>WARNING: gateway current limit!msg:{},request:{}", errMsg, request.toString()); } else { logger.info(">>WARNING: send to gateway fail!status:{}!msg:{}request:{}", status, errMsg, request.toString()); } //change silentTime if (response.containsHeader(WAIT_MINUTES)) { String waitMinutesStr = response.getFirstHeader(WAIT_MINUTES).getValue(); changeSilentTime(waitMinutesStr); } }
@Override public boolean contentExists(String url) { if (null == url) { return false; } AtomicBoolean shouldRetryWithGetRequest = new AtomicBoolean(false); Boolean contentExists = contentExists(new HttpHead(url), rsc -> { shouldRetryWithGetRequest.set(HttpStatus.SC_METHOD_NOT_ALLOWED == rsc.getRight()); LOGGER.info("Request: " + rsc.getLeft().toString() + " finished with status: " + rsc.getRight()); return false; }); if (shouldRetryWithGetRequest.get()) { contentExists = contentExists(new HttpGet(url), SynatHttpClient.getDefaultBadStatusHandler(() -> false)); } return contentExists; }
public static byte[] executeDownload(HttpRequestBase request, HttpClient httpClient) { try { HttpResponse response = httpClient.execute(request); HttpEntity responseEntity = response.getEntity(); return FileUtil.importFileBytes(responseEntity.getContent()); } catch (Exception e) { log.error(e.getLocalizedMessage()); throw new ConnectionException(request.toString(), e); } finally { request.releaseConnection(); } }
protected String performRequest(HttpClientContext context, HttpRequestBase httpRequest) throws IOException { CloseableHttpResponse response = httpClient.execute(httpRequest, context); if (logger.isInfoEnabled()) { logger.info("Request to " + httpRequest.toString()); logger.info("Response " + response.getStatusLine());
private String executeHttpCall(final HttpRequestBase httpRequest, final String body, final String identityToken) throws RESTClientException { try { LOGGER.info("Executing http call to: {} ", httpRequest.toString()); if (StringUtils.hasText(body)) { ((HttpEntityEnclosingRequestBase) httpRequest).setEntity(new StringEntity(body, ContentType.APPLICATION_JSON)); } if (StringUtils.hasText(identityToken)) { httpRequest.addHeader(RESTUtils.buildIdentityHeader(identityToken)); } if (StringUtils.hasText(secretKey)) { addSignedHeader(httpRequest, body); } final HttpResponse response = httpClient.execute(httpRequest); validateResponse(response); if (response.getEntity() != null) { return EntityUtils.toString(response.getEntity()); } else { return ""; } } catch (final RESTClientException e) { LOGGER.error("Error executing http call: {} ", httpRequest.toString(), e); throw e; } catch (final Exception e) { LOGGER.error("Error executing http call: {} ", httpRequest.toString(), e); final String msg = String.format("Error executing http call: %s ", httpRequest.toString()); throw new RESTClientException(msg, e); } }
/** * 原生HttpClient使用方法 * @author jwSun * @date 2017年6月14日 下午4:21:29 * @param request * @return */ public static ResponseEntity<String> execute(HttpRequestBase request, HttpClient httpClient) { try { HttpResponse response = httpClient.execute(request); HttpEntity responseEntity = response.getEntity(); String body = EntityUtils.toString(responseEntity, CharsetEnum.UTF_8.getCharset()); EntityUtils.consume(response.getEntity()); return ResponseEntity.status(response.getStatusLine().getStatusCode()).body(body); } catch (Exception e) { log.error(e.getLocalizedMessage()); throw new ConnectionException(request.toString(), e); } finally { request.releaseConnection(); } }
private String executeRequest(HttpRequestBase request) { String json = null; try { BasicHttpContext context = createLocalContext(client); HttpResponse response = client.execute(request, context); HttpEntity entity = response.getEntity(); if (entity != null) { if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { json = EntityUtils.toString(entity); } else if (response.getStatusLine().getStatusCode() != HttpStatus.SC_NOT_FOUND) { throw new ConnectionException("HTTP error: " + response.getStatusLine().getStatusCode() + ", msg: " + response.getStatusLine().getReasonPhrase() + ", query: " + request.toString()); } } } catch (IOException e) { throw new ConnectionException("Query: " + request.getURI(), e); } finally { request.releaseConnection(); } return json; }
private String getRequestString(HttpRequestBase request) { Header[] headers = request.getAllHeaders(); String headersString = ""; for (Header h : headers) { headersString = headersString + h.getName() + ": " + h.getValue() + "\n"; } return headersString + "\n" + request.toString(); }
LOGGER.debug("Http request: " + request.toString());
"WSC: Connection configured to have request properties " + request.toString());
"WSC: Connection configured to have request properties " + request.toString());