Home > Java > javaTutorial > body text

Why Does URLEncoder Encode Spaces as ' ' Instead of ' '?

DDD
Release: 2024-11-11 06:47:02
Original
805 people have browsed it

Why Does URLEncoder Encode Spaces as ' ' Instead of ' '?

URLEncoder Conundrum: Understanding Space Character Encoding

When attempting to encode a string using URLEncoder, you might encounter an unexpected behavior where spaces are converted to ' ' symbols instead of the expected ' ' hexadecimal code.

Cause

The URLEncoder class adheres to the HTML Specifications for encoding URLs in HTML forms. According to these specifications, spaces should be replaced with ' ' symbols for parameters in application/x-www-form-urlencoded MIME format.

Solution

To achieve the desired encoding where spaces are converted to ' ', you can perform the following steps:

String encodedString = java.net.URLEncoder.encode("Hello World", "UTF-8");
encodedString = encodedString.replace("+", "%20");
Copy after login

This will replace all ' ' symbols with ' ', resulting in the correct encoding according to your expectations.

The above is the detailed content of Why Does URLEncoder Encode Spaces as ' ' Instead of ' '?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template