Question

We have a connection string that is built like so:

http://${i.user}:${i.pass}@${i.host}:22

However, if that users password contains an '@', then we end up with a string like this:

http://user:p@ss@1.2.3.4:22

This obviously throws the script off. Is there a way to escape that first @, or something to that effect?

Thanks

Was it helpful?

Solution

Characters like "@" in the user info portion of a URI have to be escaped. When parsing such a URI, the URI consumer should unescape any escaped characters:

import java.net.URI;
import java.net.URISyntaxException;

public class URITest {

    public static void main(String[] args) throws URISyntaxException {
        URI uri = new URI("http", "user:p@ss", "example.com", -1,
                null, null, null);
        System.out.println(uri.toString());

        URI uri2 = new URI(uri.toString());
        System.out.println(uri2.getUserInfo());
    }
}

On my system, this outputs:

http://user:p%40ss@example.com
user:p@ss

In the original question, it looks like you're not escaping the '@' when constructing the URI. In your comment to @leblma, it sounds like whatever is using the URI isn't unescaping the % sequence.

OTHER TIPS

Replace every "@" in user or password with "%40"

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top