如何从查询字符串中获取原始字节
how to get raw bytes from querystring
是否有可能从查询字符串中获取原始字节?
我有 windows-1250 和 url 编码字节并且 Request.QueryString["xx"] 是字符串(内部编码为 utf-16)
我需要字节才能将其从 windows-1250 转换为 utf-8
LINQpad 示例如下:
void Main()
{
var txt="p%E1lava"; // this and next line simulates Request.QueryString["txt"]
txt = HttpUtility.UrlDecode(txt);
Console.WriteLine(txt);
Encoding wind1250 = Encoding.GetEncoding(1250);
Encoding utf8 = Encoding.UTF8;
byte[] wind1250Bytes = wind1250.GetBytes(txt); // here is problem, bytes are internally utf-16
byte[] utf8Bytes = Encoding.Convert(wind1250, utf8, wind1250Bytes);
string utf8String = Encoding.UTF8.GetString(utf8Bytes);
Console.WriteLine(utf8String);
}
我找到了解决方案,需要在 Request.Url.Query 上使用 UrlDecode(指定编码):
var wind1250 = Encoding.GetEncoding(1250);
var querystring = HttpUtility.UrlDecode(Request.Url.Query, wind1250);//;
var qs = HttpUtility.ParseQueryString(querystring);
Response.Write(qs["Where"]);
是否有可能从查询字符串中获取原始字节?
我有 windows-1250 和 url 编码字节并且 Request.QueryString["xx"] 是字符串(内部编码为 utf-16)
我需要字节才能将其从 windows-1250 转换为 utf-8
LINQpad 示例如下:
void Main()
{
var txt="p%E1lava"; // this and next line simulates Request.QueryString["txt"]
txt = HttpUtility.UrlDecode(txt);
Console.WriteLine(txt);
Encoding wind1250 = Encoding.GetEncoding(1250);
Encoding utf8 = Encoding.UTF8;
byte[] wind1250Bytes = wind1250.GetBytes(txt); // here is problem, bytes are internally utf-16
byte[] utf8Bytes = Encoding.Convert(wind1250, utf8, wind1250Bytes);
string utf8String = Encoding.UTF8.GetString(utf8Bytes);
Console.WriteLine(utf8String);
}
我找到了解决方案,需要在 Request.Url.Query 上使用 UrlDecode(指定编码):
var wind1250 = Encoding.GetEncoding(1250);
var querystring = HttpUtility.UrlDecode(Request.Url.Query, wind1250);//;
var qs = HttpUtility.ParseQueryString(querystring);
Response.Write(qs["Where"]);