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();
}
}
}
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.
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 :)
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.
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With