@Override public int getStatusCode() { return response == null || response.getStatusLine() == null ? 0 : response.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; }
httpclient.getConnectionManager().getSchemeRegistry().register(sch); HttpGet httpget = new HttpGet("https://10.2.20.20/fido/EzPay/login.php"); HttpEntity entity = response.getEntity(); System.out.println(response.getStatusLine()); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); String line = ""; while ((line = in.readLine()) != null) {
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()); } }
HttpClient httpClient = HttpClientBuilder.create().build(); HttpGet request = new HttpGet(crawlUrl); HttpResponse rsp = httpClient.execute(request); int statusCode = rsp.getStatusLine().getStatusCode(); if (statusCode == 200) { String content = EntityUtils.toString(rsp.getEntity()); Document doc = Jsoup.parse(content); //parse content, whatever you need Element price = doc.select("[itemprop=price]").first(); }
private void sendData(ArrayList<NameValuePair> data) { // 1) Connect via HTTP. 2) Encode data. 3) Send data. try { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://www.blah.com/AddAccelerationData.php"); httppost.setEntity(new UrlEncodedFormEntity(data)); HttpResponse response = httpclient.execute(httppost); Log.i("postData", response.getStatusLine().toString()); //Could do something better with response. } catch(Exception e) { Log.e("log_tag", "Error: "+e.toString()); } }
public static HttpResponse mockHttpResponse(int statusCode, String message, String body) { HttpResponse response = Mockito.mock(HttpResponse.class); Mockito.when(response.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, statusCode, message)); Mockito.when(response.getEntity()).thenReturn(new StringEntity(body, StandardCharsets.UTF_8)); return response; }
DefaultHttpClient httpclient = new DefaultHttpClient(); httpclient.setRedirectStrategy(new DefaultRedirectStrategy() { public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context) { boolean isRedirect=false; try { isRedirect = super.isRedirected(request, response, context); } catch (ProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (!isRedirect) { int responseCode = response.getStatusLine().getStatusCode(); if (responseCode == 301 || responseCode == 302) { return true; } } return isRedirect; } });
@Test public void connect() throws IOException { HttpResponse response = mock(HttpResponse.class); when(httpClient.execute(any(HttpUriRequest.class))).thenReturn(response); when(response.getStatusLine()).thenReturn(new BasicStatusLine(HTTP_1_1, 200, "OK")); client.connect(); }
HttpClient httpclient = new DefaultHttpClient(); HttpResponse response; 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 getStatusCode() { return response == null || response.getStatusLine() == null ? 0 : response.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(); } }
boolean result = false; HttpClient hc = new DefaultHttpClient(); String message; HttpPost p = new HttpPost(url); JSONObject object = new JSONObject(); try { object.put("updates", updates); object.put("mobile", mobile); object.put("last_name", lastname); object.put("first_name", firstname); object.put("email", email); } catch (Exception ex) { } try { message = object.toString(); p.setEntity(new StringEntity(message, "UTF8")); p.setHeader("Content-type", "application/json"); HttpResponse resp = hc.execute(p); if (resp != null) { if (resp.getStatusLine().getStatusCode() == 204) result = true; } Log.d("Status line", "" + resp.getStatusLine().getStatusCode()); } catch (Exception e) { e.printStackTrace(); } return result;
HttpResponse sendHttpRequest(HttpUriRequest req, HttpClient httpClient) throws ClientProtocolException, IOException { LOG.debug("Sending request {}", req); HttpResponse response = httpClient.execute(req); if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK || null == response.getEntity()) { if (response instanceof CloseableHttpResponse) { ((CloseableHttpResponse)response).close(); } throw new IOException("HTTP Request " + req + " returned unexpected response " + response); } return response; }
@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); }