简体   繁体   English

HttpURLConnection抛出java.net.SocketTimeoutException:在Android 4.1.1中SSL握手超时

[英]HttpURLConnection throws java.net.SocketTimeoutException: SSL handshake timed out in Android 4.1.1

My code works fine when it is run in Android 5.0 and up. 在Android 5.0及更高版本中运行时,我的代码运行正常。 But in Android 4.1.1 it throws java.net.SocketTimeoutException: SSL handshake timed out. 但是在Android 4.1.1中它会抛出java.net.SocketTimeoutException:SSL握手超时。

    URL url;
    HttpURLConnection connection = null; 
    String charset = "UTF-8";

    String accessToken = "";

    try {

        ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
        postParameters.add(new BasicNameValuePair("client_id", CLIENT_ID));
        postParameters.add(new BasicNameValuePair("client_secret", CLIENT_SECRET));
        postParameters.add(new BasicNameValuePair("grant_type", CLIENT_CREDENTIALS_GRANT_TYPE));

        //Create connection
        url = new URL(ACCESS_TOKEN_URL);
        connection = (HttpURLConnection)url.openConnection();
        connection.setReadTimeout( 10000 /*milliseconds*/ );
        connection.setConnectTimeout( 15000 /* milliseconds */ );
        connection.setInstanceFollowRedirects(false);
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + charset);
        connection.setUseCaches(false);
        connection.setDoInput(true);
        connection.setDoOutput(true);

        //Send request
        DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
        wr.write(getQuery(postParameters).getBytes(charset));
        wr.flush();
        wr.close();

        int responseCode = connection.getResponseCode();
        InputStream is = null;

        if(responseCode==HttpStatus.SC_OK) {
            is = connection.getInputStream();
        }
        else{
            is = connection.getErrorStream();
        }

        Log.d(TAG, "responseCode: "+responseCode);

        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
        String line = null;

        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }

        is.close();
        reader.close();

        String jsonResult = sb.toString();
        JSONObject jsonObject = new JSONObject(jsonResult);

        if(responseCode==HttpStatus.SC_OK){
            accessToken = jsonObject.getString("access_token");
            SettingsPreference.setAccessToken(accessToken);
            Log.d(TAG, "access token: "+accessToken);
        }
        else {
            accessToken = null;
        }

    } catch (Exception e) {
        e.printStackTrace();
    } finally {

        if(connection != null) {
          connection.disconnect(); 
        }
    }

I already done a lot of research and it is quite frustrating. 我已经做了很多研究,这非常令人沮丧。 What did I go wrong? 我出了什么问题? Is this a server issue? 这是服务器问题吗? Your help is greatly appreciated. 非常感谢您的帮助。

Use TrustManager on lower sdk levels? 在较低的sdk级别使用TrustManager?

There is a chance that the trusted certifications are not up-to-date on your older device. 旧设备上的可信认证可能不是最新的。 You could try to implement and use SimpleTrust if the device is under a certain sdk level. 如果设备处于某个sdk级别,您可以尝试实现并使用SimpleTrust

A simple library for trusting self signed or not properly signed domains. 用于信任自签名域或未正确签名域的简单库。 But in your case I think it can be handy to just make an exception for your domain and don't check certifications under a certain sdk level. 但在你的情况下,我认为只为你的域做一个例外并且不检查某个sdk级别的认证是很方便的。

This repo is using TrustManager to make exceptions for certain certifications/domains. 此repo使用TrustManager为某些认证/域制作例外。 If you'll use it make sure you use it properly cause Google Play might wont accept your application update / publish. 如果您使用它,请确保正确使用它,因为Google Play可能不会接受您的应用程序更新/发布。

See my answer on this other question. 看到我的回答这个其他问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 HttpURLConnection错误:java.net.SocketTimeoutException:连接超时 - HttpURLConnection error: java.net.SocketTimeoutException: Connection timed out CloseableHttpClient 抛出 java.net.SocketTimeoutException:读取超时 - CloseableHttpClient throws java.net.SocketTimeoutException: Read timed out 获取 java.net.SocketTimeoutException:连接在 android 中超时 - Getting java.net.SocketTimeoutException: Connection timed out in android Android java.net.SocketTimeoutException:连接超时 - Android java.net.SocketTimeoutException: Connection timed out java.net.SocketTimeoutException:读取超时 - java.net.SocketTimeoutException: Read timed out java.net.SocketTimeoutException:读取超时 - java.net.SocketTimeoutException: Read timed out java.net.SocketTimeoutException:连接超时 - java.net.SocketTimeoutException: Connect timed out Eclipse中的Java Jsoup程序抛出java.net.SocketTimeoutException:connect timed out - Java Jsoup program in Eclipse throws java.net.SocketTimeoutException: connect timed out 多线程服务器随机抛出java.net.SocketTimeoutException:读取超时 - Multithreaded server randomly throws java.net.SocketTimeoutException: Read timed out Java 套接字异常:java.net.SocketTimeoutException:接受超时 - Java Socket Exception : java.net.SocketTimeoutException: Accept timed out
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM