SSL套接字无法解析为某种类型

时间:2017-04-08 09:30:30

标签: java

我有这个脚本,我想用来连接到Web服务。

package pdslipay;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.security.KeyStore;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;

import za.co.ipay.retail.system.bizswitch.IpayMessageWrapper;
import za.co.ipay.retail.system.bizswitch.IpayMessageWrapper2;

public class ConnectionManager{

    private static ConnectionManager connectionManagerInstance = null;
    static Socket socket = null;
    InputStream inputStream = null;
    public static SSLClientTest sslConnect = new SSLClientTest();

    private ConnectionManager() {

       }
    public static ConnectionManager getConnectionManagerInstance() {
          if(connectionManagerInstance == null) {
              connectionManagerInstance = new ConnectionManager();
          }
          return connectionManagerInstance;
       }

    /**
     * Waits on response from server
     *
     * @param socket Server socket
     */
     public String readServerResponse(Socket socket) {
            String results = null;
            try { 
                BufferedInputStream serverReader = new BufferedInputStream(socket.getInputStream());
                IpayMessageWrapper2 wrap = new IpayMessageWrapper2();
                StringBuilder build = new StringBuilder();
                byte[] all = wrap.unWrap(serverReader);
                for (int n = 0; n < all.length; n++) {
                    char c = (char) all[n];
                    build.append(c);
                    System.out.print(c);
                }
                results = build.toString();
                System.out.println("\n\nStringBuilder " + build.toString());
                //pay.responsePrepaid(build.toString());
                //pay.responsePostPay();
                return results;
            } catch (IOException ex) {
                System.out.println("Error: Unable to read server response\n\t" + ex);
            }
            return "ERROR";
        }

    public String connection(String build) {
        BufferedOutputStream buff = null;
        String results = "ERROR";

        try {

            char[] passw = "clientpw".toCharArray();
            KeyStore ks = KeyStore.getInstance("JKS", "SUN");
            ks.load(new FileInputStream ( "resource/bizswitch-keys" ), passw );

            KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
            kmf.init(ks, passw);

            TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
            tmf.init(ks);
            TrustManager[] tm = tmf.getTrustManagers();

            SSLContext sclx = SSLContext.getInstance("TLS");
            sclx.init( kmf.getKeyManagers(), tm, null);

            SSLSocketFactory factory = sclx.getSocketFactory();
            SSLSocket socket = (SSLSocket) factory.createSocket( "01.000.000.198", 9102 );
            socket.startHandshake();


            IpayMessageWrapper wrap = new IpayMessageWrapper();

            buff = new BufferedOutputStream(socket.getOutputStream());
            buff.write(wrap.wrap(build.getBytes()));
            buff.flush();

            System.out.println("Info: Message has been sent..." + build);
            // Wait for server response
            results = getConnectionManagerInstance().readServerResponse(socket);

            return results;
        } catch (IOException ex) {
            Logger.getLogger(ConnectionManager.class.getName()).log(Level.SEVERE, null, ex);

        } 
        return results;
    }


}

在这一行

SSLSocket socket = (SSLSocket) factory.createSocket( "01.000.000.198", 9102 );我有错误

  

SSLSocket无法解析为类型

如果我导入sslsocket

package pdslipay;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.security.KeyStore;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;

import za.co.ipay.retail.system.bizswitch.IpayMessageWrapper;
import za.co.ipay.retail.system.bizswitch.IpayMessageWrapper2;

我收到错误Unhandled exception type NoSuchProviderException

如何更正代码以便读取密钥库并建立安全的ssl连接?

1 个答案:

答案 0 :(得分:0)

第一个错误只是告诉您导入该类。

执行此操作后,编译器可以告诉您代码中的其他问题,在本例中是关于代码中未处理的异常。

您需要将异常添加到方法的throws列表中,或者捕获/处理它。非常类似于您的代码已经处理的IOException!