@Override public String getStatusText() throws IOException { return this.httpResponse.getStatusLine().getReasonPhrase(); }
HttpClient httpclient = new DefaultHttpClient(); HttpResponse response = httpclient.execute(new HttpGet(URL)); StatusLine statusLine = response.getStatusLine(); if(statusLine.getStatusCode() == HttpStatus.SC_OK){ ByteArrayOutputStream out = new ByteArrayOutputStream(); response.getEntity().writeTo(out); String responseString = out.toString(); out.close(); //..more logic } else{ //Closes the connection. response.getEntity().getContent().close(); throw new IOException(statusLine.getReasonPhrase()); }
/** * Validate the given response as contained in the HttpPost object, * throwing an exception if it does not correspond to a successful HTTP response. * <p>Default implementation rejects any HTTP status code beyond 2xx, to avoid * parsing the response body and trying to deserialize from a corrupted stream. * @param config the HTTP invoker configuration that specifies the target service * @param response the resulting HttpResponse to validate * @throws java.io.IOException if validation failed */ protected void validateResponse(HttpInvokerClientConfiguration config, HttpResponse response) throws IOException { StatusLine status = response.getStatusLine(); if (status.getStatusCode() >= 300) { throw new NoHttpResponseException( "Did not receive successful HTTP response: status code = " + status.getStatusCode() + ", status message = [" + status.getReasonPhrase() + "]"); } }
private void handleHTTPError(CloseableHttpResponse response) throws RemoteException { final StatusLine statusLine = response.getStatusLine(); final int status = statusLine.getStatusCode(); final String statusText = statusLine.getReasonPhrase(); final String body = response.getEntity().toString(); StringBuilder strBuf = new StringBuilder(); strBuf.append("Crowd returned HTTP error code:").append(status); strBuf.append(" - ").append(statusText); if (StringUtils.isNotBlank(body)) { strBuf.append("\n").append(body); } throw new RemoteException(strBuf.toString()); }
private void checkHttpResponseStatus(HttpResponse response) { if (response.getStatusLine().getStatusCode() == 200) { return; } StatusLine status = response.getStatusLine(); String message = ""; try { message = EntityUtils.toString(response.getEntity()); } catch (IOException e) { //ignore } throw new ApolloOpenApiException(status.getStatusCode(), status.getReasonPhrase(), message); }
HttpException(String message, HttpResponse response) { super(String.format("%s: %d %s.%n%s", message, response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase(), trySlurpContent(response))); this.statusCode = response.getStatusLine().getStatusCode(); }
public HttpResponseException( HttpResponseDecorator resp ) { super( resp.getStatusLine().getStatusCode(), resp.getStatusLine().getReasonPhrase() ); this.response = resp; }
@Override public String getStatusText() throws IOException { return this.httpResponse.getStatusLine().getReasonPhrase(); }
String responseString = null; try { response = httpclient.execute(new HttpGet(uri[0])); StatusLine statusLine = response.getStatusLine(); if(statusLine.getStatusCode() == HttpStatus.SC_OK){ ByteArrayOutputStream out = new ByteArrayOutputStream(); response.getEntity().writeTo(out); responseString = out.toString(); out.close(); } else{ response.getEntity().getContent().close(); throw new IOException(statusLine.getReasonPhrase());
/** * Validate the given response as contained in the HttpPost object, * throwing an exception if it does not correspond to a successful HTTP response. * <p>Default implementation rejects any HTTP status code beyond 2xx, to avoid * parsing the response body and trying to deserialize from a corrupted stream. * @param config the HTTP invoker configuration that specifies the target service * @param response the resulting HttpResponse to validate * @throws java.io.IOException if validation failed */ protected void validateResponse(HttpInvokerClientConfiguration config, HttpResponse response) throws IOException { StatusLine status = response.getStatusLine(); if (status.getStatusCode() >= 300) { throw new NoHttpResponseException( "Did not receive successful HTTP response: status code = " + status.getStatusCode() + ", status message = [" + status.getReasonPhrase() + "]"); } }
private Map<String, Object> parseResponse(Response response) throws IOException { final int code = response.getStatusLine().getStatusCode(); if (code >= 200 & code < 300) { InputStream inputStream = response.getEntity().getContent(); byte[] result = IOUtils.toByteArray(inputStream); inputStream.close(); return mapper.readValue(new String(result, charset), Map.class); } else { String errorMessage = String.format("ElasticSearch reported an error while trying to run the query: %s", response.getStatusLine().getReasonPhrase()); throw new IOException(errorMessage); } }
@Override public String getStatusMessage() { return response == null || response.getStatusLine() == null ? null : response.getStatusLine().getReasonPhrase(); }
public String handleResponse(final HttpResponse response) throws HttpResponseException, IOException { final StatusLine statusLine = response.getStatusLine(); final HttpEntity entity = response.getEntity(); if (statusLine.getStatusCode() >= 300) { EntityUtils.consume(entity); throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase()); } return entity == null ? null : EntityUtils.toString(entity, Consts.UTF_8); }
public void assertOk(org.apache.http.HttpResponse resp) throws UnknownHttpResponseException { // Throw RuntimeException if HTTP error if (resp == null) { throw new UnknownHttpResponseException("Null HttpResponse", -2); } int resultCode = resp.getStatusLine().getStatusCode(); if (!(resultCode == 200 || resultCode == 403)) { String reason = resp.getStatusLine().getReasonPhrase(); throw new UnknownHttpResponseException(reason, resultCode); } }
private String getHttpResponse(String fullUrl) throws IOException { HttpUriRequest req = new HttpGet(fullUrl); for (int numTries = 0;; numTries++) { try (CloseableHttpResponse response = this.httpClient.execute(req)) { int statusCode = response.getStatusLine().getStatusCode(); if (statusCode < 200 || statusCode >= 300) { throw new IOException( String.format("status code: %d, reason: %s", statusCode, response.getStatusLine().getReasonPhrase())); } return EntityUtils.toString(response.getEntity()); } catch (IOException e) { String errMsg = "Unable to get or parse HTTP response for " + fullUrl; if (numTries >= this.maxNumTries) { throw new IOException (errMsg, e); } long backOffSec = (numTries + 1) * 2; log.error(errMsg + ", will retry in " + backOffSec + " sec", e); try { Thread.sleep(TimeUnit.SECONDS.toMillis(backOffSec)); } catch (InterruptedException e1) { Thread.currentThread().interrupt(); } } } } }
@Override public String getStatusMessage() { return response == null || response.getStatusLine() == null ? null : response.getStatusLine().getReasonPhrase(); }
public InputStream handleResponse(final HttpResponse response) throws HttpResponseException, IOException { final StatusLine statusLine = response.getStatusLine(); final HttpEntity entity = response.getEntity(); if (statusLine.getStatusCode() >= 300) { EntityUtils.consume(entity); throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase()); } return entity == null ? null : entity.getContent(); }
@Override public void process(HttpResponse response, HttpContext context) throws HttpException, IOException { // Avoid work if TRACE is not enabled for this class if (logger.isTraceEnabled()) { final StatusLine statusLine = response.getStatusLine(); final HttpHost targetHost = (HttpHost) context.getAttribute(HttpCoreContext.HTTP_TARGET_HOST); final HttpRequest httpRequest = (HttpRequest) context .getAttribute(HttpCoreContext.HTTP_REQUEST); final RequestLine request = httpRequest.getRequestLine(); logger.trace("[{} {}]: {} {}{}", statusLine.getStatusCode(), statusLine.getReasonPhrase(), request.getMethod(), targetHost.toURI(), request.getUri() ); } } }
private String execute(final HttpGet get) throws IOException { final CloseableHttpClient httpClient = getHttpClient(); if (logger.isTraceEnabled()) { Arrays.stream(get.getAllHeaders()).forEach(h -> logger.debug("REQ| {}", h)); } try (final CloseableHttpResponse response = httpClient.execute(get)) { if (logger.isTraceEnabled()) { Arrays.stream(response.getAllHeaders()).forEach(h -> logger.debug("RES| {}", h)); } final StatusLine statusLine = response.getStatusLine(); final int statusCode = statusLine.getStatusCode(); if (RESPONSE_CODE_OK != statusCode) { throw new HttpGetFailedException(statusCode, statusLine.getReasonPhrase(), null); } final HttpEntity entity = response.getEntity(); final String responseMessage = EntityUtils.toString(entity); return responseMessage; } }
@Override public String getStatusText() throws IOException { return this.httpResponse.getStatusLine().getReasonPhrase(); }