Question: I'm having trouble getting wikipedia urls including unicode!

Given a page title like: 1992\u201393\_UE\_Lleida\_seasonnow 

Just plain url ...
<http://en.wikipedia.org/wiki/1992>\u201393\_UE\_Lleida\_seasonnow 

Using URLEncoder (set to UTF-8) ....
<http://en.wikipedia.org/wiki/1992%5Cu201393_UE_Lleida_seasonnow>

When I try to resolve either url, I get nothing. If I copy the urls into my browser, I get nothing too- it's only if I actually copy the unicode character in that I get the page.

Does wikipedia have some strange way to encode unicode in urls? Or am I just doing something dumb?

Here's the code I'm using:

```
URL url = new URL("http://en.wikipedia.org/wiki/"+x);
System.out.println("trying "+url);  

// Attempt to open the wiki page
InputStream is;
        try{ is = url.openStream();
} catch(Exception e){ return null; }

```

Answer: 
> 
> Does wikipedia have some strange way to encode unicode in urls?
> 
> 
> 

It's not really strange, it's standard use of [IRI](http://en.wikipedia.org/wiki/Internationalized_Resource_Identifier)s. The IRI:

```
http://en.wikipedia.org/wiki/2009–10_UE_Lleida_season

```

which includes a Unicode en-dash, is equivalent to the URI:

```
http://en.wikipedia.org/wiki/2009%E2%80%9310_UE_Lleida_season

```

You can include the IRI form in links and it will work in modern browsers. But many network libraries—including Java's, along with older browsers—require ASCII-only URIs. (Modern browsers will still show the pretty IRI version in the address bar, even if you linked to it with the encoded URI version.)

To convert an IRI to a URI in general, you use the [IDN](http://en.wikipedia.org/wiki/Internationalized_domain_name) algorithm on the hostname, and URL-encode any other non-ASCII characters as UTF-8 bytes. In your case, it should be:

```
String urlencoded= URLEncoder.encode(x, "utf-8").replace("+", "%20");
URL url= new URL("http://en.wikipedia.org/wiki/"+urlencoded);

```

Note: replacing `+` with `%20` is necessary to make values of `x` with spaces in work. `URLEncoder` does `application/x-www-form-urlencoded`-encoding as using in query strings. But in a path-URL-segment like this, the `+`-means-space rule does not apply. Spaces in paths must be encoded with normal-URL-encoding, to `%20`.

Then again... in the specific case of Wikipedia, for readability, they replace spaces with underlines instead, so you'd probably be better off replacing `"+"` with `"_"` directly. The `%20` version will still work because they redirect from there to the underline version.
Here's a simpler way of encoding the URL in Chi's answer:

```
static String encodeUrl(String urlToEncode) throws URISyntaxException {
    return new URI(urlToEncode).toASCIIString();
}

```

See [this answer](https://stackoverflow.com/questions/6519746/url-decode-in-java-6#6603584) for clarification.