Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

java.net.MalformedURLException: Unknown protocol: ws #233

Closed
Zo2m4bie opened this issue Oct 22, 2015 · 33 comments
Closed

java.net.MalformedURLException: Unknown protocol: ws #233

Zo2m4bie opened this issue Oct 22, 2015 · 33 comments
Labels
Milestone

Comments

@Zo2m4bie
Copy link

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)
@nkzawa
Copy link
Contributor

nkzawa commented Oct 22, 2015

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

@nkzawa nkzawa added the bug label Oct 22, 2015
@Zo2m4bie
Copy link
Author

Also the same problem with wss://

@Zo2m4bie
Copy link
Author

Can I wait this fix soon?

@Zo2m4bie
Copy link
Author

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

@nkzawa
Copy link
Contributor

nkzawa commented Oct 22, 2015

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

@SteveMobile
Copy link

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

@PuKoren
Copy link

PuKoren commented Mar 16, 2016

+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

@kruyvanna
Copy link

+1

@sindrenm
Copy link

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.

@sr71k
Copy link

sr71k commented Nov 3, 2016

did it fix?

@Agraphie
Copy link

Agraphie commented Dec 8, 2016

are there any news on this?

@b95505017
Copy link
Collaborator

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

@jvilya
Copy link

jvilya commented Feb 7, 2017

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);

@isanwenyu
Copy link

isanwenyu commented Jul 30, 2017

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

@jvilya
Copy link

jvilya commented Jul 31, 2017

@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

@aaronstanley
Copy link

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

@maheshputtegowda
Copy link

hello guys this ws: issue is fixed?

@maheshputtegowda
Copy link

any solution to implement

@GirishBhutiya
Copy link

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

@PhonegapProjects
Copy link

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

@jakubzieba
Copy link

after a year ... it still does not work

@songkadi
Copy link

no hope for this topic

@tiholic
Copy link

tiholic commented Aug 11, 2019

@Arnique
Copy link

Arnique commented Dec 7, 2019

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

@xiaoshengmm
Copy link

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

@akindofyoga
Copy link

akindofyoga commented Dec 18, 2019

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.

@felipecsl
Copy link

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.

@H4kt
Copy link

H4kt commented Apr 6, 2020

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();
        }

    }

@ghost
Copy link

ghost commented Jul 28, 2020

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)

@Mohamed-Shalabi
Copy link

Mohamed-Shalabi commented Sep 15, 2020

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!

@H4kt
Copy link

H4kt commented Oct 28, 2020

@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();
        }

    }

darrachequesne added a commit that referenced this issue Apr 26, 2021
The URL constructor does not support the ws:// scheme, and would throw:

> java.net.MalformedURLException: unknown protocol: ws

Related:

- #650
- #555
- #233
@darrachequesne
Copy link
Member

For future readers: this should be fixed by 67fd5f3, included in version 2.0.1.

@darrachequesne darrachequesne added this to the 2.0.1 milestone Apr 27, 2021
@TheFoLLoNeRoU
Copy link

For future readers: this should be fixed by 67fd5f3, included in version 2.0.1.

Thank you very mach!! I was using version very old and i was founding very problems with exception about "ws" and problems of connected to server with protocol "http"

darrachequesne added a commit that referenced this issue Jul 10, 2022
The URL constructor does not support the ws:// scheme, and would throw:

> java.net.MalformedURLException: unknown protocol: ws

Related:

- #650
- #555
- #233

Backported from 67fd5f3
cedev935 added a commit to cedev935/socket-java that referenced this issue Sep 14, 2023
The URL constructor does not support the ws:// scheme, and would throw:

> java.net.MalformedURLException: unknown protocol: ws

Related:

- socketio/socket.io-client-java#650
- socketio/socket.io-client-java#555
- socketio/socket.io-client-java#233
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests