javax.net.ssl.SSLException:android旧设备上的SSL握手中止 [英] javax.net.ssl.SSLException: SSL handshake aborted on android old devices
问题描述
我有一个适用于大多数设备的android应用程序 最近,一些黑客试图对我们的服务器进行DDOS攻击,迫使我们增加一些安全性和某些防火墙
I have android application that was working fine for most of devices Recently some hackers tried to make DDOS attack on our servers that force us to add some security and some firewalls
某些设备无法正常工作,并给我以下异常
not some devices are not working and give me the following exception
javax.net.ssl.SSLException: SSL handshake aborted: ssl=0x63eb8240: I/O error during system call, Connection reset by peer
任何人都可以告诉我现在是什么问题,如何解决?
can any one please tell me what is the problem now and how can I solve it ?
编辑
这是我的execute方法的代码
this is the code of my execute method
public static BaseResponse execute(Context context, BaseRequest request) {
mStartTime = System.nanoTime();
BaseResponse response = new BaseResponse();
DataOutputStream outputStream;
try {
URL url = new URL(request.getURL());
HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
urlConnection.setConnectTimeout(TIMEOUT_DURATION);
urlConnection.setReadTimeout(TIMEOUT_DURATION);
urlConnection.setRequestMethod(request.getRequestType().getValue());
urlConnection.addRequestProperty("Accept", "application/json");
urlConnection.addRequestProperty("Content-Type","application/json");
urlConnection.setRequestProperty("Accept-Charset", CHARACTER_SET);
urlConnection.addRequestProperty("Device-Id", PhoneUtils.getDeviceId(context));
urlConnection.addRequestProperty("Version-Number", PhoneUtils.getAppVersion(context));
TLSSocketFactory socketFactory = new TLSSocketFactory();
urlConnection.setSSLSocketFactory(socketFactory);
switch (request.getRequestType()) {
case POST:
case PUT:
urlConnection.setDoOutput(true);
if (request.getStringEntity() != null) {
outputStream = new DataOutputStream(urlConnection.getOutputStream());
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, CHARACTER_SET));
writer.write(request.getStringParam());
writer.close();
outputStream.flush();
outputStream.close();
}
break;
case GET:
urlConnection.setDoOutput(false);
break;
}
urlConnection.connect();
try {
if (urlConnection.getResponseCode() == STATUS_OK) {
InputStream inputStream = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder result = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
inputStream.close();
response.setResponse(convertStringToJSONObject(result.toString()));
} else {
response.setResponse(null);
}
} catch (Exception ex) {
response.setAppError(AppError.DATA_ERROR);
}
} catch (MalformedURLException e) {
e.printStackTrace();
response.setAppError(AppError.PARSING_ERROR);
} catch (IOException e) {
e.printStackTrace();
response.setAppError(AppError.DATA_ERROR);
} catch (Exception e) {
e.printStackTrace();
response.setAppError(AppError.DATA_ERROR);
}
return response;
}
推荐答案
在进行任何网络调用之前,请在您的代码中使用此功能
Use this in your code before making any network call
/**
* Initialize SSL
* @param mContext
*/
public static void initializeSSLContext(Context mContext){
try {
SSLContext.getInstance("TLSv1.2");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
try {
ProviderInstaller.installIfNeeded(mContext.getApplicationContext());
} catch (GooglePlayServicesRepairableException e) {
e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
}
我遇到了同样的问题,这段代码解决了我的问题.仅供参考:我正在使用翻新库进行网络通话
I had the same problem and this piece of code solved my problem. FYI: I was using retrofit library for making network calls
这篇关于javax.net.ssl.SSLException:android旧设备上的SSL握手中止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!