0

I have a string s = "sunil\" and t = "sunil/"

When i create URI from the string, I get some abnormal values

http://admin:sunil%5c 

So my url is not getting executed properly.

I want my url to be http://admin:sunil\ or http://admin:sunil/

My URL creation is like this: http://userid:password@host:port

where in my password string contains "\" character. So it fails to create the URL correctly

please give information how to retain \ and / inside a string when creating a url.

i dont know if i have to use replace() in the string.

Sunil
  • 521
  • 1
  • 7
  • 24

1 Answers1

0

Use the Uri.Builder class when building URI or URLs. This can save lots of headaches.

String s = "sunil\\";
String t = "sunil/";
assertEquals("http://admin:sunil\\",
                new Uri.Builder().scheme("http").authority("admin:" + s).build().toString());
assertEquals("http://admin:sunil/",
                new Uri.Builder().scheme("http").authority("admin:" + t).build().toString());
Some Noob Student
  • 14,186
  • 13
  • 65
  • 103
  • I am creating the url with userid:password@host:port .... In which the password string contains "\" character. So when i construct the url it replaces "\" with %50C and hence the url becomes invalid and not executable – Sunil Mar 12 '15 at 12:13
  • I am creating the url with userid:password@host:port .... In which the password string contains "\" character. So when i construct the url it replaces "\" with %50C and hence the url becomes invalid and not executable – Sunil Mar 14 '15 at 09:04