URLEncoder's Treatment of Space Characters in HTML Form Urls
In URLEncoding, converting a string to the "application/x-www-form-urlencoded" MIME format, the URLEncoder expects to replace space characters with ' ' signs as per the HTML Specifications. This behavior follows the encoding rules for HTML forms, where control names and values must be escaped and spaces replaced with ' '.
The issue arises when the expected output is " " for space characters instead of the default behavior of " ". To resolve this discrepancy, one must manually replace the ' ' sign with ' ' after the initial URLEncoding. For instance:
System.out.println(java.net.URLEncoder.encode("Hello World", "UTF-8").replace("+", "%20"));
This will output the expected "Hello World" with space characters correctly encoded as " ".
The above is the detailed content of Why does URLEncoder replace spaces with ' ' in HTML form URLs, and how can I get " " instead?. For more information, please follow other related articles on the PHP Chinese website!