我正在使用Java创建一个桌面应用程序,该应用程序使用API。为了保护与API的通信安全,他们的支持告诉我要使用HTTPS。请指导我如何从Java客户端设置https连接。
API有这个功能,可以选择一个安全连接:
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;
}
这段代码让我知道"request"会成为我当前的URL或服务器,因此是我自己设置HTTPS连接。
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();
}
}
}