protected String getRallyXML(String apiUrl) throws Exception { String responseXML = ""; DefaultHttpClient httpClient = new DefaultHttpClient(); Base64 base64 = new Base64(); String encodeString = new String(base64.encode((rallyApiHttpUsername + ":" + rallyApiHttpPassword).getBytes())); HttpGet httpGet = new HttpGet(apiUrl); httpGet.addHeader("Authorization", "Basic " + encodeString); HttpResponse response = httpClient.execute(httpGet); HttpEntity entity = response.getEntity(); if (entity != null) { InputStreamReader reader = new InputStreamReader(entity.getContent()); BufferedReader br = new BufferedReader(reader); StringBuilder sb = new StringBuilder(); String line = ""; while ((line = br.readLine()) != null) { sb.append(line); } responseXML = sb.toString(); } log.debug("responseXML=" + responseXML); return responseXML; }
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; }
public String getEntityString(HttpEntity entity) throws Exception { StringBuilder sb = new StringBuilder(); if (entity != null) { InputStreamReader reader = new InputStreamReader(entity.getContent()); BufferedReader br = new BufferedReader(reader); String line = ""; while ((line = br.readLine()) != null) { sb.append(line); } } return sb.toString(); }
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(); }