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; }
private String sendBasic(HttpRequestBase request) throws HttpException { try { HttpResponse response = httpClient.execute(request); HttpEntity entity = response.getEntity(); String body = ""; if (entity != null) { body = EntityUtils.toString(entity, UTF_8); if (entity.getContentType() == null) { body = new String(body.getBytes(ISO_8859_1), UTF_8); } } int code = response.getStatusLine().getStatusCode(); if (code < 200 || code >= 300) { throw new Exception(String.format(" code : '%s' , body : '%s'", code, body)); } return body; } catch (Exception ex) { throw new HttpException( "Fail to send " + request.getMethod() + " request to url " + request.getURI() + ", " + ex.getMessage(), ex); } finally { request.releaseConnection(); } }
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; }