如何从查询字符串中获取原始字节

时间:2015-03-31 14:34:37

标签: c# asp.net .net asp.net-mvc encoding

是否有可能从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);  
}

1 个答案:

答案 0 :(得分:1)

我找到了解决方案,需要在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"]);