我正在使用java创建桌面应用程序,并且此应用程序使用API。为了保证与API的通信,我获悉他们支持使用HTTPS。请指导我如何从java客户端设置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;
}
这段代码给了我的想法,“请求”将是我当前的URL或服务器,所以这将是我的我的身边将设置https连接。
Clockworksms API包装:
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();
}
}
}
你可能会从这里的一些想法[http://stackoverflow.com/ question/4722644/web-service-client-with-java-application-and-ssl]也可以看看[http://www.javaworld.com/article/2077600/learn-java/java-tip-96--请使用https-in-your-java-client-code.html] – Vishal
您需要向安全的https API发出请求。对? – John
@John是的,我想要在那里有一个安全的连接,我被告知,https是实现这一目标的方式 – magicianiam