简体   繁体   English

如何从Java应用程序设置Https连接

[英]how to setup an Https connection from Java Application

I am using java to create a desktop application, and this application uses an API. 我正在使用Java创建桌面应用程序,并且此应用程序使用API​​。 To secure the communication with the API, I was informed by their support to use HTTPS. 为了确保与API的通信安全,他们的支持使我得知使用HTTPS。 Please guide me on how to setup a https connection from a java client. 请指导我如何从Java客户端设置https连接。

The API has this function which states that it can choose a secure connection: 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;
}

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. 这段代码使我想到“请求”将是我当前的URL或服务器,因此将由我这边来设置https连接。

Clockworksms API Wrapper: 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();
      }
   }
}

If it's a secure REST API. 如果是安全的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 . 如果这只是您需要访问的HTTP资源,请查看Apache HTTPClient库及其教程

There are hundreds of resources out there, please try and then post specific questions. 那里有数百种资源,请尝试然后发布特定问题。

Actually we need to override existing default behavior and add allow all the as trusted servers, Below code snippet will help you: 实际上,我们需要覆盖现有的默认行为,并添加允许所有受信任的服务器,下面的代码段将帮助您:

    /* START of the fix*/
    TrustManager[] trustAllCerts = 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, trustAllCerts, new java.security.SecureRandom());
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

    // Create all-trusting host name verifier
    HostnameVerifier allHostsValid = new HostnameVerifier() {
        public boolean verify(String hostname, SSLSession session) { return true; }
    };
    // Install the all-trusting host verifier
    HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
    /* End of the fix*/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM