@Override public InputStream getInputStream() throws IOException { return response == null || response.getEntity() == null ? null : response.getEntity().getContent(); }
HttpClient httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet(urltofetch); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); if (entity != null) { long len = entity.getContentLength(); InputStream inputStream = entity.getContent(); // write the file to whether you want it. }
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
protected InputStreamReader openStream( String encoding, HttpResponse httpResponse ) throws Exception { if ( !Utils.isEmpty( encoding ) ) { return new InputStreamReader( httpResponse.getEntity().getContent(), encoding ); } else { return new InputStreamReader( httpResponse.getEntity().getContent() ); } }
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); HttpClient httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet(uri); httpget.setHeader("User-Agent", userAgent); HttpResponse response = httpclient.execute(httpget, localContext); HttpEntity entity = response.getEntity(); if (entity != null && response.getStatusLine().getStatusCode() == 200) { InputStream instream = entity.getContent(); String videoInfo = getStringFromInputStream(encoding, instream); if (videoInfo != null && videoInfo.length() > 0) { HttpClient httpclient2 = new DefaultHttpClient(); HttpResponse response2 = httpclient2.execute(httpget2); HttpEntity entity2 = response2.getEntity(); if (entity2 != null && response2.getStatusLine().getStatusCode() == 200) { long length = entity2.getContentLength(); InputStream instream2 = entity2.getContent(); log.finer("Writing " + length + " bytes to " + outputfile); if (outputfile.exists()) { Reader reader = new BufferedReader(new InputStreamReader(instream, encoding)); int n; while ((n = reader.read(buffer)) != -1) {
private JsonObject executeRequest(CloseableHttpClient client, HttpPost request) throws IOException, MojoExecutionException { try { CloseableHttpResponse response = client.execute(request); int statusCode = response.getStatusLine().getStatusCode(); logger.debug("Response status %d", statusCode); if (statusCode != HttpStatus.SC_OK) { throw new MojoExecutionException("AWS authentication failure"); } HttpEntity entity = response.getEntity(); Reader jr = new InputStreamReader(entity.getContent(), StandardCharsets.UTF_8); return new Gson().fromJson(jr, JsonObject.class); } finally { client.close(); } }
HttpClient httpclient = HttpClients.createDefault(); HttpPost httppost = new HttpPost("http://www.a-domain.com/foo/"); // Request parameters and other properties. List<NameValuePair> params = new ArrayList<NameValuePair>(2); params.add(new BasicNameValuePair("param-1", "12345")); params.add(new BasicNameValuePair("param-2", "Hello!")); httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8")); //Execute and get the response. HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); if (entity != null) { InputStream instream = entity.getContent(); try { // do something useful } finally { instream.close(); } }
HttpHost proxy = new HttpHost("ip address",port number); DefaultHttpClient httpclient = new DefaultHttpClient(); httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy); HttpPost httpost = new HttpPost(url); List<NameValuePair> nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("param name", param)); httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.ISO_8859_1)); HttpResponse response = httpclient.execute(httpost); HttpEntity entity = response.getEntity(); System.out.println("Request Handled?: " + response.getStatusLine()); InputStream in = entity.getContent(); httpclient.getConnectionManager().shutdown();
private InputStream get(final URI uri, boolean withoutImageMime) throws UnsupportedOperationException, IOException { final HttpGet get = new HttpGet(uri); final HttpResponse respone = client.execute(get, context); final HttpEntity entity = respone.getEntity(); // normal jpeg return image/jpeg as opposite to mjpeg if (withoutImageMime) { final Header contentType = entity.getContentType(); if (contentType == null) { throw new WebcamException("Content Type header is missing"); } if (contentType.getValue().startsWith("image/")) { throw new WebcamException("Cannot read images in PUSH mode, change mode to PULL " + contentType); } } return entity.getContent(); }
private Plain asPlain(org.apache.http.HttpResponse response) throws IOException { assertThat(response.getStatusLine().getStatusCode(), is(200)); HttpEntity entity = response.getEntity(); MediaType mediaType = MediaType.parse(entity.getContentType().getValue()); assertThat(mediaType.type(), is("application")); assertThat(mediaType.subtype(), is("json")); return Jsons.toObject(entity.getContent(), Plain.class); }
@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 String getContent(HttpResponse response) throws IOException { StringBuffer result = new StringBuffer(); try (BufferedReader rd = new BufferedReader( new InputStreamReader(response.getEntity().getContent(), StandardCharsets.UTF_8))) { String line; while ((line = rd.readLine()) != null) { result.append(line); } } return result.toString(); }
private static String getStringContent(HttpResponse response) throws IOException { return CharStreams.toString(new InputStreamReader(response.getEntity().getContent(), UTF_8)); }
InputStream is = null; ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); nameValuePairs.add(new BasicNameValuePair("lastupdate", lastupdate)); try { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(connection); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); is = entity.getContent(); Log.d("HTTP", "HTTP: OK"); } catch (Exception e) { Log.e("HTTP", "Error in http connection " + e.toString()); }
try { HttpEntity entity = response.getEntity(); final InputStream in = entity.getContent(); final SAXParser parser = SAXParserFactory.newInstance().newSAXParser(); final XmlHandler handler = new XmlHandler(); Reader reader = new InputStreamReader(in, "UTF-8"); InputSource is = new InputSource(reader); is.setEncoding("UTF-8"); parser.parse(is, handler); //TODO: get the data from your handler } catch (final Exception e) { Log.e("ParseError", "Error parsing xml", e); }
public static InputStream getData(String url, String requestHeaderName, String requestHeaderValue) throws ClientProtocolException, IOException { HttpGet httpget = new HttpGet(url); if (requestHeaderValue != null) { httpget.addHeader(requestHeaderName, requestHeaderValue); } HttpClient httpclient = getClient(); HttpResponse response = httpclient.execute(httpget); if(response.getStatusLine().getStatusCode() != 200) return null; HttpEntity entity = response.getEntity(); if (entity == null) { return null; } return entity.getContent(); }
@Override public InputStream getInputStream() throws IOException { return response == null || response.getEntity() == null ? null : response.getEntity().getContent(); }
public CookieRequest makeRequest(){ httpGet.addHeader("Cookie", cookie); try{ HttpResponse httpResponse = new DefaultHttpClient().execute(httpGet); if(httpResponse.getStatusLine().getStatusCode() == 200){ response = inputStream2String(httpResponse.getEntity().getContent()); } else{ response = ""; } }catch(Exception e){} return this; }
@Override public CachedImage load(ClientDetailsEntity key) throws Exception { try { HttpResponse response = httpClient.execute(new HttpGet(key.getLogoUri())); HttpEntity entity = response.getEntity(); CachedImage image = new CachedImage(); image.setContentType(entity.getContentType().getValue()); image.setLength(entity.getContentLength()); image.setData(IOUtils.toByteArray(entity.getContent())); return image; } catch (IOException e) { throw new IllegalArgumentException("Unable to load client image."); } }
private void assertJson(final String url, final String content) throws IOException { HttpResponse response = helper.getResponse(url); HttpEntity entity = response.getEntity(); byte[] bytes = ByteStreams.toByteArray(entity.getContent()); assertThat(new String(bytes), is(content)); MediaType mediaType = MediaType.parse(entity.getContentType().getValue()); assertThat(mediaType.type(), is("application")); assertThat(mediaType.subtype(), is("json")); } }