public static String executeHTTPGetRequest(String uri, String acceptContentType) throws ClientProtocolException, IOException { HttpClient httpClient = new DefaultHttpClient(); HttpGet request = new HttpGet(uri); if(acceptContentType != null && !acceptContentType.isEmpty()) { request.setHeader(HTTP_HEADERS.Accept.name(), acceptContentType); } HttpResponse response = httpClient.execute(request); // Parse the response and store it in a String HttpEntity entity = response.getEntity(); StringBuilder responseString = new StringBuilder(); if (entity != null) { BufferedReader buf = new BufferedReader(new InputStreamReader(entity.getContent())); String line = buf.readLine(); while(line != null) { responseString.append(line); responseString.append('\n'); line = buf.readLine(); } } return responseString.toString(); }
private static String invokeHTTPRequest(HttpPost httpPost, String contentType, String acceptContentType) throws ClientProtocolException, IOException { HttpClient httpClient = new DefaultHttpClient(); if (acceptContentType != null && !acceptContentType.isEmpty()) { httpPost.setHeader(HTTP_HEADERS.Accept.name(), acceptContentType); } if (contentType != null && !contentType.isEmpty()) { httpPost.setHeader("Content-Type", contentType); } // Execute the request HttpResponse response = httpClient.execute(httpPost); // Parse the response and store it in a String HttpEntity entity = response.getEntity(); StringBuilder responseString = new StringBuilder(); if (entity != null) { BufferedReader buf = new BufferedReader(new InputStreamReader(entity.getContent(),"UTF-8")); String line = buf.readLine(); while(line != null) { responseString.append(line); responseString.append('\n'); line = buf.readLine(); } } return responseString.toString(); }
request.setHeader(HTTP_HEADERS.Accept.name(), "application/json; text/xml");