Using .Net, I want to pass some binary data(some serialized objects) through an HttpWebRequest.
Can I just put it on the request stream, or do I need to encode it into a base64 string?
I mean if i have:
byte[] data = new byte[1000];
GetSomeStrangeData(data);
Do I need to Use Convert.ToBase64String or can I just write it to the stream from HttpWebRequest.GetRequestStream ?
for posterity :
https://www.rfc-editor.org/rfc/rfc2616
If you write your data to a stream via the HttpWebRequest.GetRequestStream
, you will be sending pure binary data without any transformation to base64. You will have to parse the data on the receiving end as a binary stream.
As a side note, I would also always steer away from base64 when sending data across a network because it will increase your bandwidth to transfer the data. For every 3 bytes that you convert to base64 will come out 4. So you have a 33% overhead for all of your data.
EDIT: Going into a little more depth here for Hellfrost. The HttpWebRequest.GetRequestStream
allows you lower level access to the stream which in-turn allows you to send binary data over the connection. If you are trying to send the data to a web-server, I would suggest you looking into post-data and multipart/form-data
. You can read more about it here: Upload files with HTTPWebrequest (multipart/form-data)