我有一个与网络服务通信的Android应用程序。在Web服务上添加SSL
之前,我们使用http
协议进行通信。一切都很顺利。
启用了Web服务的SSL,现在客户端无法与服务器通信。它没有连接。
// Method to create the socket
private Socket initSocket() throws Exception {
IO.Options op = new IO.Options();
try {
SSLContext sslContext = SSLContext.getInstance("TLS");
HostnameVerifier hostnameVerifier = new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
HostnameVerifier hv = HttpsURLConnection.getDefaultHostnameVerifier();
boolean result = hv.verify(hostname, session);
return result;
}
};
op.hostnameVerifier = hostnameVerifier;
op.sslContext = sslContext;
op.secure = true;
}
catch (Exception e) {
e.printStackTrace();
}
op.timeout = SOCKET_TIMEOUT;
op.reconnection = true;
op.reconnectionAttempts = 2;
op.reconnectionDelay = 2000l;
op.reconnectionDelayMax = 5000l;
Socket socket = IO.socket(URL, op);
return socket;
}
创建套接字并连接到服务器:
// Try to connect to the socket
Socket socket = initSocket();
socket.on(Socket.EVENT_CONNECTING, new Emitter.Listener() {
@Override
public void call(Object... args) {
Log.d(TAG, "Connecting);
}
}).on(Socket.EVENT_CONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
Log.d(TAG, "Connected !);
}
});
socket.connect();
呼叫socket.connect();
将尝试连接到网络服务,但它会挂起。没有错误消息,没有例外。我可以在控制台中看到的唯一调试消息是:"Connecting"
,来自套接字的回调。我错过了什么?