Socket.io-client-java: java.net.MalformedURLException: Unknown protocol: ws

Created on 22 Oct 2015  Â·  31Comments  Â·  Source: socketio/socket.io-client-java

Hi. i try to connect to socket server which start with "ws://..." and when I set this url to

 IO.socket(pathSocket, options);

I receive error

 java.lang.RuntimeException: java.net.MalformedURLException: Unknown protocol: ws
 W/System.err﹕ at io.socket.client.Url.parse(Url.java:52)
 W/System.err﹕ at io.socket.client.IO.socket(IO.java:60)
 W/System.err﹕ at io.socket.client.IO.socket(IO.java:41)
bug

Most helpful comment

All 31 comments

Ah, I didn't know URL doesn't accept ws. It's a bug, please use http:// url for now.

Also the same problem with wss://

Can I wait this fix soon?

I wanted t ofix it by myself but there're not all class in project

Just use http:// or https://. They are actually the same with ws:// and wss:// .

we use the websocket can connect wss:// , but use this library ,can't connect the "https://" , no any reponse,can you help ?

+1, this is causing heavy issues on socket servers: losing a lot of CPU on back and forth requests upgrading the connexion. This is a major issue when you have a lot of conn/second

+1

This is caused by java.net.URL not knowing how to open a connection towards a ws:// stream. It could be solved by using java.net.URI instead, which is what the Java-WebSocket library uses.

did it fix?

are there any news on this?

We are using Socket.io 1.4.5 and this library on Android client. For connection string we are using http(s), but it switches to ws automatically with these setup:

    IO.Options opts = new IO.Options();
    opts.transports = new String[]{ WebSocket.NAME};
    return IO.socket("https://socket.io.server", opts);

@jvilya hi,I have tried your code above,but it doesn't work.
Is the code used in android client?

@isanwenyu yes. Try to debug it with any proxy-debugging tool and see what happens
In my case I saw connection to https and then switch to ws

2 Years later and still no fix? is this product no longer being supported?

hello guys this ws: issue is fixed?

any solution to implement

Still not working and it is not connect with http:// .

not working at all!!!!!!!!!

after a year ... it still does not work

no hope for this topic

The socket server I'm connecting to only works with wss...any solutions for 2019?

java.lang.RuntimeException: java.net.MalformedURLException: Unknown protocol: ws who know?how to fix it

Maybe we could use this to resolve the issue
https://medium.com/square-corner-blog/okhttps-new-url-class-515460eea661#.v55accs1c

This doesn't fix the ws: issue. OkHttp just replaces ws: with http: before parsing the URL.

from the HttpUrl javadocs:

 * Sometimes referred to as *protocol*, A URL's scheme describes what mechanism should be used to
 * retrieve the resource. Although URLs have many schemes (`mailto`, `file`, `ftp`), this class only
 * supports `http` and `https`. Use [java.net.URI][URI] for URLs with arbitrary schemes.

Idk if someone is still looking for an easy hot-fix, but I'll just leave it here.

    public static void addURLProtocols() {

        try {

            Field field = URL.class.getDeclaredField("handlers");
            field.setAccessible(true);

            URLStreamHandler handler = new URLStreamHandler() {
                @Override
                protected URLConnection openConnection(URL url) throws IOException {
                    return new URLConnection(url) {
                        @Override
                        public void connect() throws IOException {}
                    };
                }
            };

            Hashtable<String, URLStreamHandler> handlers = (Hashtable<String, URLStreamHandler>) field.get(null);
            handlers.put("wss", handler);
            handlers.put("ws", handler);

        } catch(Exception ex) {
            ex.printStackTrace();
        }

    }

Hi,

I was able to solve this problem in this way:

(Kotlin)

val options = IO.Options()
            options.forceNew = true
            options.path = "/wss"

 socket = IO.socket("https://", options)

Nothing here is working for me..
But I didn't check medium blog
I have used org.java_websocket and it worked well, but I need to know what is the problem with socket io!

@Mohamed-Shalabi
Have you tried the code below?

Idk if someone is still looking for an easy hot-fix, but I'll just leave it here.

    public static void addURLProtocols() {

        try {

            Field field = URL.class.getDeclaredField("handlers");
            field.setAccessible(true);

            URLStreamHandler handler = new URLStreamHandler() {
                @Override
                protected URLConnection openConnection(URL url) throws IOException {
                    return new URLConnection(url) {
                        @Override
                        public void connect() throws IOException {}
                    };
                }
            };

            Hashtable<String, URLStreamHandler> handlers = (Hashtable<String, URLStreamHandler>) field.get(null);
            handlers.put("wss", handler);
            handlers.put("ws", handler);

        } catch(Exception ex) {
            ex.printStackTrace();
        }

    }
Was this page helpful?
0 / 5 - 0 ratings