title: Encoding an URL sent to a server (not in query)
body: <p>I need to be testing my server for several URLs daily since these URLs are updated by my users - and this will be dine in Java. However, these URLs contains strange characters (like the german umlaut). Basicly what I am doing is:</p>
  
  <pre><code>for every URL in the list to check
    URL u = new URL(the_url);
    u.openConnection(..);
    // read the content and handle it
  </code></pre>
  
  <p>Now, what Ive found is that <code>org.apache.commons.codec.net.URLCodec</code> is fine for encoding string to paste into the QueryString, it is not as suitable to encode strange URLs into their hex counterparts. Here are some examples of URLs:</p>
  
  <ul>
  <li>http:// www.example com/u/überraum-03/</li>
  <li>http:// www.example com/u/são-paulo-dude/</li>
  <li>http:// www.example com/u/håkon-hellström/</li>
  </ul>
  
  <p>The desired result for the first would be;</p>
  
  <ul>
  <li>http:// www.example com/u/%c3%9berraum-03/</li>
  </ul>
  
  <p>Are there any library in the Apache Commons or java itself, to convert special character in the ACTUAL url (not querystring - and therefore not replace the same kind of characters) ?</p>
  
  <p>Thank you for your time.</p>
  
  <p><strong>Edited</strong>
  Firefox translates "yr.no/place/Norway/Nordland/Moskenes/Å/data.html"; into "yr.no/place/Norway/Nordland/Moskenes/%C3%85/data.html" (try this by entering the first URL, press enter, then copy the url into a document). It is this effect that I am looking for - since this is the actual translation. What is most likely happening is either FF knows Å is a bad thing, it tries multiple versions or it accepts the servers "Location" header; either way - there is a tranformation from "Å" to "%C3%85" on only a subset of the URL. This is the function we need.</p>
  
  <p><strong>Edited</strong>
  I just verified that the code given by commentor does not work sadly. As an example, try this:</p>
  
  <pre><code>try{
          String urlStr = "http://www.yr.no/place/Norway/Nordland/Moskenes/Å/data.html";
          URL u=new URL(urlStr);
          URI uri = new URI(u.getProtocol(),
                      u.getUserInfo(), u.getHost(), u.getPort(),
                      u.getPath(), u.getQuery(),
                      null); // removing ref
  
          URL urlObj = uri.toURL();
          HttpURLConnection connection = (HttpURLConnection) urlObj.openConnection();
          connection.setInstanceFollowRedirects(false);
          connection.connect();
  
          for (int i=0;i&lt;connection.getHeaderFields().size();i++)
              System.out.println(connection.getHeaderFieldKey(i)+": "+connection.getHeaderField(i));
          System.exit(0);
      }catch(Exception e){e.printStackTrace();};
  </code></pre>
  
  <p>Will yield a 404 error - strangely enough the encoded part does also not work.</p>