【问题标题】:How to solve javax.net.ssl.SSLHandshakeException: Connection closed by peer如何解决 javax.net.ssl.SSLHandshakeException: Connection closed by peer
【发布时间】:2019-11-04 10:15:55
【问题描述】:

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

javax.net.ssl.SSLHandshakeException:连接被对等方关闭

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

下面是正在使用的代码

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;
    }
}

【问题讨论】:

    标签: java android ssl ssl-certificate tls1.2


    【解决方案1】:

    在调试 JSSE 方面是一件痛苦的事......

    握手很可能失败,JSSE 大部分时间只向您显示最后一个问题,即服务器正在关闭连接。服务器通常会在 TLS 警报消息中告诉您问题,但不会出现在异常消息中。

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

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

    【讨论】:

    • 警报总是导致包含它的异常。对于在需要时不提供证书的客户来说,关闭可能是一毛钱,因为这不是警报条件。
    • @user207421 绝对不是这样。我希望是这样,因为如果是这样的话,我的生活会轻松很多。我必须经常支持这种类型的问题,并且经常使用 Wireshark 或上述系统属性。
    猜你喜欢
    • 2017-07-20
    • 1970-01-01
    • 2022-12-04
    • 2015-10-31
    • 2019-02-02
    • 1970-01-01
    • 2018-10-15
    • 2012-01-18
    • 1970-01-01
    相关资源
    最近更新 更多