Avatar ·

Encoding URL query parameters in Java: how to do it correctly?

Hello everyone! I'm a beginner in Java and writing a small application that sends HTTP requests to an external API. I need to pass parameters in the query string, but I've run into a problem: some parameters contain special characters such as spaces, Cyrillic, &, = signs, etc.\n\nFor example, one of the parameters is a search query: "Java & Spring Framework". If I simply add it to the URL, I get an error because the '&' character is interpreted as a parameter separator.\n\nI tried using `URLEncoder.encode()`, but the results look strange. For example, a space turns into '+', not '%20'. Also, I'm not sure whether I need to encode the entire URL or just the parameter values. Here's an example of my code:\n\n```java\nString query = "Java & Spring Framework";