// Instantiate the custom HttpClient DefaultHttpClient client = new MyHttpClient(getApplicationContext()); HttpGet get = new HttpGet("https://www.mydomain.ch/rest/contacts/23"); // Execute the GET call and obtain the response HttpResponse getResponse = client.execute(get); HttpEntity responseEntity = getResponse.getEntity();
public class WeatherTest { String weatherurl = "http://weather.yahooapis.com/forecastrss?w=35801&u=c"; HttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(weatherurl); public void myMethod() { try { ... } catch { ... } } }
HttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet("http://foo.com/bar"); httpGet.addHeader(BasicScheme.authenticate( new UsernamePasswordCredentials("user", "password"), "UTF-8", false)); HttpResponse httpResponse = httpClient.execute(httpGet); HttpEntity responseEntity = httpResponse.getEntity(); // read the stream returned by responseEntity.getContent()
HttpClient httpclient = new DefaultHttpClient(); // Create HTTP Client HttpGet httpget = new HttpGet("http://yoururl.com"); // Set the action you want to do HttpResponse response = httpclient.execute(httpget); // Executeit HttpEntity entity = response.getEntity(); InputStream is = entity.getContent(); // Create an InputStream with the response BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) // Read line by line sb.append(line + "\n"); String resString = sb.toString(); // Result is here is.close(); // Close the stream
public final T doAction() throws Exception { processBefore(); String requestStr = makeRequestString(treeMap); HttpGet httpGet = new HttpGet(protocol + "://" + endPoint + "?" + requestStr); HttpResponse response = executeHttpRequest(httpGet, endPoint); if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { String result = EntityUtils.toString(response.getEntity()); throw new RuntimeException("http request failed! " + result); } return processResult(response); } }
@Test public void shouldParameteriseUrisInNodeRepresentationWithoutHostHeaderUsingRequestUri() throws Exception { try ( CloseableHttpClient httpclient = HttpClientBuilder.create().build() ) { HttpGet httpget = new HttpGet( nodeUri ); httpget.setHeader( "Accept", "application/json" ); HttpResponse response = httpclient.execute( httpget ); HttpEntity entity = response.getEntity(); String entityBody = IOUtils.toString( entity.getContent(), StandardCharsets.UTF_8 ); assertThat( entityBody, containsString( nodeUri.toString() ) ); } }
HttpGet get = new HttpGet("http://192.168.1.36/"); get.setHeader("Content-Type", "text/plain; charset=utf-8"); get.setHeader("Expect", "100-continue"); HttpResponse resp = null; try { HttpClient httpClient = new DefaultHttpClient(); resp = httpClient.execute(get); } catch (ClientProtocolException e) { Log.e(getClass().getSimpleName(), "HTTP protocol error", e); } catch (IOException e) { Log.e(getClass().getSimpleName(), "Communication error", e); } if (resp != null) { // got a response, do something with it } else { // there was a problem }
@Override protected InputStream getStreamFromNetwork(String imageUri, Object extra) throws IOException { HttpGet httpRequest = new HttpGet(imageUri); HttpResponse response = httpClient.execute(httpRequest); HttpEntity entity = response.getEntity(); BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity); return bufHttpEntity.getContent(); } }
private int httpGet(String server, int port) throws IOException { HttpClient client = HttpClientBuilder.create().build(); String urlString = "http://" + server + ":" + port; log.info("Trying: GET " + urlString); HttpResponse execute = client.execute(new HttpGet(urlString)); return execute.getStatusLine().getStatusCode(); }
private String getHttpResponse(String url) { DefaultHttpClient client = new DefaultHttpClient(); String msg = null; int retry_times = 0; while (msg == null && retry_times < HTTP_RETRY) { retry_times++; HttpGet request = new HttpGet(url); try { request.addHeader("accept", "application/json"); HttpResponse response = client.execute(request); msg = EntityUtils.toString(response.getEntity()); } catch (Exception e) { logger.warn("Failed to fetch http response. Retry={}", retry_times, e); } finally { request.releaseConnection(); } } return msg; }
@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 ) ); } } }
/* init client */ HttpClient http = null; CookieStore httpCookieStore = new BasicCookieStore(); HttpClientBuilder builder = HttpClientBuilder.create().setDefaultCookieStore(httpCookieStore); http = builder.build(); /* do stuff */ HttpGet httpRequest = new HttpGet("http://stackoverflow.com/"); HttpResponse httpResponse = null; try {httpResponse = http.execute(httpRequest);} catch (Throwable error) {throw new RuntimeException(error);} /* check cookies */ httpCookieStore.getCookies();
HttpGet httppost = new HttpGet("http://www.urlOfThePageYouWantToRead.nl/text.txt"); HttpResponse response = httpclient.execute(httppost); HttpEntity ht = response.getEntity(); BufferedHttpEntity buf = new BufferedHttpEntity(ht); InputStream is = buf.getContent(); BufferedReader r = new BufferedReader(new InputStreamReader(is)); StringBuilder total = new StringBuilder(); String line; while ((line = r.readLine()) != null) { total.append(line + "\n"); } TextView.setText(total);
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());
public String getKylinProperties() throws IOException { String url = baseUrl + "/admin/config"; HttpGet request = new HttpGet(url); HttpResponse response = null; try { response = client.execute(request); String msg = EntityUtils.toString(response.getEntity()); Map<String, String> map = JsonUtil.readValueAsMap(msg); msg = map.get("config"); if (response.getStatusLine().getStatusCode() != 200) throw new IOException(INVALID_RESPONSE + response.getStatusLine().getStatusCode() + " with cache wipe url " + url + "\n" + msg); return msg; } finally { cleanup(request, response); } }
@Test public void shouldParameteriseUrisInNodeRepresentationWithHostHeaderValue() throws Exception { try ( CloseableHttpClient httpclient = HttpClientBuilder.create().build() ) { HttpGet httpget = new HttpGet( nodeUri ); httpget.setHeader( "Accept", "application/json" ); httpget.setHeader( "Host", "dummy.neo4j.org" ); HttpResponse response = httpclient.execute( httpget ); HttpEntity entity = response.getEntity(); String entityBody = IOUtils.toString( entity.getContent(), StandardCharsets.UTF_8 ); assertThat( entityBody, containsString( "http://dummy.neo4j.org/db/data/node/" ) ); } }
HttpClient httpclient = new DefaultHttpClient(); HttpConnectionParams.setConnectionTimeout(httpclient.getParams(), 10000); //Timeout Limit HttpGet httpGet = new HttpGet("http://example.com/blob/getuploadurl"); response = httpclient.execute(httpGet);
StringBuilder requestUrl = new StringBuilder("your_url"); String querystring = URLEncodedUtils.format(params, "utf-8"); requestUrl.append("?"); requestUrl.append(querystring); HttpClient httpclient = new DefaultHttpClient(); HttpGet get = new HttpGet(requestUrl.toString());
@Test void running_compose_defined_container_is_accessible_on_configured_port() throws Exception { HttpClient client = HttpClientBuilder.create().build(); HttpResponse response = client.execute(new HttpGet("http://" + host + ":" + port)); assertEquals(200, response.getStatusLine().getStatusCode()); } }