This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I am using URLEncoder's encode method to encode a string with spaces in it that is to become a URL. I have two problems/questions. I'm using the "UTF-8" encoding class and the spaces do not convert to %20, but instead convert to the "+" sign. I suspect that is because this is for converting data on a form and that I may need to use a different encoding class. The other problem is that any colons or slashes that are a legitimate portion of the URL are encoded and I do not want them to be converted.
Is there a character class I should be using that is more appropriate to URLs instead of form data? Do I need to define my own? Is there a different method I need to be using? Or do I just need to substitute manually within my strings?
In my situation, since I want to encode a URL, not form data, is there another encoding class that would perform my conversion correctly and exclude converting slashes and colons that are a real part of my string?
Or do I need to do my conversion manually by substituting the spaces for the %20 in my string?
Or is there another method somewhere that does what I need?
Originally posted by Greg Ostravich: is there another encoding class that would perform my conversion correctly and exclude converting slashes and colons that are a real part of my string?
No. Perhaps you should give us an example. I'm confused as to why you are attempting to create a URL containing spaces, slashes and colons.
Joined: Jul 11, 2002
Here's my example.
I am generating a URL that will be e-mailed out to a list of people periodically based on database content. The URL will be composed of a path that has spaces in it as well as the project name, which has spaces in it. The two parts which make up the URL come from a database and are read in at run-time.
I suspect if I was opening the URL directly it would not be an issue, but because the URL is in an e-mail the link doesn't work correctly without some sort of encoding to escape the spaces in the generated URL that is in the e-mail message.
That's why I thought encoding was the way to go.
Here's an example of what it looks like before encoding:
I can do a straight substitution if I need to. My question was if there was an existing "encode" that would handle the URL as I needed or if one of the Encoding classes was designed to do this already and I just needed to pick that instead of "UTF-8"
If there's not, that's fine, but I figured I'd ask here first.
Note: I just noticed after I posted that you can see the 'break' I'm talking about in my own URL included above. The one without the %20 is 'broken' in the URL. [ June 13, 2005: Message edited by: Greg Ostravich ]
My question was if there was an existing "encode" that would handle the URL as I needed or if one of the Encoding classes was designed to do this already and I just needed to pick that instead of "UTF-8"
No. Changing the encoding will not change will not change the handling of the space character.
Joined: Jul 11, 2002
Thanks for all the replies. I had wanted to use something built-in in case there are other characters I needed to modify but instead I did this and it worked for what I needed: