SSLProtocolException:读取错误:ssl=0x9af236c0:ssl库中出现故障,通常是android emulator中的协议错误
我使用以下方法读取流SSLProtocolException:读取错误:ssl=0x9af236c0:ssl库中出现故障,通常是android emulator中的协议错误,android,emulation,Android,Emulation,我使用以下方法读取流 public static String readStream(InputStream inputStream) { try { ByteArrayOutputStream bo = new ByteArrayOutputStream(); int i = inputStream.read(); while (i != -1) { bo.write(i); i = inp
public static String readStream(InputStream inputStream) {
try {
ByteArrayOutputStream bo = new ByteArrayOutputStream();
int i = inputStream.read();
while (i != -1) {
bo.write(i);
i = inputStream.read();
}
return bo.toString();
} catch (IOException e) {
e.printStackTrace();
return "";
}
}
我在这一行遇到以下异常
int i = inputStream.read();
例外情况:
javax.net.ssl.SSLProtocolException: Read error: ssl=0x9af236c0: Failure in SSL library, usually a protocol error
2019-03-15 16:25:25.978 5367-5389/com.healics.myhealics W/System.err: error:1e000065:Cipher functions:OPENSSL_internal:BAD_DECRYPT (external/boringssl/src/crypto/cipher/e_aes.c:1143 0x989b8e9f:0x00000000)
2019-03-15 16:25:25.978 5367-5389/com.healics.myhealics W/System.err: error:1000008b:SSL routines:OPENSSL_internal:DECRYPTION_FAILED_OR_BAD_RECORD_MAC (external/boringssl/src/ssl/tls_record.c:277 0x989b8e9f:0x00000000)
2019-03-15 16:25:25.979 5367-5389/com.healics.myhealics W/System.err: at com.android.org.conscrypt.NativeCrypto.SSL_read(Native Method)
2019-03-15 16:25:25.980 5367-5389/com.healics.myhealics W/System.err: at com.android.org.conscrypt.OpenSSLSocketImpl$SSLInputStream.read(OpenSSLSocketImpl.java:741)
2019-03-15 16:25:25.980 5367-5389/com.healics.myhealics W/System.err: at com.android.okhttp.okio.Okio$2.read(Okio.java:136)
2019-03-15 16:25:25.980 5367-5389/com.healics.myhealics W/System.err: at com.android.okhttp.okio.AsyncTimeout$2.read(AsyncTimeout.java:211)
2019-03-15 16:25:25.980 5367-5389/com.healics.myhealics W/System.err: at com.android.okhttp.okio.RealBufferedSource.read(RealBufferedSource.java:50)
2019-03-15 16:25:25.980 5367-5389/com.healics.myhealics W/System.err: at com.android.okhttp.internal.http.Http1xStream$FixedLengthSource.read(Http1xStream.java:393)
2019-03-15 16:25:25.980 5367-5389/com.healics.myhealics W/System.err: at com.android.okhttp.okio.RealBufferedSource$1.read(RealBufferedSource.java:371)
2019-03-15 16:25:25.981 5367-5389/com.healics.myhealics W/System.err: at java.io.BufferedInputStream.fill(BufferedInputStream.java:248)
2019-03-15 16:25:25.982 5367-5389/com.healics.myhealics W/System.err: at java.io.BufferedInputStream.read(BufferedInputStream.java:267)
2019-03-15 16:25:25.983 5367-5389/com.healics.myhealics W/System.err: at com.interrahealth.i3user.util.StreamReader.readStream(StreamReader.java:18)
现在,当我在真正的android设备上运行代码时,这只会在模拟器上发生,而不会发生。这是一个间歇性问题,有时并非每次都会出现
添加代码
我没有添加任何设置SSL/TLS设置的代码,下面是我的代码
private static String mGetResponseFromNetworkRequest(String userPass, String apiEndpoint, String
outputStreamBytes, String requestMethod, String contentType) {
long startTime = System.currentTimeMillis();
String response = "";
Logger.d("apiEndpoint: " + apiEndpoint);
try {
HttpURLConnection httpURLConnection = getHttpUrlConnectionWithDigestAuth(apiEndpoint, requestMethod);
httpURLConnection.setRequestMethod(requestMethod);
httpURLConnection.setRequestProperty("Content-Type", contentType);
httpURLConnection.setRequestProperty("Content-Language", "en-US");
httpURLConnection.setRequestProperty("charset", "utf-8");
httpURLConnection.addRequestProperty("User-Agent", "XYZ");
httpURLConnection.addRequestProperty("Full-App-Version", BuildConfig.VERSION_NAME);
httpURLConnection.addRequestProperty("Platform", "Android");
httpURLConnection.setUseCaches(false);
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
DataOutputStream dataOutputStream = new DataOutputStream(httpURLConnection.getOutputStream());
dataOutputStream.writeBytes(outputStreamBytes);
dataOutputStream.flush();
dataOutputStream.close();
Logger.d("dataOutputStream: " + outputStreamBytes);
Logger.d("Response Code: " + httpURLConnection.getResponseCode());
Logger.d("httpURLConnection.getHeaderFields(): " + httpURLConnection.getHeaderFields().toString());
Logger.d("httpURLConnection: " + httpURLConnection.toString());
// WAS THROWING ERRORS
if (httpURLConnection.getResponseCode() == 500) {
return "500";
} else {
InputStream inputStream = new BufferedInputStream(httpURLConnection.getInputStream());
response = StreamReader.readStream(inputStream);
httpURLConnection.disconnect();
}
Logger.d("Result: " + response);
} catch (MalformedInputException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
Log.d("mGetResponseFromNetworkRequest", "Connection Time = " + (System.currentTimeMillis() - startTime));
return response;
}
您需要在HttpsURLConnection上设置setSSLSocketFactory,如下所示:
httpURLConnection.setSSLSocketFactory(new MyFactory());
在Android API中,16级+TLS 1.1和1.2在默认情况下未启用,因此需要启用。上面我们使用了一个名为MyFactory的类,您将在其中设置SSLContext。这里有一个链接,显示您需要执行的操作您能否提供设置SSL/TLS设置的代码我没有添加任何设置SSL/TLS设置的代码下面是我的代码