1

How can I escape non-english characters like "ö" from my url since it causes 404 response error. I am using Java. Please help me.

skmaran.nr.iras
  • 8,152
  • 28
  • 81
  • 116

4 Answers4

5

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).

duselbaer
  • 935
  • 2
  • 6
  • 10
3

use URLEncoder/ URLDecoder in the java.net package

Puce
  • 37,247
  • 13
  • 80
  • 152
3

Try the java.net.URLEncoder

Karl-Bjørnar Øie
  • 5,554
  • 1
  • 24
  • 30
2

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.

Community
  • 1
  • 1
Emperor Orionii
  • 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