Passing binary data through HttpWebRequest

11,713

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)

Share:
11,713
AK_
Author by

AK_

Updated on June 04, 2022

Comments

  • AK_
    AK_ almost 2 years

    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

  • AK_
    AK_ over 12 years
    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 ?
  • John Saunders
    John Saunders over 12 years
    Streams just read and write binary. They're the lowest-level I/O abstraction. They don't convert anything.
  • AK_
    AK_ over 12 years
    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_
    AK_ over 12 years
    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...
  • DanielG
    DanielG over 12 years
    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.
  • feroze
    feroze over 12 years
    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.
  • AK_
    AK_ over 12 years
    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...