Blog
Wild & Free Tools

URL Encoding in Java, C#, PHP, and Bash: Code Examples

Last updated: February 2026 5 min read
Quick Answer

Table of Contents

  1. Java URL Encoding
  2. C# URL Encoding
  3. PHP URL Encoding
  4. Bash / Shell URL Encoding
  5. Frequently Asked Questions

Every major language has a URL encoding function, but they're not all identical. Java's URLEncoder encodes spaces as +. PHP has two functions: urlencode() (spaces as +) and rawurlencode() (spaces as %20). C#'s Uri.EscapeDataString() is the right choice for query values. Bash doesn't have a native function but has two clean workarounds.

Here's the correct function for each language, with copy-paste examples.

URL Encoding in Java

Use java.net.URLEncoder.encode() with StandardCharsets.UTF_8:

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

String value = "hello world & more";
String encoded = URLEncoder.encode(value, StandardCharsets.UTF_8);
// Result: "hello+world+%26+more"

Note: URLEncoder encodes spaces as + (form encoding). If you need RFC 3986-style %20 for spaces, replace afterward:

String encoded = URLEncoder.encode(value, StandardCharsets.UTF_8)
    .replace("+", "%20");

For building complete URLs, use java.net.URI or UriComponentsBuilder from Spring, which handle encoding more holistically.

URL Encoding in C# and .NET

For query parameter values, use Uri.EscapeDataString(). It follows RFC 3986 and encodes spaces as %20:

using System;

string value = "hello world & more";
string encoded = Uri.EscapeDataString(value);
// Result: "hello%20world%20%26%20more"

Avoid HttpUtility.UrlEncode() (from System.Web) for new code — it encodes spaces as + and has some quirks. Use Uri.EscapeDataString() for encoding individual values.

For building query strings, System.Web.HttpUtility.ParseQueryString() gives you a NameValueCollection that handles encoding when you call .ToString().

Sell Custom Apparel — We Handle Printing & Free Shipping

URL Encoding in PHP

PHP has two encoding functions with different space handling:

<?php

// urlencode() — spaces become +, for form data
$encoded = urlencode("hello world & more");
// Result: "hello+world+%26+more"

// rawurlencode() — spaces become %20, RFC 3986
$encoded = rawurlencode("hello world & more");
// Result: "hello%20world%20%26%20more"

// http_build_query() — encodes an array as a query string
$params = ['q' => 'coffee & tea', 'limit' => 10];
$query = http_build_query($params);
// Result: "q=coffee+%26+tea&limit=10"

Use rawurlencode() for URL path segments and urlencode() for query parameter values. Use http_build_query() to build a complete query string from an array.

URL Encoding in Bash and Shell Scripts

Bash has no native URL encode function. The cleanest options:

Using curl:

# Encode a string and print it
curl -Gso /dev/null -w '%{url_effective}'   --data-urlencode "q=hello world & more" "http://localhost" | cut -c3-
# Extracts: q=hello%20world%20%26%20more

Using Python (one-liner):

python3 -c "import urllib.parse; print(urllib.parse.quote('hello world & more', safe=''))"
# Result: hello%20world%20%26%20more

Using jq:

echo -n "hello world & more" | jq -Rr @uri
# Result: hello%20world%20%26%20more

The Python one-liner is the most portable — available on any system with Python 3.

URL Encode Without Writing Code

Paste any string into the Mongoose URL Encoder for an instant percent-encoded result. No Java, C#, PHP, or bash required.

Open URL Encoder

Frequently Asked Questions

Why does Java URLEncoder encode ! and * but JavaScript encodeURIComponent doesn't?

They follow different specifications. encodeURIComponent() follows RFC 3986 and treats !, *, ', (, and ) as unreserved characters. Java's URLEncoder was designed for HTML form encoding and encodes those characters.

What is the correct C# function for URL encoding — there seem to be several?

Use Uri.EscapeDataString() for encoding individual values (query parameter values, path segments). It follows RFC 3986 and is the modern .NET standard. Avoid HttpUtility.UrlEncode() and WebUtility.UrlEncode() — they have + for spaces and some legacy quirks.

PHP rawurlencode vs urlencode — which should I use?

Use rawurlencode() for modern code. It follows RFC 3986 and encodes spaces as %20. urlencode() uses HTML form encoding with + for spaces, which is accepted by most servers but less correct for URL paths.

Is there a way to URL encode in bash without Python or curl?

You can do it with a pure bash loop using printf: printf '%s' "your string" | while IFS= read -rn1 char; do printf '%%%02X' "'$char"; done. It works but is slow for long strings. The Python one-liner is faster and cleaner.

Ryan Callahan
Ryan Callahan Lead Software Engineer

Ryan architected the client-side processing engine that powers every tool on WildandFree — ensuring your files never leave your browser.

More articles by Ryan →
Launch Your Own Clothing Brand — No Inventory, No Risk