You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node.js中对应C# HttpUtility.UrlEncode的等效方法及编码差异问题

Node.js Equivalent of C#'s HttpUtility.UrlEncode for Byte Arrays

I ran into this exact mismatch before—C#'s HttpUtility.UrlEncode and Node.js's standard URI tools handle encoding rules differently, especially when dealing with non-ASCII bytes and reserved characters like @ or |. Let’s break down why your results don’t line up, and how to replicate the C# behavior in Node.js.

Why the Difference?

  • C#'s HttpUtility.UrlEncode: Takes your byte array, treats it as UTF-8 (by default), and encodes every byte except those matching ASCII letters, numbers, or the special characters *, -, _, .. It outputs lowercase hex codes (e.g., %40 for @, %7c for |).
  • Node.js's encodeURI: Only encodes characters that are strictly forbidden in any part of a URI. It leaves reserved characters like @, |, and $ unencoded, which is why your results diverge.

The Equivalent Node.js Solutions

To get output identical to HttpUtility.UrlEncode(bytes), you have two solid options:

Option 1: Use querystring.escape() (with lowercase hex tweak)

The querystring module’s escape function encodes most of the same characters as HttpUtility.UrlEncode, though it outputs uppercase hex codes. We’ll add a quick replace to convert those to lowercase to match C#'s output:

const querystring = require('querystring');

// Your sample byte array
const bytes = [86,63,228,90,223,138,78,142,224,198,114,68,205,42,206,252,233,190,184,160,199,64,124,176,104,189,106,187,103,105,141,45,16,210,219,189,63,56,150,150];

// Convert bytes to UTF-8 string
const utf8Str = Buffer.from(bytes).toString('utf8');

// Encode and convert hex codes to lowercase
const encoded = querystring.escape(utf8Str).replace(/%[0-9A-F]/g, match => match.toLowerCase());

console.log(encoded); 
// Output: V%3f%e4Z%df%8aN%8e%e0%c6rD%cd*%ce%fc%e9%be%b8%a0%c7%40%7c%b0h%bdj%bbgi%8d-%10%d2%db%bd%3f8%96%96

Option 2: Custom Byte-by-Byte Encoding

If you want to work directly with the byte array (skipping string conversion, which avoids any potential UTF-8 edge cases), you can write a function that mirrors HttpUtility.UrlEncode’s exact logic:

function urlEncodeBytes(bytes) {
  let result = '';
  for (const byte of bytes) {
    // Check if byte is an allowed ASCII character
    if ((byte >= 65 && byte <= 90) || // A-Z
        (byte >= 97 && byte <= 122) || // a-z
        (byte >= 48 && byte <= 57) || // 0-9
        byte === 42 || // *
        byte === 45 || // -
        byte === 46 || // .
        byte === 95) { // _
      result += String.fromCharCode(byte);
    } else {
      // Convert byte to lowercase hex with leading zero if needed
      result += `%${byte.toString(16).padStart(2, '0').toLowerCase()}`;
    }
  }
  return result;
}

// Test with your sample bytes
const encoded = urlEncodeBytes(bytes);
console.log(encoded); 
// Exact match to your C# result

Both approaches will produce the exact encoded string you got from HttpUtility.UrlEncode. The custom function is more direct for byte array input, while the querystring method is great if you’re starting from a UTF-8 string instead.

内容的提问来源于stack exchange,提问作者Lothre1

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:21:45