2

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);
Shekhar_Pro
  • 18,056
  • 9
  • 55
  • 79
jullin
  • 633
  • 2
  • 12
  • 21

3 Answers3

2

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.

Dan
  • 2,321
  • 3
  • 27
  • 40
0

@dan you are correct, its works very well

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

 Uri.UnescapeDataString(scaprstring);
Amila Thennakoon
  • 419
  • 1
  • 5
  • 15
0

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.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • I am still confused, in my case param should be: param="%E4%FA%E7%E1%F8" and I should use ASCII instead of UTF8? – jullin Dec 22 '10 at 13:10
  • Yes, exactly. If you set the content type, you also specify the encoding of the content. If you fail to properly encode the content (it's just a byte array), then the receiver will fail to decode it. It would be more simple if you could pass a unicode string to the framework and have it figure out the encoding using the same rules the other side will use for decoding ... – Aaron Digulla Dec 25 '10 at 23:45