• Post Reply Bookmark Topic Watch Topic
  • New Topic

MalformedURLException  RSS feed

Stephanie Smith
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I create a URL with this constructor and it works fine:
URL u = new URL("ftp", "localhost", 21, "\\channels");
I then invoke u.toString() and it prints out
So this makes it appear that using the "\" is valid and the URL is valid.
If I then try to create a second URL with this string:
URL u2 = new URL(u.toString());
it fails with
java.net.MalformedURLException: For input string: "21\channels"
at java.net.URL.<init>(URL.java:613)
at java.net.URL.<init>(URL.java:476)
at java.net.URL.<init>(URL.java:425)

So my question is, why does this second one fail and the first one succeed? thanks
Lewin Chan
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want to look some RFC's (try rfc1738 might be a good start).
ftp://localhost:21\channels isn't a valid url.
a "\" is considered dangerous character

Characters can be unsafe for a number of reasons. The space
character is unsafe because significant spaces may disappear and
insignificant spaces may be introduced when URLs are transcribed or
typeset or subjected to the treatment of word-processing programs.
The characters "<" and ">" are unsafe because they are used as the
delimiters around URLs in free text; the quote mark (""") is used to
delimit URLs in some systems. The character "#" is unsafe and should
always be encoded because it is used in World Wide Web and in other
systems to delimit a URL from a fragment/anchor identifier that might
follow it. The character "%" is unsafe because it is used for
encodings of other characters. Other characters are unsafe because
gateways and other transport agents are known to sometimes modify
such characters. These characters are "{", "}", "|", "\", "^", "~",
"[", "]", and "`".

Now you have it from TBL himself.
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!