Send ASCII string using HTTP Post
Part 1) I have a bi开发者_开发百科tmap/jpeg image. I need to convert this image into an ASCII string (because I need to hash it) How is this conversion to ASCII done?
Part 2) Then, I need to send this ASCII string from client to server using HTTP Post.
From what I understand, I can only send data as a byte array using HTTP Post. How can I send an ASCII string instead of byte[]?
Code in C# would be much appreciated!!
You can convert any binary data to ASCII using base 64 encoding or mime encoding. I expect there are functions to do this in the .net framework. However, I think that's not exactly what you want to do. Here is why:
You can calculate a hash code of binary data, you do not need ASCII data in order to calculate a hash code. There are .net functions for calculating hashed that work with binary data. Also, you can do an http post of binary data, you don't have to use plain ASCII for a post.
One way to convert binary data such as an image into an ASCII string is to use base-64. An example of Base-64 encoding is at: http://arcanecode.com/2007/03/21/encoding-strings-to-base64-in-c/
You need to encode your image in Base64 so to form an ASCII string: http://en.wikipedia.org/wiki/Base64
You can do that in C# like this:
static public string EncodeTo64(string toEncode{
byte[] toEncodeAsBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);
string returnValue = System.Convert.ToBase64String(toEncodeAsBytes);
return returnValue;
}
精彩评论