5

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

http://msdn.microsoft.com/en-us/library/d4cek6cc.aspx

http://www.wireshark.org/

http://www.fiddler2.com

Community
  • 1
  • 1
AK_
  • 7,981
  • 7
  • 46
  • 78
  • Why did you create a 1000-byte array and then discard it? – John Saunders Jan 18 '12 at 14:06
  • @JohnSaunders because i was careless writing the question. I've edited – AK_ Jan 18 '12 at 14:25
  • Does "some serialized objects" mean `new BinaryFormatter().Serialize(...)`? Do not store such data or send it to another machine. The .NET binary serialization format changes between hardware and .NET version. – Dour High Arch Jan 19 '12 at 00:50
  • @DourHighArch that's only true for .NET 1.1 , Read the documentation in the MSDN about binary serialization, it should be upgrade-able. other then that, that isn't what I'm sending... – AK_ Jan 19 '12 at 08:29
  • 1
    @Hellfrost, really? Provide a link and I'll +1 your question. – Dour High Arch Jan 19 '12 at 17:52
  • 1.http://msdn.microsoft.com/en-us/library/ms229752(v=vs.80).aspx 2.http://www.codeguru.com/csharp/.net/net_general/netframeworkclasses/article.php/c9297 – AK_ Jan 19 '12 at 18:02

2 Answers2

5

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)

Community
  • 1
  • 1
DanielG
  • 131
  • 11
  • here too: http://msdn.microsoft.com/en-us/library/d4cek6cc.aspx since HTTP is intended for text this means one need to convert his data into a textual representation... – AK_ Jan 18 '12 at 16:04
  • The HTTP protocol is majorly used on websites to transfer text data, but at the point that it is transmitted to you, the data is just binary data. I updated my answer with more detail. – DanielG Jan 18 '12 at 16:54
0

Streams read and write binary. GetRequestStream returns a Stream.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • Will writing my array to that specific stream, will just end up writing the buffer into the network stream, or will it be converted to base64 ? – AK_ Jan 18 '12 at 14:27
  • Streams just read and write binary. They're the lowest-level I/O abstraction. They don't convert anything. – John Saunders Jan 18 '12 at 15:04
  • That's nonsense... a LOT of stream object manipulate data transfered to them e.g. Compression... Since raw binary data like the value 0 (not the text) is problematic with HTTP, I would expect it to be converted – AK_ Jan 18 '12 at 16:03
  • The Stream.Write interface takes a byte buffer. It is up to the stream implementation to decide what it wants to do with it - whether it wants to transform it in some manner, or send it as is. So yes, a particular stream implementation might transform data. However, the stream that you get back from GetRequestStream() does not. – feroze Jan 18 '12 at 23:02
  • I needed some idea of what will pass on the wire, and i hoped doing that, without reading the HTTP RFC and sitting for an evening or two with Wireshark... – AK_ Jan 19 '12 at 08:31