Do Not Construct URLs with String Concatenation ― Safely Building Japanese Queries with PowerShell + UriBuilder

プログラミング・Web開発カテゴリを表すパンダのイラスト Programming / Web Development

About this article
This article was created using an automated generation workflow leveraging generative AI. It reviews .NET’s UriBuilder and Uri.EscapeDataString specifications and organizes tips on avoiding string concatenation when building URLs.

Verification Status: 📘 Official API Confirmed · PowerShell Sample Implemented · Hardware Not Verified

Do Not Construct URLs with String Concatenation ― Safely Building Japanese Queries with PowerShell + UriBuilder

When creating API URLs, relying solely on "?q=" + $keyword + "&page=" + $page and string concatenation easily breaks the moment Japanese text, spaces, &, =, or similar characters get mixed in.

Using .NET’s UriBuilder and Uri.EscapeDataString from PowerShell allows you to separate processing responsibilities.

Example: Using “Tokyo Station” as a Search Parameter

# [ordered] is a hashtable that preserves the key sort order.


# Used to make URL logs easier to view by keeping them in the same order every time.

$params = [ordered]@{
    q    = "東京 駅"
    page = 2
}

# URL-encode keys and values one by one.


# The key point is passing only the data portion to EscapeDataString, rather than the entire URL.

$query = (
    $params.GetEnumerator() | ForEach-Object {
        $key = [Uri]::EscapeDataString([string]$_.Key)
        $value = [Uri]::EscapeDataString([string]$_.Value)
        "$key=$value"
    }
) -join "&"

# UriBuilder is a .NET class for constructing URLs structurally.

$builder = New-Object System.UriBuilder("https://example.com/search")
$builder.Query = $query

# Display the completed absolute URL.

$builder.Uri.AbsoluteUri

Japanese characters and spaces are escaped into a format that can be handled in URLs.

Use EscapeDataString for “Values”

Instead of escaping the entire URL wholesale, process it per query key or value. If you convert characters like https:// or /, the URL structure itself will break.

flowchart LR
    A[Base URL] --> C[UriBuilder]
    B[query values] --> D[EscapeDataString]
    D --> C
    C --> E[完成URL]

What Can It Be Used For?

  • REST API calls

  • Search URL generation

  • URLs containing Japanese filenames

  • Bulk generation of links with parameters

It is short code, but it provides a significantly safer implementation than “sloppily concatenating strings.”

GitHub Sample

Official / Primary Sources

Document information

Article title
Do Not Construct URLs with String Concatenation ― Safely Building Japanese Queries with PowerShell + UriBuilder
Published
Updated
Source
https://papanda925.com/?p=15249&lang=en

License: Text and original figures for which this site holds the relevant rights are available under CC BY 4.0 , unless otherwise noted. This article may include content created or edited with generative AI. If code has a separate license notice or a linked GitHub repository license, that license takes precedence for the code. Quotations, third-party materials, images, and trademarks are excluded from this license. Usage policy

Copied title and URL