Code Robo
Virtual Service
Comparator
Tester
Converter
Utility
Java Code Complience
Validator
Formatter
EncoderDecoder



Java Code To invoke REST API / Service with https
       Talk to EasyAssistant

This page descirbe how to invoke REST api from JAVA client. It provides sample Java client code to invoke REST API with http/HTTPS protocol. It can invoke REST Api/Serice with GET, POST, PUT and DELETE method. It shows basic java code to send an email connecting to GMail server. It can be copied and used any other java java project. It uses java mail api class .

Webservice consumer need to invoke the REST service/api very frequently. Normally REST provider provides the end point URL and a smaple request JSON and response JSON. URL uses HTTPS protocol. Consumer need to write code to access/invoke the REST service. We invoke REST API/SERVICE with GET< PUT, POST amd DELETE methods.
Sample code given below can be used to invoke REST API/SERVICES.
In general REST API/Service uses basic authentication model to client authentication. In basic authentication model, first we invoke a api/service to get the authentication token passing userid and password as authentication credential. In the actual API call we pass the token as Bearer for authentication and to get the response.
This example code uses basic authentication to support HTTPS url.
Java Code To Get Token and Invoke REST API/SERVIC in PUT, GET, POST AND DELETE method.:
package tr.iv;

import java.net.HttpURLConnection;
import java.net.URL;
import java.security.cert.X509Certificate;
import java.util.List;
import java.util.Properties;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import javax.xml.bind.DatatypeConverter;

public class InvokeRESTServiceForEasyCode {
	Properties property;

	public static String getToken(String url, String user, String password) throws Exception {

		String response = "";
		try {
			System.out.println("urlHttps=" + url);
			java.net.HttpURLConnection conn = getConnectionHttps(url);
			conn.setRequestMethod("POST");
			String credentials = user + ":" + password;
			byte[] message = "hello world".getBytes("UTF-8");
			String encoded = DatatypeConverter.printBase64Binary(message);

			String encoding3 = DatatypeConverter.printBase64Binary(credentials.getBytes());
			encoding3 = encoding3.replaceAll("\n", "");

			conn.setRequestProperty("Content-Type", "application/json");
			conn.setRequestProperty("Authorization", "Basic " + encoding3);

			conn.setDoOutput(true);
			String str = "" + "{ \"grant_type\": \"client_credentials\" }" + "";
			java.io.OutputStream out = conn.getOutputStream();
			java.io.Writer wr = new java.io.OutputStreamWriter(out);
			wr.write(str);
			wr.close();
			java.io.InputStream in = conn.getInputStream();
			java.io.Reader reader = new java.io.InputStreamReader(in);
			java.io.StringWriter sw = new java.io.StringWriter();
			char[] buf = new char[500];
			int bread = 0;

			while ((bread = reader.read(buf)) != -1) {
				sw.write(buf, 0, bread);
				response = response + new String(buf);
			}

			reader.close();

			int inx = response.indexOf("201");
			if (response.indexOf("201") > 0) {
			} else {
			}

		} catch (Exception ex) {
			ex.printStackTrace();
			System.out.println("ex=" + ex);
			ex.printStackTrace(System.out);
		}
		return response;
	}

	public static String invokeRestApi(String method, String url, String token, String jsonInput) throws Exception {
		String response = "";

		try {

			java.net.HttpURLConnection conn = getConnectionHttps(url);
			conn.setRequestMethod(method);
			conn.setRequestProperty("Content-Type", "application/json");
			conn.setRequestProperty("Authorization", "Bearer " + token);
			conn.setDoOutput(true);
			java.io.OutputStream out = conn.getOutputStream();
			java.io.Writer wr = new java.io.OutputStreamWriter(out);
			System.out.println("----3Setting input xml3--");
			if (jsonInput != null) {
				wr.write(jsonInput);
			}

			wr.close();
			System.out.println("----444--");
			java.io.InputStream in = conn.getInputStream();
			java.io.Reader reader = new java.io.InputStreamReader(in);
			java.io.StringWriter sw = new java.io.StringWriter();
			char[] buf = new char[500];
			int bread = 0;
			System.out.println("----About To Read Responset--");
			while ((bread = reader.read(buf)) != -1) {
				sw.write(buf, 0, bread);
				System.out.println("5->Read:" + buf);
				response = response + new String(buf);
			}
			System.out.println("----before closing reader--");
			reader.close();
			System.out.println("RESp**:=" + response);
			int inx = response.indexOf("201");
			// ---call--//
		} catch (Exception ex) {
			ex.printStackTrace();
			System.out.println("ex=" + ex);
			ex.printStackTrace(System.out);
		}
		return response;
	}

