Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Java URLEncode question?

I'm writing a Java program that creates a URI. Unfortunately it looks like I have to use a URLEncoder to convert all the spaces into compatible URL syntax. Normally that's either %20 or the plus + sign. However, when I try to put a URL like "http://server/a+file.mp3%22 into Firefox the server comes back with a 404 error. If I replace the plus sign with %20 it works. Anyone know why?

Update:

URLs cannot contain spaces. URL encoding normally replaces a space with a + sign.

http://www.w3schools.com/TAGS/ref_urlencode.asp

Update 2:

The quote is directly from that link. Likewise, both the URLEncoder in Java and .Net replace spaces with + signs in URLs.

3 Answers

Relevance
  • 1 decade ago
    Favorite Answer

    Yes, the w3schools.com page does say that. I remember using "+" for <space> in URLs a long time ago. I think %20 became the more commonly used replacement, perhaps related to the introduction of URIs. The earlier RFC 2396 specified %20, as does the current authoritative document RFC3986.

    http://tools.ietf.org/html/rfc3986#section-2.1

    Or maybe because Javascript uses only %20 when encoding. Anyway, either should work. The problem probably doesn't involve Firefox at all. The problem almost certainly resides in the server. I'm guessing the server-side code is decoding the HTTP request, and when it parses the requested resource from the GET request line, it isn't properly replacing the "+" with a blank when it searches for the "a file.mp3" resource.

  • Silent
    Lv 7
    1 decade ago

    I think you're a little confused. The encoded version of a space is %20, not a plus sign. "a file.mp3" and "a+file.mp3" are two different files.

    Edit:

    You're correct that URLs shouldn't contain spaces, but URL encoding replaces a space with %20, not a plus sign. I'm not sure where you're getting the plus sign thing from; the page you linked to does not mention it.

  • Relex
    Lv 4
    1 decade ago

    That's because it's uhmm.. Hex I think, and %20 is just instead of a space, while a + has the value of %21 I think..

    So if you type in server.com/my index/index.html the browser (Modern once at least) automatically converts the space into %20, so it becomes readable, same with the plus, the browser converts it into %21 (Or whatever the code is) so that it's readable..

    Hope this helped!

Still have questions? Get your answers by asking now.