通过HttpWebRequest的传递二进制数据二进制数、HttpWebRequest

2023-09-03 06:22:13 作者:丈母娘、你发货太慢了

使用.net,我想通过传递一些二进制数据(一些序列化对象)的HttpWebRequest的。

Using .Net, I want to pass some binary data(some serialized objects) through an HttpWebRequest.

我可以只把它的请求流,或者我需要连接code成一个base64字符串?

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);

我是否需要使用Convert.ToBase64String或可我刚刚从HttpWebRequest.GetRequestStream?

Do I need to Use Convert.ToBase64String or can I just write it to the stream from HttpWebRequest.GetRequestStream ?

为后人:

http://tool​​s.ietf.org/html/rfc2616

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

http://www.wireshark.org/

http://www.fiddler2.com

推荐答案

如果你写你的数据通过 HttpWebRequest.GetRequestStream ,你会被发送纯二进制流数据,而无需任何转换为​​base64。你将不得不解析接收端作为二进制流上的数据。

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.

作为一个方面说明,我也总是从BASE64通过网络发送数据时,因为它会增加你的带宽来传输数据引导了。对于每3个字节将其转换为base64会出来4.所以,你有33%的开销为您的所有数据。

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.

编辑:进入一个更深入一点这里Hellfrost。该 HttpWebRequest.GetRequestStream 让你流,后者又可以让您通过该连接发送的二进制数据低级别的访问。如果您正试图将数据发送到web服务器,我建议你寻找到后期数据和的multipart / form-data的。你可以阅读更多关于它在这里:上传文件的HttpWebRequest(的multipart / form-data的)

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)