Java tutorial
/* Smart Cloud/City Engine backend (SCE). Copyright (C) 2015 DISIT Lab http://www.disit.org - University of Florence This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ package sce; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.nio.charset.Charset; import org.apache.commons.codec.binary.Base64; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.HttpClientBuilder; public class HTTPPostReq { HttpPost createConnectivity(String restUrl, String username, String password, int timeout) { HttpPost post = new HttpPost(restUrl); RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(timeout).setConnectTimeout(timeout) .setConnectionRequestTimeout(timeout).build(); post.setConfig(requestConfig); if (username != null && password != null) { String auth = new StringBuffer(username).append(":").append(password).toString(); byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(Charset.forName("US-ASCII"))); String authHeader = "Basic " + new String(encodedAuth); post.setHeader("AUTHORIZATION", authHeader); } post.setHeader("Content-Type", "application/json"); post.setHeader("Accept", "application/json"); post.setHeader("Accept-Enconding", "UTF-8"); post.setHeader("X-Stream", "true"); return post; } void executeReq(String jsonData, HttpPost httpPost) { try { executeHttpRequest(jsonData, httpPost); } catch (UnsupportedEncodingException e) { //System.out.println("error while encoding api url : " + e); } catch (IOException e) { //System.out.println("ioException occured while sending http request : " + e); } catch (Exception e) { //System.out.println("exception occured while sending http request : " + e); } finally { httpPost.releaseConnection(); } } void executeHttpRequest(String jsonData, HttpPost httpPost) throws UnsupportedEncodingException, IOException { HttpResponse response; String line; StringBuffer result = new StringBuffer(); httpPost.setEntity(new StringEntity(jsonData)); HttpClient client = HttpClientBuilder.create().build(); response = client.execute(httpPost); //System.out.println("Post parameters : " + jsonData); //System.out.println("Response Code : " + response.getStatusLine().getStatusCode()); BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); while ((line = reader.readLine()) != null) { result.append(line); } //System.out.println(result.toString()); } }