codenameone / CodenameOne

Cross-platform framework for building truly native mobile apps with Java or Kotlin. Write Once Run Anywhere support for iOS, Android, Desktop & Web.
https://www.codenameone.com/
Other
1.72k stars 409 forks source link

network manager gets exceptions opening https connections #1917

Open ddyer0 opened 8 years ago

ddyer0 commented 8 years ago

Preparing to convert my HTTP to HTTPS, I find that my lower level use of the network manager fails when opening HTTPS connections. This works fine with http requests, and curiously, also works fine with https on real devices. This is a simulator only problem.

run the attached program (there's nothing special about the URL) and on the simulator the network thread gets a SSLHandshakeException and fails to call either of the callbacks. On real devices, the normal contination callback is used.

package dtest.boardspace;
//
// this version shows the network code getting an exception 
// opening a https connection.
//
import com.codename1.io.ConnectionRequest;
import com.codename1.io.NetworkEvent;
import com.codename1.io.NetworkManager;
import com.codename1.ui.Component;
import com.codename1.ui.Display;
import com.codename1.ui.Form;
import com.codename1.ui.Image;
import com.codename1.ui.Label;
import com.codename1.ui.events.ActionListener;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.ui.Graphics;
import com.codename1.ui.layouts.BorderLayout;

import java.util.Vector;

public class Dtest implements ActionListener<NetworkEvent> {

    private Form current;
    Component labl = null;
    Vector<String>messages = new Vector<String>();

    @SuppressWarnings("unused")
    private Resources theme;
     int step = 0;
     Image chip = null;

    public void init(Object context) {
        theme = UIManager.initFirstTheme("/theme");
         // Pro only feature, uncomment if you have a pro subscription
        // Log.bindCrashProtection(true);
    }
    @Override
    public void actionPerformed(NetworkEvent evt) {
        messages.addElement("response: "+evt);
    }
    public void load()
    {   String str = "https://boardspace.net/english/index.html";
        messages.addElement("Starting load of "+str);
        ConnectionRequest u = new ConnectionRequest(str);
        NetworkManager n = NetworkManager.getInstance();
        messages.addElement("url "+u);
        labl.repaint();
        u.addResponseCodeListener(this);
        u.addResponseListener(this);
        n.addToQueueAndWait(u);
        messages.add("Complete");
        labl.repaint();
    }

    public void start() {
        chip = Resources.getGlobalResources().getImage("black-chip-np.jpg");
        chip = chip.scaled(109,109);
        if(current != null){
            current.show();
            return;
        }
        Form hi = new Form("Hi >>0 World");
         current = hi;
        hi.setLayout(new BorderLayout());
        labl = new Label("Hi Overlay World 2") {
        @Override
            public void paint(Graphics g) {
                int w = getWidth();
                int h = getHeight();
                g.setColor(0x8f8f9f7f);
                g.fillRect(0,0,w,h);
                step++;
                g.setColor(0x0);
                g.drawString("step "+step,10,10);
                for(int i=0;i<messages.size();i++)
                {   
                g.drawString(messages.elementAt(i),10,30+30*i);
                }

             }

        };
        hi.addComponent(BorderLayout.CENTER, labl);
        messages.addElement("Start");
        hi.show();
        try {
        load();
        }
        catch (Throwable err)
        {
            messages.addElement("error "+err);
            labl.repaint();
        }
    }

    public void stop() {
        current = Display.getInstance().getCurrent();
    }

    public void destroy() {
    }

}
ddyer0 commented 8 years ago

Looking a little further into this, I think the immediate problem is that the certificate for boardspace.net is not sufficiently trusted by the JVM, so the correct response would be to call the error callback.