Java SSLv3连接没有';行不通

Java SSLv3连接没有';行不通,java,ssl,https,Java,Ssl,Https,我在尝试连接到服务器时遇到问题。我已经做了很多例子,但没有得到一个连接。该网站是免费的,它有一个证书(根据Chrome,它是第3版) 我有一个基本的程序来做这件事,但它总是失败,有以下两个错误: Exception in thread "main" javax.net.ssl.SSLException: Received fatal alert: unexpected_message 或 这是我的代码: import java.net.URL; import java.io.*; import

我在尝试连接到服务器时遇到问题。我已经做了很多例子,但没有得到一个连接。该网站是免费的,它有一个证书(根据Chrome,它是第3版)

我有一个基本的程序来做这件事,但它总是失败,有以下两个错误:

Exception in thread "main" javax.net.ssl.SSLException: Received fatal alert: unexpected_message

这是我的代码:

import java.net.URL;
import java.io.*;
import javax.net.ssl.HttpsURLConnection;

public class Test
{
    public static void main(String[] args) {   
        System.setProperty("https.protocols", "SSLv3");
        System.setProperty("javax.net.debug", "all");

        String httpsURL = "https://sikuani.udea.edu.co:4443";
        URL myurl = new URL(httpsURL);
        HttpsURLConnection con = (HttpsURLConnection)myurl.openConnection();
        InputStream ins = con.getInputStream();
        InputStreamReader isr = new InputStreamReader(ins);
        BufferedReader in = new BufferedReader(isr);

        String inputLine;

        while ((inputLine = in.readLine()) != null) {   
            System.out.println(inputLine);
        }   

        in.close();
    }   
}
我在谷歌上搜索过,这里是java的foros,但我没有找到解决方案(一些foros说这是java错误,其他foros说使用SSLv3参数就足够了,其他foros说创建TrussStore就可以了)

我认为close解决方案是使用信任库和证书,但我不知道如何设置正确的参数来完成这项工作

谢谢你的帮助

编辑:

这是我使用System.setProperty(“javax.net.debug”、“ssl、握手”)的输出:

如果我去掉了将协议设置为SSLv3的行,我有以下内容:

main, WRITE: TLSv1.2 Handshake, length = 235
main, READ: TLSv1 Alert, length = 2
main, RECV TLSv1.2 ALERT: fatal, unexpected_message main, called closeSocket()
main, handling exception: javax.net.ssl.SSLException: Received fatal alert: unexpected_message
Exception in thread "main" javax.net.ssl.SSLException: Received fatal alert: unexpected_message – 

下面是您应该使用的代码:

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class Test {
    public static void main(String[] args) {   
        System.setProperty("https.protocols", "TLSv1,SSLv3,SSLv2Hello");
        System.setProperty("javax.net.debug", "all");

        try{
            String httpsURL = "https://sikuani.udea.edu.co:4443";
            URL myurl = new URL(httpsURL);
            HttpURLConnection con = (HttpURLConnection)myurl.openConnection();
            InputStream ins = con.getInputStream();
            InputStreamReader isr = new InputStreamReader(ins);
            BufferedReader in = new BufferedReader(isr);

            String inputLine;

            while ((inputLine = in.readLine()) != null) {   
                System.out.println(inputLine);
            }   

            in.close();
        }
        catch(Exception ex){

        }
    } 
}
保持调试处于打开状态,以便查看正在发生的情况。 我根据收到的评论修改了代码。没有nee来添加我添加的行。此外,您还可以使用
HttpURLConnection
而不是
HttpsURLConnection
,因为它扩展了它。添加“SSLv2Hello”修复了该问题


我认为这应该行得通——现在就试试。

去掉设置SSLv3协议的线路。你不需要它。X509版本3与SSLv3不同。

可能是服务器端的问题吗?如果您尝试连接到
https://www.google.com/
一切正常。但服务器工作正常,如果我在浏览器上尝试相同的URL,它工作正常。发布由
jqvax.net.debug=ssl打印的内容,握手“
。把它编辑成你的问题。嗨,阿尔文,我没有Eclipse,我正在控制台上工作,我已经添加了这两行代码,现在我有一个错误:
线程“main”java.lang.ClassCastException中的异常:com.sun.net.ssl.internal.www.protocol.https.HttpsURLConnectionOldImpl不能转换为javax.net.ssl.HttpsURLConnection
Alvin,我已经复制了你的代码,但仍然不起作用(它会给我同样的错误)。我正在使用jdk-1.8.0_20.jdk。我不知道这是否相关。谢谢你的帮助。我刚刚下载并安装了JDK1.8.0_20,代码可以使用它。您是否重新运行了“javac Test.java”和“java Test”?永远不要忽略异常。有一个未使用的导入。十年来,您不需要设置
java.protocol.handler.pkgs
,也不需要添加Sun SSL提供程序。你还没有解释为什么这是他必须使用的代码-1Hi Alvin,我做了javac和java,但错误是一样的,java不能做转换。Hi EJP,如果我去掉这行,我有以下内容:
main,WRITE:TLSv1.2握手,length=235 main,READ:TLSv1 Alert,length=2 main,RECV TLSv1.2 Alert:fatal,意外消息main,称为closeSocket()main,处理异常:javax.net.ssl.SSLException:收到致命警报:线程“main”javax.net.ssl.SSLException:收到致命警报:收到致命警报消息
。如果我删除它,我得到的警报是非法参数。如果我将它设置为TLSv1它是意外消息。如果我将它设置为SSLv3它是关闭通知。@EJP,这应该由
System.setProperty(“https.protocols”,“SSLv3,SSLv2Hello”)解决。我看到了类似的错误消息。@AlvinBunk好的,至少应该是
“TLSv1,SSLv3,SSLv2Hello”
。是的,这是我的问题,所以您认为这是服务器的问题吗?还是关于被忽略的密码?还是关于Java配置?谢谢你的帮助。
main, WRITE: TLSv1.2 Handshake, length = 235
main, READ: TLSv1 Alert, length = 2
main, RECV TLSv1.2 ALERT: fatal, unexpected_message main, called closeSocket()
main, handling exception: javax.net.ssl.SSLException: Received fatal alert: unexpected_message
Exception in thread "main" javax.net.ssl.SSLException: Received fatal alert: unexpected_message – 
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class Test {
    public static void main(String[] args) {   
        System.setProperty("https.protocols", "TLSv1,SSLv3,SSLv2Hello");
        System.setProperty("javax.net.debug", "all");

        try{
            String httpsURL = "https://sikuani.udea.edu.co:4443";
            URL myurl = new URL(httpsURL);
            HttpURLConnection con = (HttpURLConnection)myurl.openConnection();
            InputStream ins = con.getInputStream();
            InputStreamReader isr = new InputStreamReader(ins);
            BufferedReader in = new BufferedReader(isr);

            String inputLine;

            while ((inputLine = in.readLine()) != null) {   
                System.out.println(inputLine);
            }   

            in.close();
        }
        catch(Exception ex){

        }
    } 
}