Android Question HttpServer library how use Https

Lello1964

Well-Known Member
Licensed User
Longtime User
Hi,
I am using the HttpServer library to communicate with a web application,
I would like to know how to use the Https protocol with an SSLcertificate.

Raffaele
 

aeric

Expert
Licensed User
Longtime User
Which OS are you running for the server?

For Ubuntu:

There are other threads in this forum using Windows and other paid SSL. Just do a search.
 
Upvote 0

Lello1964

Well-Known Member
Licensed User
Longtime User
I use the library in an android app to receive requests and send information via Https.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
No need to purchase anything. HttpServer is a simplified version of jServer (which does support SSL). With some work it can be added, however as we have an excellent tool to create powerful http servers (B4J), which can run on small RPis if needed, I don't see a strong need for an Android http server.
Use MQTT instead.
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Thanks Erel, maybe in the moment of slowdown of the work I will try to add it. But more for a challenge with oneself than for a real need.
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
No need to purchase anything. HttpServer is a simplified version of jServer (which does support SSL).
@Erel forgive that I return to this topic and if it is the case to open a new thread tell me.

In the jServer Library (B4J) the instance of the Server class (org.eclipse.jetty.server.Server) is created with the Initialize method. Before starting the server, you can add the SSL connector.
It can also be done not using the SetSslConfiguration command, but with JavaObject since the Server class is public.

In the HTTPServer library (B4A) the instance of the Server class is created with the START method and inside it the connector is inserted and the server started. So adding an SSL connector before or after the start would generate an error.
In any case it would not be possible to use the addConnector command because Server is a private variable, so I cannot use it with JavaObject.

So what do you advise me?
Unfortunately it is the best I can do in Java and it is already a lot for my poor language skills
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Use the Reflection library instead of JavaObject when trying to access private variables
I tried but without success. But I'm not sure I used it correctly
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Maybe try this (I used the code below with the HttpServer example):
B4X:
    server.Initialize("Server")
    server.Start(port)
    Dim r As Reflector
    r.Target = server
    r.Target = r.GetField("server")    'Get private server object
    Log($"toString output: ${r.RunMethod("toString")}"$) 'Just to show we got the private server object
    Log("Stopping server via private server object")
    r.RunMethod("stop") 'Stop server
    'Maybe now can set up SSL
    Log("Starting server via private server object")
    r.RunMethod("start") 'Restart server after setting up SSL
Leaving SSL to someone else
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Maybe try this (I used the code below with the HttpServer example):
B4X:
    server.Initialize("Server")
    server.Start(port)
    Dim r As Reflector
    r.Target = server
    r.Target = r.GetField("server")    'Get private server object
    Log($"toString output: ${r.RunMethod("toString")}"$) 'Just to show we got the private server object
    Log("Stopping server via private server object")
    r.RunMethod("stop") 'Stop server
    'Maybe now can set up SSL
    Log("Starting server via private server object")
    r.RunMethod("start") 'Restart server after setting up SSL
Leaving SSL to someone else
Already tried. In this way it says the field does not exist.
Have you tried this code?
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User

Attachments

  • HttpServerExample_Reflection.zip
    58.5 KB · Views: 256
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Note: You have to use HttpServer's start method first before you can properly access the underlying server object (it is not created until then).
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
I need to start it myself.
Why do I have to plug in the connector before starting it.
Actually I should also create a Server instance and then insert the SSL connector.

Inside the Start of httpserver.Start does everything together. I need to do something similar to rewriting the httpserver Start method.

Here's more or less what I should do:
B4X:
Public Sub SetupSSL
    Dim r As Reflector
    r.Target = server
    Dim O As Object = r.GetField("server")    'Get private server object
    
    Dim J As JavaObject = Me
    j.RunMethod("active_ssl", Array(o))
End Sub

#If Java
import org.eclipse.jetty.http.ssl.SslContextFactory;
import org.eclipse.jetty.server.ssl.*;
import org.eclipse.jetty.server.*;
import org.eclipse.jetty.util.resource.Resource;

    public void active_ssl(Server jserver){
        //jserver = new Server();
        Resource keystore = Resource.newClassPathResource("/server.jks");
        if (keystore != null && keystore.exists()) {
            //connector.setConfidentialPort(8443);
            SslContextFactory factory = new SslContextFactory();
            factory.setKeyStoreResource(keystore);
            factory.setKeyStorePassword("xxxxx");
            factory.setTrustStore(keystore);
            factory.setKeyManagerPassword("xxxxx");
            SslSocketConnector sslConnector = new SslSocketConnector(factory);
            sslConnector.setMaxIdleTime(5000);
            sslConnector.setPort(8443);
            sslConnector.setAcceptors(4);
            jserver.addConnector(sslConnector);
            System.out.println("SSL access to the quickstart has been enabled on port 8443");
            System.out.println("You can access the application using SSL on https://localhost:8443");
            System.out.println();
            //jserver.start();
        }
    }
#End If

This code must be after the Server variable / class is instantiated, but before the Server variable is started
 
Upvote 0
Top