	public static String invokeRESTApiByGet(String url, String token, String jsonInput1) throws Exception {

		String response = "";
		url = url + jsonInput1;
		try {
			System.out.println("urlHttps=" + url);
			java.net.HttpURLConnection conn = getConnectionHttps(url);
			conn.setRequestMethod("GET");
			conn.setRequestProperty("Content-Type", "application/json");
			conn.setRequestProperty("Authorization", "Bearer " + token);
			conn.setDoOutput(true);
			int code = conn.getResponseCode();
			System.out.println("code=" + code);
			java.io.InputStream in = conn.getInputStream();
			java.io.Reader reader = new java.io.InputStreamReader(in);
			java.io.StringWriter sw = new java.io.StringWriter();
			int bread = 0;
			while ((bread = reader.read()) != -1) {
				char ch = (char) bread;
				response = response + ch;

			}

			reader.close();
			System.out.println("RESp**:=" + response);
			int inx = response.indexOf("201");

		} catch (Exception ex) {
			ex.printStackTrace();
			System.out.println("ex=" + ex);
			ex.printStackTrace(System.out);
		}
		return response;
	}

	public static String invokeRestApiByPost(String url, String token, String jsonInput, List storeResponse)
			throws Exception {

		String response = "";

		try {
			System.out.println("urlHttps=" + url);

			java.net.HttpURLConnection conn = getConnectionHttps(url);
			conn.setRequestMethod("POST");

			String credentials = token;

			conn.setRequestProperty("Content-Type", "application/json");
			conn.setRequestProperty("Authorization", "Bearer " + token);

			conn.setDoOutput(true);
			java.io.OutputStream out = conn.getOutputStream();
			java.io.Writer wr = new java.io.OutputStreamWriter(out);

			if (jsonInput != null) {
				wr.write(jsonInput);
			}

			wr.close();
			int code = conn.getResponseCode();

			java.io.InputStream in = conn.getInputStream();
			java.io.Reader reader = new java.io.InputStreamReader(in);
			int bread = 0;
			while ((bread = reader.read()) != -1) {
				char ch = (char) bread;
				response = response + ch;
			}

			reader.close();

			System.out.println("RESpcode**:=" + code);
			storeResponse.add(response);
			response = " {\"ResponseCode\": " + code + "}";

		} catch (Exception ex) {
			ex.printStackTrace();
			System.out.println("ex=" + ex);
			ex.printStackTrace(System.out);
			storeResponse.add("" + ex);
		}
		System.out.println("response:" + response);
		return response;
	}

	public static String invokeRestApiByPUT(String url, String token, String jsonInput) throws Exception {
		String response = "";
		try {
			System.out.println("urlHttps=" + url);
			java.net.HttpURLConnection conn = getConnectionHttps(url);
			conn.setRequestMethod("PUT");

			conn.setRequestProperty("Content-Type", "application/json");
			conn.setRequestProperty("Authorization", "Bearer " + token);
			conn.setDoOutput(true);
			java.io.OutputStream out = conn.getOutputStream();
			java.io.Writer wr = new java.io.OutputStreamWriter(out);

			if (jsonInput != null) {
				wr.write(jsonInput);
			}

			wr.close();
			int code = conn.getResponseCode();
			java.io.InputStream in = conn.getInputStream();
			java.io.Reader reader = new java.io.InputStreamReader(in);
			java.io.StringWriter sw = new java.io.StringWriter();
			char[] buf = new char[500];
			int bread = 0;

			while ((bread = reader.read(buf)) != -1) {
				sw.write(buf, 0, bread);
				response = response + new String(buf);
			}

			reader.close();
			int inx = response.indexOf("201");
			System.out.println("inx=" + inx + response);
			if (code == 202) {
				response = " {\"ResponseCode\": 202}";
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			ex.printStackTrace(System.out);
		}
		return response;
	}

	public static HttpURLConnection getConnectionHttps(String urlHttps) throws Exception {
		System.out.println("urlHttps=" + urlHttps);
		TrustManager[] trustCerts = new TrustManager[] { new X509TrustManager() {
			public java.security.cert.X509Certificate[] getAcceptedIssuers() {
				return null;
			}

			public void checkClientTrusted(X509Certificate[] certs, String authType) {
			}

			public void checkServerTrusted(X509Certificate[] certs, String authType) {
			}
		} };

		SSLContext sc = SSLContext.getInstance("SSL");
		sc.init(null, trustCerts, new java.security.SecureRandom());
		HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

		HostnameVerifier allHostsValid = new HostnameVerifier() {
			public boolean verify(String hostname, SSLSession session) {
				return true;
			}
		};
		HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
		URL url = new URL(urlHttps);
		HttpURLConnection con = (java.net.HttpURLConnection) url.openConnection();
		return con;
	}
}




Note: If service provider provides any security certificate, use it.
  1. Please first check whether you able to get the token or not by passing userid and password.
  2. Then use token to get API response.
  3. If it fails, please add some debug(logger/System.out) statement in the code and debug it.

User Comments:
anonymous(2022-04-16):
Thanks for providing the sample code. It helped me in our project to do a POC.You are the best!!!
    Reply;
easycodeforall(admim): (2022-04-21)
Thank you for using easycodeforall and posting the comment.
    Reply
anonymous: (2022-08-31)
    Reply


Post Your Comment:
Name :
Email ( Optional) :
Comments / Suggestion (* Required) It is required: :
: