java.io.IOException: Hostname was not verified

后端 未结 10 633
逝去的感伤
逝去的感伤 2020-11-28 06:27

I am trying to connect to a URL from a my Android app in Andorid Version 4.1.1, and I get the error indicated in the Title of my question, but when I tried to connect the sa

10条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-28 07:22

    it's possible that your problem is what your url's were resolved via "https". you must convert all string urls to "http" and it will work.

    EDIT:

    SchemeRegistry schemeRegistry = new SchemeRegistry ();
    
    schemeRegistry.register (new Scheme ("http",
        PlainSocketFactory.getSocketFactory (), 80));
    schemeRegistry.register (new Scheme ("https",
        new CustomSSLSocketFactory (), 443));
    
    ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager (
        params, schemeRegistry);
    
    return new DefaultHttpClient (cm, params);
    

    CustomSSLSocketFactory:

    public class CustomSSLSocketFactory extends org.apache.http.conn.ssl.SSLSocketFactory
    {
        private SSLSocketFactory FACTORY = HttpsURLConnection.getDefaultSSLSocketFactory ();
    
        public CustomSSLSocketFactory ()
        {
            super(null);
            try
            {
                SSLContext context = SSLContext.getInstance ("TLS");
                TrustManager[] tm = new TrustManager[] { new FullX509TrustManager () };
                context.init (null, tm, new SecureRandom ());
    
                FACTORY = context.getSocketFactory ();
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    
        public Socket createSocket() throws IOException
        {
            return FACTORY.createSocket();
        }
    
        // TODO: add other methods like createSocket() and getDefaultCipherSuites().
        // Hint: they all just make a call to member FACTORY 
    }
    

    FullX509TrustManager is a class that implements javax.net.ssl.X509TrustManager, yet none of the methods actually perform any work, get a sample [here][1].

    Good Luck!

提交回复
热议问题