使用HttpWebRequest发布非英语数据

时间:2010-12-22 12:58:36

标签: c# html localization character-encoding httpwebrequest

该网站采用希伯来语,采用Windows-1255编码。 使用Firefox的Tamer Data扩展,我看到其中一个Post参数是:
+++++++++%E4%FA%E7%E1%F8 +++++++++
使用this表我把它翻译成希伯来语:

++++++++++++++++++++++++++++++++++++++++++++++++++++++

现在,我想将其作为发布数据发送:

 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);

3 个答案:

答案 0 :(得分:2)

我认为最安全的方式是用希伯来语输入,并使用

Uri.EscapeDataString(paramValue)以逃避角色

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

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

所以在这种情况下,使用ASCII是安全的。

答案 1 :(得分:0)

如果使用内容类型"application/x-www-form-urlencoded",则必须以URL格式对参数进行编码(即,URL中无效的每个字节必须为% +十六进制代码)。在解码期间+成为空格,顺便说一句。

要获取byteArray,请使用ASCII编码(该数组中的所有字符都有一个代码点<128)。在这种情况下,UTF-8也会起作用,因为在这个范围内,它们匹配,但不应该是你的意图。

答案 2 :(得分:0)

@dan你是对的, 它的作品非常好

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

 Uri.UnescapeDataString(scaprstring);
相关问题