Salik Reza
Salik Reza

Reputation: 31

How to parse URL if protocol is not http or https?

My question is how to parse url where protocol is different?

String str = "olqmeeting://192.168.43.55/uam/meeting/meeting.php?id=A72U5AF9";
URL url = new URL(str);

Upvotes: 2

Views: 5122

Answers (2)

Stephen C
Stephen C

Reputation: 718916

You can use the URI class to parse those URLs. It will work ... though you are limited to operations on the URL itself, no on the resource that it refers to. (Refer to the URI javadoc for more details.)

If you want URL to work (and to be able to "connect" to the resource), then you need to implement and configure a URLStreamHandlerFactory that (minimally) understands the URL's protocol and how to handle it.

(If you don't supply a non-null URLStreamHandlerFactory explicitly when you create the URL object, the constructor will attempt to find the default one for the URL's protocol. If it can't do that, you get a MalformedURLException.)

Upvotes: 3

Sam
Sam

Reputation: 2970

URL has other constructors where you can define the protocol

URL(String protocol, String host, int port, String file)
Creates a URL object from the specified protocol, host, port number, and file.
URL(String protocol, String host, int port, String file, URLStreamHandler handler)
Creates a URL object from the specified protocol, host, port number, file, and handler.
URL(String protocol, String host, String file)
Creates a URL from the specified protocol name, host name, and file name.

Eventually you can even use the .set() method but it is protected so you need to implement a new custom URL class

Upvotes: 0

Related Questions