Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to setup an Https connection from Java Application

Tags:

java

https

api

I am using java to create a desktop application, and this application uses an API. To secure the communication with the API, I was informed by their support to use HTTPS. Please guide me on how to setup a https connection from a java client.

The API has this function which states that it can choose a secure connection:

private String getUrlAddress(XmlRequest request) {
    // determine if this is a secure connection
    String url = this.ssl ? "https://" : "http://";

    // determine service endpoint based on type of class/request passed in
    if( request.getClass() == MessageRequest.class) {
        url += ClockWorkSmsService.SMS_URL;
    }
    else {
        url += ClockWorkSmsService.CREDIT_URL;
    }

    return url;
}

this code gives me the idea that "request" will be my current url or server, so it will be me on my side that will setup the https connection.

Clockworksms API Wrapper:

import com.clockworksms.*;

public class DemoSms
{
   public static void main(String[] args)
   {
      try
      {
         ClockWorkSmsService clockWorkSmsService = new ClockWorkSmsService("apikey");
         SMS sms = new SMS("441234567890", "Hello World");         
         ClockworkSmsResult result = clockWorkSmsService.send(sms);

         if(result.isSuccess())
         {
            System.out.println("Sent with ID: " + result.getId());
         }
         else
         {
            System.out.println("Error: " + result.getErrorMessage());
         }
      }
      catch (ClockworkException e)
      {
         e.printStackTrace();
      }
   }
}
like image 720
magicianiam Avatar asked Oct 05 '15 12:10

magicianiam


People also ask

Can you share source code for a Java HTTPS client application?

Java HTTPS client FAQ: Can you share some source code for a Java HTTPS client application? Sure, here’s the source code for an example Java HTTPS client program I just used to download the contents of an HTTPS (SSL) URL.

Do I need to host a HTTPS server for Java client?

Oct 5 '15 at 12:24 So in that case you need not host a https server, your java client will make a https connection request. – John Oct 5 '15 at 12:25 @Vishal thank you for that, i'll take a look into it :)

Is there an example Java program to download HTTPS (SSL) URLs?

Sure, here’s the source code for an example Java HTTPS client program I just used to download the contents of an HTTPS (SSL) URL. I actually found some of this in a newsgroup a while ago, but I can’t find the source today to give them credit, so my apologies for that.

How do I create an HTTPS connection from my applet?

In 4.0 and later versions of Netscape Navigator and Internet Explorer, HTTPS is enabled by default for their respective VMs. Therefore, if you want to create an HTTPS connection from within your applet code, simply specify HTTPS as your protocol when creating an instance of the URL class:


1 Answers

If it's a secure REST API. Have a look here

If it's just a HTTP resource that you need to access then have a look at Apache HTTPClient library and it's tutorials.

There are hundreds of resources out there, please try and then post specific questions.

like image 192
John Avatar answered Oct 26 '22 07:10

John