Android HttpsURLConnection在getInputStream()

时间:2015-11-18 20:17:54

标签: android https

我正在尝试在我的Android应用中对https://google.com执行https请求。应用程序具有Internet权限并在异步任务中运行代码。我想要的只是回应简单的请求。我相信所有证书(我知道中间人攻击,但现在不打扰我)。应用程序使用消息

在getInputStream()中保持崩溃
Fatal signal 4 (SIGILL), code 2, fault addr 0xb71e65ce in tid 2551 (AsyncTask #1)

这是我的代码

    @Override
    protected String doInBackground(String... params) {
        String response = null;

        try {
            TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return null;
                }

                public void checkClientTrusted(X509Certificate[] certs, String authType) {
                }

                public void checkServerTrusted(X509Certificate[] certs, String authType) {
                }
            }};

            final SSLContext sc = SSLContext.getInstance("TLS");
            sc.init(null, trustAllCerts, new java.security.SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

            HostnameVerifier allHostsValid = new HostnameVerifier() {
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }
            };

            HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);

            ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
            if (networkInfo != null && networkInfo.isConnected()) {
                Log.d("TEST", "CONNECTION OK");
            } else {
                Log.d("TEST", "No network connection available.");
            }

            URL url = new URL("https://google.com");
            HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
            InputStream in = con.getInputStream();  // crashes here
            final Reader reader = new InputStreamReader(in);
            final BufferedReader r = new BufferedReader(reader);
            StringBuilder total = new StringBuilder();
            String line;
            while ((line = r.readLine()) != null) {
                total.append(line);
            }
            response = total.toString();
            con.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return response;
    }

...和logcat

 I/art: Late-enabling -Xcheck:jni
 I/GMPM: App measurement is starting up
 E/GMPM: getGoogleAppId failed with status: 10
 E/GMPM: Uploading is not possible. App measurement disabled
 I/art: Background sticky concurrent mark sweep GC freed 4582(267KB) AllocSpace objects, 3(48KB) LOS objects, 10% free, 2MB/3MB, paused 5.265ms total 37.052ms
 D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
 D/: HostConnection::get() New Host Connection established 0xb42fff60, tid 2672
 D/TEST: CONNECTION OK
 D/Atlas: Validating map...
 W/art: Suspending all threads took: 17.025ms
 D/libEGL: loaded /system/lib/egl/libEGL_emulation.so
 D/libEGL: loaded /system/lib/egl/libGLESv1_CM_emulation.so
 D/libEGL: loaded /system/lib/egl/libGLESv2_emulation.so
 D/: HostConnection::get() New Host Connection established 0xa2852230, tid 2703
 I/OpenGLRenderer: Initialized EGL, version 1.4
 D/OpenGLRenderer: Enabling debug mode 0
 W/EGL_emulation: eglSurfaceAttrib not implemented
 W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xb427ad60, error=EGL_SUCCESS
 W/ViewRootImpl: Dropping event due to no window focus: KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_ALT_RIGHT, scanCode=100, metaState=META_ALT_ON|META_ALT_RIGHT_ON, flags=0x8, repeatCount=15086, eventTime=1157039, downTime=389613, deviceId=1, source=0x301 }
 W/ViewRootImpl: Dropping event due to no window focus: KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_ALT_RIGHT, scanCode=100, metaState=META_ALT_ON|META_ALT_RIGHT_ON, flags=0x8, repeatCount=15087, eventTime=1157818, downTime=389613, deviceId=1, source=0x301 }
 W/ViewRootImpl: Dropping event due to no window focus: KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_ALT_RIGHT, scanCode=100, metaState=META_ALT_ON|META_ALT_RIGHT_ON, flags=0x8, repeatCount=15088, eventTime=1158024, downTime=389613, deviceId=1, source=0x301 }
 A/libc: Fatal signal 4 (SIGILL), code 2, fault addr 0xb71e65ce in tid 2702 (AsyncTask #1)

你能告诉我这是什么问题吗?

0 个答案:

没有答案