zx_wing
zx_wing

Reputation: 1966

How to handle "@" in password of java URI?

I have a URL "ssh://root:zstackqwe:!@#@172.16.36.184" which contains "@" and ":" in the password part. I use java.net.URI to wrap the string like:

URI u1 = new URI("ssh://root:zstackqwe:!@#@172.16.36.184/");
System.out.println(u1.getAuthority());
System.out.println(u1.getHost());

the output is:

root:zstackqwe:!@
null

The authority part is correct while the host part returns null. How should correctly handle those special chars?

UPDATE:

The string "ssh://root:zstackqwe:!@#@172.16.36.184" is a raw string without any encoding, passed from the API to my application. I am unable to use constructors other than UIR(string). So I am looking for a way to handle the raw string making it work with jave.net.URI.

Upvotes: 4

Views: 1221

Answers (2)

Jasen
Jasen

Reputation: 12432

So that string comes from someone else:

It's mal-formed. Get them to submit a correctly formed URL instead.

Upvotes: 0

Jasen
Jasen

Reputation: 12432

If it's a URI use percent encoding.

URI u1 = new URI("ssh://root:zstackqwe%3a!%40%[email protected]/");

Upvotes: 1

Related Questions