2010-12-22 22 views
2

該網站是用Windows-1255編碼的希伯來語版本。 使用Firefox的馴數據擴展,我看到帖子的參數之一是:
+++++++++%E4%FA%E7%E1%F8 +++++++++
使用this表我把它翻譯爲希伯來語:在HttpWebRequest上發佈非英文數據

+++++++++התחבר+++++++++

現在,我想將它作爲發送POST數據:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 

string param =;// What should I set here? should i type in hebrew? should i conver it to hex?   
//Should I type +++++++++%E4%FA%E7%E1%F8+++++++++ or maybe %E4%FA%E7%E1%F8? 
string postData = string.Format({0}={1}",param,value); 
byte[] byteArray = Encoding.UTF8.GetBytes(postData); //what encoding to use?   
request.ContentType = "application/x-www-form-urlencoded";   
request.ContentLength = byteArray.Length; 

Stream dataStream = request.GetRequestStream(); 
dataStream.Write(byteArray, 0, byteArray.Length); 

回答

0

如果您使用的內容類型"application/x-www-form-urlencoded",那麼你必須以URL形式對參數進行編碼(即每一個無效的字節在URL中必須是% +十六進制代碼)。在解碼期間,+成爲空間,順便說一句。

要獲取byteArray,請使用ASCII編碼(該數組中的所有字符將有一個代碼點< 128)。在這種情況下,UTF-8也可以工作,因爲在這個範圍內,它們匹配,但它不會符合你的意圖。在我看來

+0

我仍然困惑,在我的情況PARAM應該是:PARAM = 「%E4%FA%E7%E1%F8」 我應該使用ASCII而不是UTF8? – jullin 2010-12-22 13:10:42

2

最保險的辦法是在希伯來語中鍵入,並使用

Uri.EscapeDataString(paramValue)逃脫字符

string param = 'paramName';   
string value = 'התחבר'; 

string postData = string.Format("{0}={1}", param, Uri.EscapeDataString(value)); 

所以在這種情況下,這將是安全的使用ASCII。

0

@丹你是正確的, 它的作品非常好

string scaprstring= Uri.EscapeDataString("unicode string")` 

Uri.UnescapeDataString(scaprstring); 
相關問題