java - 如何解决javax.net.ssl.SSLHandshakeException : Connection closed by peer

标签 java android ssl ssl-certificate tls1.2

我正在尝试使用 volley 发送 http 请求但收到错误

javax.net.ssl.SSLHandshakeException: Connection closed by peer

我四处搜索并尝试了一些建议,但没有一个能解决我的问题。请帮忙

下面是我正在使用的代码

public class volley {

    static RequestQueue queue=null;
    public static String TAG = "MY_VOLLEY";
    volley(final String URL,
           final String Text
           final Context context
    ){
        helper.log(TAG,"Started Loading");
        // Instantiate the RequestQueue.
        if(queue==null) {
           queue = Volley.newRequestQueue(context);
        }
        // Request a string response from the provided URL.
        StringRequest stringRequest = new StringRequest(
                Request.Method.POST, URL, new Response.Listener<String>()
        {
            @Override
            public void onResponse(String response) {

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

                }
                return;
            }
        }
    ){
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                try {
                    params.put("message",Text);
                    params.put("from",From);
                } catch (Exception e) {
                    helper.log(TAG, "Error, hash map failed");
                }
                return params;
            }
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                params.put("Content-Type", "application/x-www-form-urlencoded");
                return params;
            }
    };
        // Add the request to the RequestQueue.
        queue.add(stringRequest);
        return;
    }
}

最佳答案

就调试 JSSE 而言,这是一种痛苦......

握手很可能失败,而 JSSE 大多数时候只向您显示最后的问题,即服务器正在关闭连接。服务器通常会在 TLS 警报消息中告诉您问题,但不会显示在异常消息中。

如果您安装了 Wireshark 或类似软件,最简单的方法是启动 session 并查看 TLS 记录(Wireshark 可以很好地显示它们,因此很容易识别包含错误代码和含义的错误数据包).

如果您还没有该工具,您可以激活 SSL 调试并查看 STDOUT 上的消息。请注意,如果您不熟悉 TLS 并且 - 因为它是一个全局设置 - 你应该确保你的测试在没有建立其他 TLS 连接时发生,否则它们会有点难以阅读,否则你会得到混合输出。要启用 TLS 握手调试,请设置系统属性 -Djavax.net.debug=ssl:handshake

关于java - 如何解决javax.net.ssl.SSLHandshakeException : Connection closed by peer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56705519/

相关文章:

Java类实例化

java - 如何通过网络发送更少的比特数

android - 使用 hash_map 时,我在 android NDK 上遇到编译错误 _GLIBCXX_PERMIT_BACKWARD_HASH

android - 无法解决错误 - 查看需要 API 级别 14(当前最小值为 8): <GridLayout>

java - Oracle JDBC 瘦驱动程序 SSL

java - 如何修复 java.security.InvalidAlgorithmParameterException : the trustAnchors parameter must be non-empty when connecting to elasticsearch

Android RadioButton显示

Java 服务器,TLSv1.1 快,TLSv1.2 极慢(90MByte/sec 对比 4MByte/sec)

php - simplexml_load_file() : Failed to enable crypto

java - JXTable:如何查询列模型上的状态?