public String readResponse(HttpResponse response) {
    String output = "";

    HttpEntity entity = response.getEntity();

    try {
      trapException(response.getStatusLine().getStatusCode());
    } catch (CrowdFlowerException e1) {
      e1.printStackTrace();
    }

    InputStream instream;
    try {
      instream = entity.getContent();
      BufferedReader reader = new BufferedReader(new InputStreamReader(instream));

      // do something useful with the response
      output = output + reader.readLine();
      instream.close();
    } catch (IllegalStateException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }

    return output;
  }
Example #2
0
 public static byte[] readbinaryfile(InputStream stream) throws IOException {
   // Extract the stream into a bytebuffer
   ByteArrayOutputStream bytes = new ByteArrayOutputStream();
   byte[] tmp = new byte[1 << 16];
   for (; ; ) {
     int cnt;
     cnt = stream.read(tmp);
     if (cnt <= 0) break;
     bytes.write(tmp, 0, cnt);
   }
   return bytes.toByteArray();
 }
  public static void main(String[] args) throws MalformedURLException, IOException {
    URLConnection con =
        new URL("http://localhost:8080/RobotControlServer/rest/control/hello").openConnection();
    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
    String inputLine;
    while ((inputLine = in.readLine()) != null) {
      System.out.println(inputLine);
    }
    in.close();

    HttpClient client = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost("http://localhost:8080/RobotControlServer/rest/control/photo");
    byte[] bytes = {0x01b};
    httpPost.setEntity(new ByteArrayEntity(bytes));
    HttpResponse response = client.execute(httpPost);
    HttpEntity entity = response.getEntity();
    InputStream inStream = entity.getContent();
    // Apache IOUtils makes this pretty easy :)
    System.out.println(IOUtils.toString(inStream));
    inStream.close();
  }