Home  >  Article  >  Java  >  Why does URLEncoder replace spaces with ' ' in HTML form URLs, and how can I get " " instead?

Why does URLEncoder replace spaces with ' ' in HTML form URLs, and how can I get " " instead?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-11-11 10:45:03248browse

Why does URLEncoder replace spaces with ' ' in HTML form URLs, and how can I get

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn