@Override public int getRawStatusCode() throws IOException { return this.httpResponse.getStatusLine().getStatusCode(); }
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()); }
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); }
public String getLookupSnapshotCacheState(String lookupTableName, String snapshotID) throws IOException { String url = baseUrl + "/tables/" + lookupTableName + "/" + snapshotID + "/snapshotLocalCache/state"; HttpGet get = new HttpGet(url); HttpResponse response = client.execute(get); String content = getContent(response); if (response.getStatusLine().getStatusCode() != 200) { throw new IOException(INVALID_RESPONSE + response.getStatusLine().getStatusCode() + WITH_URL + url + "\n"); } return content; }
public static void testUrl(String url) throws IOException { HttpHead head = new HttpHead(url); HttpClient httpclient = getClient(); HttpResponse response = httpclient.execute(head); StatusLine status = response.getStatusLine(); if (status.getStatusCode() == 404) { throw new FileNotFoundException(); } if (status.getStatusCode() != 200) { throw new RuntimeException("Could not get URL: " + status.getStatusCode() + ": " + status.getReasonPhrase()); } }
@Test public void shouldRespondToHttpClientGet() throws Exception { try ( DefaultHttpClient httpclient = new DefaultHttpClient() ) { HttpGet httpget = new HttpGet( serverUrl + "/?id=storeId+v=kernelVersion" ); try ( CloseableHttpResponse response = httpclient.execute( httpget ) ) { HttpEntity entity = response.getEntity(); if ( entity != null ) { try ( InputStream instream = entity.getContent() ) { byte[] tmp = new byte[2048]; while ( (instream.read( tmp )) != -1 ) { } } } assertThat( response, notNullValue() ); assertThat( response.getStatusLine().getStatusCode(), is( HttpStatus.SC_OK ) ); } } }
private void checkServerRunning() { try (CloseableHttpClient client = HttpClientBuilder.create().build()) { CloseableHttpResponse response = client.execute(new HttpGet(baseUrl + PING_PATH)); if (response != null && response.getStatusLine() != null) { Assume.assumeThat(response.getStatusLine().getStatusCode(), Is.is(200)); } } catch (IOException e) { throw new AssumptionViolatedException("SolrServer does not seem to be running", e); } }
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); } }
@Before public void init() throws IOException { SlaveConnectionManager connectionManager = SlaveConnectionManager.getInstance(); HttpClient httpClient = spy( connectionManager.createHttpClient() ); // mock response CloseableHttpResponse closeableHttpResponseMock = mock( CloseableHttpResponse.class ); // mock status line StatusLine statusLineMock = mock( StatusLine.class ); doReturn( HttpStatus.SC_NOT_FOUND ).when( statusLineMock ).getStatusCode(); doReturn( statusLineMock ).when( closeableHttpResponseMock ).getStatusLine(); // mock entity HttpEntity httpEntityMock = mock( HttpEntity.class ); doReturn( httpEntityMock ).when( closeableHttpResponseMock ).getEntity(); doReturn( closeableHttpResponseMock ).when( httpClient ).execute( any( HttpGet.class ) ); doReturn( closeableHttpResponseMock ).when( httpClient ).execute( any( HttpPost.class ) ); doReturn( closeableHttpResponseMock ).when( httpClient ).execute( any( HttpPost.class ), any( HttpClientContext.class ) ); slaveServer = spy( new SlaveServer() ); doReturn( httpClient ).when( slaveServer ).getHttpClient(); doReturn( "response_body" ).when( slaveServer ).getResponseBodyAsString( any( InputStream.class ) ); }
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());
@Test public void shouldHandleMultipleInvocations() throws Exception { FakeHttp.addPendingHttpResponse(200, "a happy response body"); FakeHttp.addPendingHttpResponse(201, "another happy response body"); HttpResponse response1 = requestDirector.execute(null, new HttpGet("http://example.com"), null); HttpResponse response2 = requestDirector.execute(null, new HttpGet("www.example.com"), null); assertThat(response1.getStatusLine().getStatusCode()).isEqualTo(200); assertThat(getStringContent(response1)).isEqualTo("a happy response body"); assertThat(response2.getStatusLine().getStatusCode()).isEqualTo(201); assertThat(getStringContent(response2)).isEqualTo("another happy response body"); }
/** * @param response The http response to verify. * @return null on failure or the response string if return code is in 200 range */ @Override public String handleResponse(final HttpResponse response) throws IOException { int status = response.getStatusLine().getStatusCode(); HttpEntity entity = response.getEntity(); String entityString = (entity != null ? EntityUtils.toString(entity) : null); if (status >= 200 && status < 300) { return entityString; } else { LOG.error("Got unexpected response code {}; entity: {}", status, entityString); return null; } } }
@Override public int getRawStatusCode() throws IOException { return this.httpResponse.getStatusLine().getStatusCode(); }
@Override public void connect() throws IOException { HttpPost post = new HttpPost(baseUrl() + "/kylin/api/user/authentication"); addHttpHeaders(post); StringEntity requestEntity = new StringEntity("{}", ContentType.create("application/json", "UTF-8")); post.setEntity(requestEntity); try { HttpResponse response = httpClient.execute(post); if (response.getStatusLine().getStatusCode() != 200 && response.getStatusLine().getStatusCode() != 201) { throw asIOException(post, response); } } finally { post.releaseConnection(); } }
@Test public void testGetIndex() throws IOException { CloseableHttpClient client = HttpClients.createDefault(); HttpGet getReq = new HttpGet(String.format("http://localhost:%s/", this.portNumber)); try (CloseableHttpResponse response = client.execute(getReq)) { assertEquals(200, response.getStatusLine().getStatusCode()); HttpEntity body = response.getEntity(); String bodyString = EntityUtils.toString(body); assertStringContains("JOB SUMMARY", bodyString); } }
/** * {@inheritDoc} */ @Override public ClientResponse apply(ClientRequest jerseyRequest) { try { final HttpUriRequest apacheRequest = buildApacheRequest(jerseyRequest); final CloseableHttpResponse apacheResponse = client.execute(apacheRequest); final StatusLine statusLine = apacheResponse.getStatusLine(); final String reasonPhrase = Strings.nullToEmpty(statusLine.getReasonPhrase()); final Response.StatusType status = Statuses.from(statusLine.getStatusCode(), reasonPhrase); final ClientResponse jerseyResponse = new ClientResponse(status, jerseyRequest); for (Header header : apacheResponse.getAllHeaders()) { jerseyResponse.getHeaders().computeIfAbsent(header.getName(), k -> new ArrayList<>()) .add(header.getValue()); } final HttpEntity httpEntity = apacheResponse.getEntity(); jerseyResponse.setEntityStream(httpEntity != null ? httpEntity.getContent() : new ByteArrayInputStream(new byte[0])); return jerseyResponse; } catch (Exception e) { throw new ProcessingException(e); } }
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(); }
@Test public void shouldGetHttpResponseFromExecuteSimpleApi() throws Exception { FakeHttp.addPendingHttpResponse(200, "a happy response body"); HttpResponse response = requestDirector.execute(null, new HttpGet("http://example.com"), null); assertThat(response.getStatusLine().getStatusCode()).isEqualTo(200); assertThat(getStringContent(response)).isEqualTo("a happy response body"); }
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); }
@Override public int getStatusCode() { return response == null || response.getStatusLine() == null ? 0 : response.getStatusLine().getStatusCode(); }