@Override public InputStream getInputStream() throws IOException { return response == null || response.getEntity() == null ? null : response.getEntity().getContent(); }
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()); }
public void post() throws Exception{ HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost("http://www.baidu.com"); String xml = "<xml>xxxx</xml>"; HttpEntity entity = new ByteArrayEntity(xml.getBytes("UTF-8")); post.setEntity(entity); HttpResponse response = client.execute(post); String result = EntityUtils.toString(response.getEntity()); }
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() ); } }
URL url=new URL(urlToHit); HttpPost httppost = new HttpPost(url.toString()); HttpResponse response = LoginScreen.httpClient.execute(httppost); // Log.v("response code",""+response.getStatusLine().getStatusCode()); // Get hold of the response entity HttpEntity entity = response.getEntity(); InputStream instream = null; if (entity != null) { instream = entity.getContent(); } xr.parse(new InputSource(instream)); //SAX parsing
HttpEntity entity = MultipartEntityBuilder .create() .addTextBody("number", "5555555555") .addTextBody("clip", "rickroll") .addBinaryBody("upload_file", new File(filePath), ContentType.create("application/octet-stream"), "filename") .addTextBody("tos", "agree") .build(); HttpPost httpPost = new HttpPost("http://some-web-site"); httpPost.setEntity(entity); HttpResponse response = httpClient.execute(httpPost); HttpEntity result = response.getEntity();
private JSONObject readJSONFromUrl(String urlString, Map<String, String> headers) throws IOException, JSONException { HttpClient httpClient = openConnection(); HttpGet request = new HttpGet(urlString); for (Map.Entry<String, String> entry : headers.entrySet()) { request.addHeader(entry.getKey(), entry.getValue()); } HttpResponse response = httpClient.execute(request); InputStream content = response.getEntity().getContent(); return readAllIntoJSONObject(content); }
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); }
String encoding = Base64Encoder.encode ("test1:test1"); HttpPost httppost = new HttpPost("http://host:post/test/login"); httppost.setHeader("Authorization", "Basic " + encoding); System.out.println("executing request " + httppost.getRequestLine()); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity();
@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(); } }
HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet(url); HttpResponse response = client.execute(request); String html = ""; InputStream in = response.getEntity().getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); StringBuilder str = new StringBuilder(); String line = null; while((line = reader.readLine()) != null) { str.append(line); } in.close(); html = str.toString();
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(); } }
private static String getStringContent(HttpResponse response) throws IOException { return CharStreams.toString(new InputStreamReader(response.getEntity().getContent(), UTF_8)); }
@Override public InputStream getInputStream() throws IOException { return response == null || response.getEntity() == null ? null : response.getEntity().getContent(); }
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());
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")); } }
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams()); HttpPost httppost = new HttpPost(http://someJSONUrl/jsonWebService); // Depends on your web service httppost.setHeader("Content-type", "application/json"); InputStream inputStream = null; String result = null; try { HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); inputStream = entity.getContent(); // json is UTF-8 by default BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } result = sb.toString(); } catch (Exception e) { // Oops } finally { try{if(inputStream != null)inputStream.close();}catch(Exception squish){} }
HttpPost post = new HttpPost("https://yourdomain.com/yourskript.xyz"); List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); nameValuePairs.add(new BasicNameValuePair("postValue1", "my Value")); nameValuePairs.add(new BasicNameValuePair("postValue2", "2nd Value")); post.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpClient client = new DefaultHttpClient(); HttpResponse response = client.execute(post); HttpEntity entity = response.getEntity(); String responseText = EntityUtils.toString(entity);
@Override public void run() throws IOException { org.apache.http.HttpResponse httpResponse = helper.getResponse(remoteUrl("/dir/dir.response")); String value = httpResponse.getFirstHeader(HttpHeaders.CONTENT_TYPE).getValue(); assertThat(value, is("text/plain")); String content = CharStreams.toString(new InputStreamReader(httpResponse.getEntity().getContent())); assertThat(content, is("response from dir")); } });