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

6
  • Why did you create a 1000-byte array and then discard it? Commented Jan 18, 2012 at 14:06
  • @JohnSaunders because i was careless writing the question. I've edited Commented Jan 18, 2012 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. Commented Jan 19, 2012 at 0: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... Commented Jan 19, 2012 at 8:29
  • 1
    @Hellfrost, really? Provide a link and I'll +1 your question. Commented Jan 19, 2012 at 17:52

2 Answers 2

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)

Sign up to request clarification or add additional context in comments.

2 Comments

here too: 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...
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.
0

Streams read and write binary. GetRequestStream returns a Stream.

5 Comments

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 ?
Streams just read and write binary. They're the lowest-level I/O abstraction. They don't convert anything.
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
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.
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...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.