How can I redirect all requests to https in NanoHTTPD? If I turn on the server before calling makeSecure (), then a call from the browser at http: // myserver gives an error

SEVERE: Could not send response to the client javax.net.ssl.SSLException: Connection has been shutdown: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection? at sun.security.ssl.SSLSocketImpl.checkEOF(SSLSocketImpl.java:1484) at sun.security.ssl.SSLSocketImpl.checkWrite(SSLSocketImpl.java:1496) at sun.security.ssl.AppOutputStream.write(AppOutputStream.java:62) at fi.iki.elonen.NanoHTTPD$Response.sendBody(NanoHTTPD.java:1620) at fi.iki.elonen.NanoHTTPD$Response.sendBodyWithCorrectEncoding(NanoHTTPD.java:1593) at fi.iki.elonen.NanoHTTPD$Response.sendBodyWithCorrectTransferAndEncoding(NanoHTTPD.java:1583) at fi.iki.elonen.NanoHTTPD$Response.send(NanoHTTPD.java:1550) at fi.iki.elonen.NanoHTTPD$HTTPSession.execute(NanoHTTPD.java:952) at fi.iki.elonen.NanoHTTPD$ClientHandler.run(NanoHTTPD.java:192) at java.lang.Thread.run(Thread.java:701) 

How to avoid it?

  • You need to raise two servers, one with HTTP on port 80 (which will generate a redirect, substituting port 443 in the request data), the second with HTTPS on port 443, which will already do the real work. I'm not sure that it is physically possible to listen to both protocols on the same socket (you can, of course, try { readHttps() } catch (Any e) { readHttp() } , but there is a lot of space for a shot in the leg). - etki
  • In NanoHttpd, the server constructor takes a port as an argument. Accordingly, I am going to access the server https : //192.168.0.1: 8080. But what to do if the user in the browser types http : //192.168.0.1: 8080? - McMan
  • he just won't be able to connect - etki
  • Is there really no way somewhere at the socket level to "pop out an unencrypted request and send a redirect"? - McMan
  • How do you imagine the separation of encrypted byte stream from unencrypted? Again, you can, in theory, read the first few bytes and analyze on the basis of them, but it still shoots up and is difficult to integrate into the infrastructure. - etki

0