Java 设置SSL连接中的套接字超时
Hi想知道是否有办法设置套接字超时,以及在连接得到握手之前的重试次数。我在非常糟糕的连接模式下测试了我的应用程序,并将我的请求的重试策略设置为10秒,但SSL握手似乎是个问题,因为它的默认超时设置为60秒,并且由于尝试次数或超时,只有在套接字失败时才会触发截取超时 以下是我的方法:Java 设置SSL连接中的套接字超时,java,android,sockets,ssl,android-volley,Java,Android,Sockets,Ssl,Android Volley,Hi想知道是否有办法设置套接字超时,以及在连接得到握手之前的重试次数。我在非常糟糕的连接模式下测试了我的应用程序,并将我的请求的重试策略设置为10秒,但SSL握手似乎是个问题,因为它的默认超时设置为60秒,并且由于尝试次数或超时,只有在套接字失败时才会触发截取超时 以下是我的方法: private SSLSocketFactory newSslSocketFactory() { try { // Get an instance of the Bouncy
private SSLSocketFactory newSslSocketFactory() {
try {
// Get an instance of the Bouncy Castle KeyStore format
KeyStore trusted = KeyStore.getInstance("BKS");
// Get the raw resource, which contains the keystore with
// your trusted certificates (root and any intermediate certs)
InputStream in = getApplicationContext().getResources().openRawResource(R.raw.muip);
try {
// Initialize the keystore with the provided trusted certificates
// Provide the password of the keystore
trusted.load(in, KEYSTORE_PASSWORD.toCharArray());
} finally {
in.close();
}
String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
tmf.init(trusted);
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, tmf.getTrustManagers(), null);
SSLSocketFactory sf = new NoSSLv3Factory(context.getSocketFactory());
HttpsURLConnection.setDefaultSSLSocketFactory(sf);
return sf;
} catch (Throwable e) {
Log.e(TAG, "newSslSocketFactory: "+ e.getMessage(),e );
throw new AssertionError(e);
}
}
我正在使用截击来提出我的请求,我实现这一点的方法是:
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
Network network = new BasicNetwork(new HurlStack(null, newSslSocketFactory()));
Cache cache = new DiskBasedCache(getCacheDir(), 1024 * 1024); // 1MB cap
mRequestQueue = new RequestQueue(cache, network);
mRequestQueue.start();
}
return mRequestQueue;
}
我还尝试如下设置超时:
SSLSocketFactory sf = new NoSSLv3Factory(SSLCertificateSocketFactory.getDefault(10000, new SSLSessionCache(this)));
在
mRequestQueue.start()之后,可以通过以下方式设置超时:
int socketTimeout = 30000; // 30 seconds - change to what you want
RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
stringRequest.setRetryPolicy(policy);
queue.add(stringRequest);
只要读一下问答,你就会知道截击超时取决于套接字超时。请仔细阅读描述。我说,当他试图握手时,它不是在截击端,而是在SSL连接端。如果你仔细阅读了描述,你会注意到我提到的是,我已经在使用一个设置为10秒的重试策略,但是它只有在套接字握手失败时才会被触发