25 August 2005
Encoding URLs
If you're going to reference URLs in a Web page generated through code, you need to encode the search part of those URLs to have acceptably escaped HTML characters. In Java you can do the following:
import java.lang.*; import java.net.*; // Copy our raw URL. String encodedURL = rawURL; // Determine if the URL contains a query. URL url = new URL(rawURL); if (url.getQuery() != null) { // Encode only the query part. int queryIndex = rawURL.indexOf(url.getQuery()); encodedURL = rawURL.substring(0, queryIndex) + url.getQuery().replaceAll("&", "&"); }
Friday 26 August 2005
Code changed above. Instead of using URL.Encode() to encode the query, just replace ampersands with their HTML escape. We don't want any equal signs encoded.
Keywords
[ posted by sstrader on
25 August 2005 at 11:56:02 AM in Programming
]
- Techniques after using Swift for a month posted by sstrader on 26 August 2015 at 11:41:51 PM
- Some thoughts on Ruby after finishing a week (minus one day) of training posted by sstrader on 15 December 2011 at 8:59:30 PM
- Links on the singleton pattern posted by sstrader on 9 December 2011 at 9:19:50 AM
- Phonebot posted by sstrader on 29 October 2011 at 6:37:05 PM
- The labeled break in Java posted by sstrader on 4 September 2011 at 11:27:56 AM
Related entries
Other entries categorized in Programming: