2015-03-31 128 views
0

是否有可能從查詢字符串中獲取原始字節?如何從查詢字符串中獲取原始字節

我有有窗戶-1250和url編碼的字節和的Request.QueryString [「XX」]是字符串(在UTF-16內部編碼)

我需要字節,以便能夠將其從轉換窗口-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

我發現溶液,需要在使用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"]); 
相關問題