Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: openURL Not Working with Foreign Characters



Herb Bowie wrote:

>I converted the entire URL using URLEncoder, then put the : and /  
>characters back in where I found their replacements, and this works  
>for all the test cases I have tried so far.

Only some parts of a URL should be URL-encoded.  See RFC 1738 and RFC 2396
for what the parts of a URL are, and why those parts have to be encoded:
  <http://www.ietf.org/rfc.html>

Understanding the spec, makes it easier to see what to do.


Next, refer to this java.net.URL constructor:
    public URL(String protocol, String host, int port, String file)

Only the 'file' part should be URL-encoded.  The 'file' part consists of
the 'path' part and the 'query' part.  Read the API docs for URL.getFile()
for details.

The basic idea is to use the URL class to parse your text-form URL into
parts, only encode the parts that should be encoded, then use
URL.toString() to assemble the parts again.

So:
  String textForm = "http://www.google.se/webhp?hl=sv&btnG=Google-s–kning&meta=";;
  URL url = new URL( textForm );
  String encoded = URLEncoder.encode( url.getFile(), "UTF-8" );
  url.setFile( encoded );
  String encodedForm = url.toString();

and you have to catch MalformedURLException.

It would be easy to encapsulate all the non-literal elements into a utility
function, with a String arg and a String return value.

  -- GG


 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/java-dev/email@hidden

This email sent to email@hidden



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.