我跟随this link在同一端口上运行socket和sslsocket。 请检查以下代码以查找相同的端口。
private void startServer(int port) {
try {
ServerSocketFactory ssf = ServerSocketFactory.getDefault();
ServerSocket serverSocket = ssf.createServerSocket();
serverSocket.bind(new InetSocketAddress("127.0.0.1", port));
serverSocket.setReuseAddress(true);
SSLContext sslContext = SslUtil.createSSLContext(this);
SSLSocketFactory sslSf = sslContext.getSocketFactory();
while (true) {
try {
Socket client = serverSocket.accept();
SSLSocket sslSocket = (SSLSocket) sslSf.createSocket(client, client.getInetAddress().getHostAddress(),
client.getPort(), false);
sslSocket.setUseClientMode(false);
boolean isSecure = false;
String _url = "http://127.0.0.1:" + port + "/";
String protocol = sslSocket.getSession().getProtocol();
if (protocol != null && protocol.contains("TLS")) {
_url = "https://127.0.0.1:" + port + "/";
isSecure = true;
}
if (isSecure) {
executorService.execute(new HttpProcessor(sslSocket, _url, ctx));
} else {
executorService.execute(new HttpProcessor(client, _url, ctx));
}
} catch (Exception ex) {
serverSocket.close();
Log.e("EX:", ex.toString());
findPort();
break;
}
}
} catch (Exception e) {
Log.e("Error:p1:", e.toString());
}
}
此代码在Android版本6.0(Marshmallow)上工作正常。 但是当我在Android 7.0及更高版本上运行此代码时,Https工作正常,但Http无法正常工作。
这一行" 字符串协议= sslSocket.getSession()。getProtocol(); "连接时关闭,同时调用http请求。
还有其他方法可以达到这个目的吗?
如何在android中找到客户端请求是http还是https sslSocket?
由于