使用 HttpWebRequest 发布非英语数据

发布于 2024-10-09 02:11:37 字数 932 浏览 6 评论 0原文

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

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

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

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

The web site is in hebrew with Windows-1255 encoding.
Using Firefox's Tamer Data extension, i see that one of the Post parameters is:
+++++++++%E4%FA%E7%E1%F8+++++++++
using this table I translated it to hebrew:

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

Now, I want to send it as post data:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

此岸叶落 2024-10-16 02:11:37

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

Uri.EscapeDataString(paramValue) 转义字符

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

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

因此在这种情况下,使用 ASCII 是安全的。

Safest way in my opinion is type it in hebrew, and use

Uri.EscapeDataString(paramValue) to escape the characters

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

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

So in this case, it would be safe to use ASCII.

醉殇 2024-10-16 02:11:37

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

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

If you use the content type "application/x-www-form-urlencoded", then you must encode the parameters in URL form (i.e. every byte that's not valid in a URL must be % + the hex code). The + becomes a space during decoding, btw.

To get the byteArray, use ASCII encoding (all characters in that array will have a code point < 128). In this case, UTF-8 would also work because in this range, they match, but it would not should your intention.

叹梦 2024-10-16 02:11:37

@dan你是对的,
它的效果很好

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

 Uri.UnescapeDataString(scaprstring);

@dan you are correct,
its works very well

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

 Uri.UnescapeDataString(scaprstring);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文