androidsslhttpsnanohttpd

java.net.SocketException - NanoHTTPD (https) - Android


I'm stuck and looked everywhere to find a solution but I think it won't be as effective as getting help from humans haha.
I set up a NanoHTTPD server to listen to https post requests:

public ChargingListener(int port, MainActivity activity) {
    super(port);
    try {
        this.activity = activity;
        makeSecure(NanoHTTPD.makeSSLSocketFactory("/keystore.jks", "password".toCharArray()), null);
        start(NanoHTTPD.SOCKET_READ_TIMEOUT, false);
        logger.info(TAG, "Listener created on port: " + port);
    } catch (IOException e) {
        logger.error(TAG, "Error creating listener: " + e.getMessage());
    }
}

When I send a request, I get the following exception (if needed, I can paste the full exception stack):

java.net.SocketException: Socket is closed

The server works normally with http...
BTW, I used the code from a NanoHTTPD guide.

Update: I managed to send a post request with postman and actually received a response, but when I try to send a post request via python requests library, this error rises again.

Solution: By default, postman does not verify self-signed certificates, but python requests does...
Hope this helps someone!


Solution

  • Solution: By default, postman does not verify self-signed certificates, but python requests does...
    So, when sending a post request, just add a "verify=False" flag. Hope this helps someone!