Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
559 views
in Technique[技术] by (71.8m points)

c# - Passing binary data through HttpWebRequest

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...