How can I escape non-english characters like "ö" from my url since it causes 404 response error. I am using Java. Please help me.
-
3Url encoding? http://download.oracle.com/javase/1.5.0/docs/api/java/net/URLEncoder.html – Anders Lindahl Jun 10 '11 at 11:10
4 Answers
E.g. by using URL-Encoding as specified in RFC3986 (http://tools.ietf.org/html/rfc3986). Please also have a look at: http://en.wikipedia.org/wiki/Percent-encoding
Java provides some methods to do this:
http://download.oracle.com/javase/1.4.2/docs/api/java/net/URLEncoder.html
Be aware of different encodings like ISO-8859-1/15, UTF-8. Depending on this for example an 'ö' will be encoded to %F6 or &C3%D6 (or sth. like this).

- 935
- 2
- 6
- 10
I had a similar problem, there was a 'ü' in URL path. After a few hours of experimenting with various SO posts I got this (from here):
URL url = new URL(urlString);
URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
url = new URL(uri.toASCIIString());
Trick is in converting URI to URL. Most answers ended with URI.toURL() method call. While this method correctly encodes whitespaces and non-letter characters, it doesn't encode non-ASCII letters. Method URI.toASCIIString() is answer to that problem.

- 1
- 1

- 703
- 10
- 22
-
Great find! Thank you, works like a charm! (I had cyrillyc letters embedded in URL and URLEncoder.encode() wasn't able to encode them). – dimsuz Jul 08 '13 at 15:02