关于HttpsURLConnection请求的javax.net.ssl.SSLException

时间:2015-03-05 11:32:53

标签: java http java-ee https ftp4j

我试图在重命名ftp服务器目录中的文件后,在我的java应用程序中调用https url。

该文件已重命名,https url请求已成功,但我收到与我的cacerts文件中导入的证书相关的奇怪异常。

在ftp目录中重命名文件然后生成https url请求的代码如下:

...
FTPClient client = null;
try {
    client = Ftp4jUtility.ftpsConnect1(SERVER_MACHINE, PORT, SERVER_USERNAME, SERVER_PASSWORD);          
    client.rename(oldDir, newDir);
    renameStatus=true;
    String url = config.getExtractZipUrl()+stdDet.getStudyDetailsCaseId(tmpNewFile.trim());
    try {
            JavaHttpsUrlConnectionReader(url);
        } catch (Exception ex) {
            Logger.getLogger(TranferFileFtp4j.class.getName()).log(Level.ERROR, null, ex);
        } 

        client.logout();
        client.disconnect(true);

    } catch (IllegalStateException ex) {
        log.info("Failed to rename: " + oldDir);
        Logger.getLogger(TranferFileFtp4j.class.getName()).log(Level.ERROR, null, ex);

    } catch (IOException | FTPIllegalReplyException | FTPException ex) {
        log.info("Failed to rename: " + oldDir);
        Logger.getLogger(TranferFileFtp4j.class.getName()).log(Level.ERROR, null, ex);

    } finally {
        if (client.isConnected()) {
            try {
                try {
                    client.logout();
                    client.disconnect(true);
                } catch (IllegalStateException | FTPIllegalReplyException | FTPException ex) {
                    Logger.getLogger(TranferFileFtp4j.class.getName()).log(Level.ERROR, null, ex);
                }                    
            } catch (IOException ex) {
                Logger.getLogger(TranferFileFtp4j.class.getName()).log(Level.ERROR, null, ex);
            }
        }

    }
return renameStatus;

在我的日志中我会发现:

2015-03-05 13:09:04 ERROR TranferFileFtp4j:398 - 
javax.net.ssl.SSLException: Unsupported record version Unknown-53.49
at sun.security.ssl.InputRecord.checkRecordVersion(InputRecord.java:552)    at sun.security.ssl.InputRecord.checkRecordVersion(InputRecord.java:552)
at sun.security.ssl.InputRecord.readV3Record(InputRecord.java:565)
at sun.security.ssl.InputRecord.read(InputRecord.java:532)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:954)
at sun.security.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:911)
at sun.security.ssl.AppInputStream.read(AppInputStream.java:105)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
at sun.nio.cs.StreamDecoder.read0(StreamDecoder.java:127)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:112)
at java.io.InputStreamReader.read(InputStreamReader.java:168)
at it.sauronsoftware.ftp4j.NVTASCIIReader.readLine(NVTASCIIReader.java:105)
at it.sauronsoftware.ftp4j.FTPCommunicationChannel.read(FTPCommunicationChannel.java:142)
at it.sauronsoftware.ftp4j.FTPCommunicationChannel.readFTPReply(FTPCommunicationChannel.java:187)
at it.sauronsoftware.ftp4j.FTPClient.disconnect(FTPClient.java:1133)
at com.npap.network.TranferFileFtp4j.renameFileFtps1(TranferFileFtp4j.java:390)
at com.npap.utils.ProcessDicomFiles.sendZippFiles(ProcessDicomFiles.java:195)
at com.npap.scheduler.MainJob.execute(MainJob.java:84)
at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)
2015-03-05 13:09:04 ERROR TranferFileFtp4j:415 - 
java.lang.IllegalStateException: Client not authenticated
at it.sauronsoftware.ftp4j.FTPClient.logout(FTPClient.java:1406)
at com.npap.network.TranferFileFtp4j.renameFileFtps1(TranferFileFtp4j.java:412)
at com.npap.utils.ProcessDicomFiles.sendZippFiles(ProcessDicomFiles.java:195)
at com.npap.scheduler.MainJob.execute(MainJob.java:84)
at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)

这里也是制作https网址请求的代码(非常简单):

public void JavaHttpsUrlConnectionReader(String myUrl) throws Exception {
    URL url = null;
    BufferedReader reader = null;
    StringBuilder stringBuilder;
    try {
        url = new URL(myUrl);
        HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();

        // just want to do an HTTP GET here
        connection.setRequestMethod("GET");
        // give it 15 seconds to respond
        connection.setReadTimeout(15*1000);
        connection.connect();
        // read the output from the server
        reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        stringBuilder = new StringBuilder();

        String line = null;
        while ((line = reader.readLine()) != null)
        {
          stringBuilder.append(line + "\n");
        }
        //return stringBuilder.toString();
    }
    catch (Exception e)
    {
      e.printStackTrace();
      throw e;
    }
    finally
    {
        // close the reader; this can throw an exception too, so
        // wrap it in another try/catch block.
        if (reader != null)
        {
          try
          {
            reader.close();
          }
          catch (IOException ioe)
          {
            ioe.printStackTrace();
          }
        }
    }

}

我为什么收到:

javax.net.ssl.SSLException: Unsupported record version Unknown-53.49
at sun.security.ssl.InputRecord.checkRecordVersion(InputRecord.java:552)    at sun.security.ssl.InputRecord.checkRecordVersion(InputRecord.java:552)

如果你进一步检查:

java.lang.IllegalStateException: Client not authenticated
at it.sauronsoftware.ftp4j.FTPClient.logout(FTPClient.java:1406)

实际上我的代码中的第390行引用了日志是我想要的地方:

client.disconnect(真);

所以当我试图断开与ftp会话的连接时,我收到异常。任何人都知道为什么我会收到这个例外?

2 个答案:

答案 0 :(得分:0)

您使用的是哪个Java版本?也许你可以试试JCE

答案 1 :(得分:0)

最后似乎

client.logout();

之前:

client.disconnet(true);

导致异常:

java.lang.IllegalStateException: Client not authenticated
at it.sauronsoftware.ftp4j.FTPClient.logout(FTPClient.java:1406)

javax.net.ssl.SSLException: Unsupported record version Unknown-53.49
at sun.security.ssl.InputRecord.checkRecordVersion(InputRecord.java:552)        at sun.security.ssl.InputRecord.checkRecordVersion(InputRecord.java:552)

是由于https请求(HttpsURLConnection)没有足够的readTimeOut:

HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setReadTimeout(15*1000);
connection.connect();

15秒还不够。增加到60,它解决了......