Code Robo
Formatter
Comparator
Tester
Converter
Utility
Java Code Complience
Validator
EncoderDecoder
Virtual Service
Java Code To Connect Https URL (URLConnection)
       Talk to EasyAssistant

This page descirbe how to connect a https url and get the URLConnection object in Java with out having client security certificate We need to connect a https URL from java client program. This sample code helps you to connect a https and post/get data to the server. Just take this sample code to your eclipse, change the https url and execute it

Sample Java code to which can connect both http and https URL.
This example code uses show https url connection without security certificate.
Java Code To Connect Https URL :

package sg.ardpfnd.httpxs;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.security.cert.X509Certificate;

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 org.apache.log4j.Logger;

public class ConnectHttpsForEZL {

	public static void main(String[] args) throws Exception {

		String url = "https://www.easycodeforall.com/";
		if (args[0] == null)
			url = args[0];
		
		String response=invokeUrlByGet(url);
		System.out.println("response="+response);

	}

	public static HttpURLConnection getConnectionHttpOrHttps(String url) throws Exception {
		System.out.println("conn using url=" + url);
		String protocol = "http";
		if (url.toUpperCase().startsWith("HTTPS")) {
			protocol = "https";
		}

		if (protocol.equalsIgnoreCase("http")) {
			return getConnectionHttp(url);
		} else {
			return getConnectionHttps(url);
		}
	}

	public static HttpURLConnection getConnectionHttp(String urlhttp) throws IOException {
		URL url = new URL(urlhttp);
		java.net.HttpURLConnection conn = (java.net.HttpURLConnection) url.openConnection();
		return conn;
	}

	public static HttpURLConnection getConnectionHttps(String urlHttps) throws Exception {
		TrustManager[] certs = 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, certs, new java.security.SecureRandom());
		HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

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

	public static String invokeUrlByGet(String url) throws Exception {
		String response = "";
		try {
			java.net.HttpURLConnection conn = getConnectionHttpOrHttps(url);
			conn.setRequestMethod("GET");
			conn.setRequestProperty("Content-Type", "application/xml");
			conn.setDoOutput(true);
			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();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return response;
	}
}





Note: If service provider provides any security certificate, use it.
  1. It has the code to connect http URL
  2. It has code to connect HTTPS url.
  3. Get Method to connect and do the http request with GET method.


User Comments:
anonymous (2022-04-16) :
Very useful utility!!!
    Reply;
anonymous: (2022-04-21)
Thank you for using it!
    Reply

anonymous (2022-08-22) :
Very useful java code! thanks for making it public!
    Reply;


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