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 getRequest(String url, String queryString) { String responseText = ""; try { httpGet.setURI(new URI(url + "?" + queryString)); httpGet.addHeader("Accept", "application/json"); HttpResponse response = httpClient.execute(httpGet); responseText = EntityUtils.toString(response.getEntity()); } catch (ClientProtocolException e) { } catch (IOException e) { } catch (URISyntaxException e1) { } return responseText; }
public String postRequest(String url, List<NameValuePair> entity) { String responseText = ""; try { httpPost.setURI(new URI(url)); httpPost.addHeader("Accept", "application/json"); httpPost.setEntity(new UrlEncodedFormEntity(entity)); HttpResponse response = httpClient.execute(httpPost); responseText = EntityUtils.toString(response.getEntity()); } catch (ClientProtocolException e) { } catch (IOException e) { } catch (URISyntaxException e1) { } return responseText; }
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(); }
protected String postRallyXML(String apiUrl, String requestXML) throws Exception { String responseXML = ""; DefaultHttpClient httpClient = new DefaultHttpClient(); Base64 base64 = new Base64(); String encodeString = new String(base64.encode((rallyApiHttpUsername + ":" + rallyApiHttpPassword).getBytes())); HttpPost httpPost = new HttpPost(apiUrl); httpPost.addHeader("Authorization", "Basic " + encodeString); httpPost.setEntity(new StringEntity(requestXML)); HttpResponse response = httpClient.execute(httpPost); HttpEntity entity = response.getEntity(); responseXML = getEntityString(entity); return responseXML; }