我应该使用Base64编码还是byte []来返回.NET Web服务中的二进制数据

时间:2023-02-03 07:06:32

I have a simple question. Should I return a byte-array or simply base64 encode my binary data, when exposing it through a web service in .NET? Is there any pros/cons with either method, which is mostly supported etc.

我有一个简单的问题。当我通过.NET中的Web服务公开它时,我应该返回一个字节数组还是简单地对我的二进制数据进行base64编码?是否有任何方法的优点/缺点,大多数支持等。

2 个解决方案

#1


10  

Use byte array. Over a SOAP protocol this byte array will automatically be serialized using base64 encoding. Your web service will be also more descriptive if you use a byte array. Imagine someone who wants to consume your web service and looking at a method whose signature looks like this:

使用字节数组。通过SOAP协议,该字节数组将使用base64编码自动序列化。如果使用字节数组,您的Web服务也将更具描述性。想象一下,有人想要使用您的Web服务并查看其签名如下所示的方法:

string GetBinaryImageFromDatabase();

He might wonder why does this method returns a string and what am I going to do with this string, while if it returns a byte array it is much more clear.

他可能想知道为什么这个方法返回一个字符串以及我将如何处理这个字符串,而如果它返回一个字节数组则更清楚。

I've seen web service method that look like this:

我见过这样的web服务方法:

string SaveCustomer(string customer);

Guess what the customer and the return types were: they were some proprietary XML. There's absolutely no sense of using SOAP if people reinvent protocols over it.

猜猜客户和退货类型是什么:它们是一些专有的XML。如果人们重新使用SOAP,那么绝对没有使用SOAP的意识。

#2


0  

One consideration is that a Base64 encoded string enables you to use the "Test Form" to help debug any issues, etc. The Byte array is a complex type and therefore the test form will not work.

一个考虑因素是Base64编码的字符串使您可以使用“测试表单”来帮助调试任何问题,等等。字节数组是一个复杂的类型,因此测试表格不起作用。

#1


10  

Use byte array. Over a SOAP protocol this byte array will automatically be serialized using base64 encoding. Your web service will be also more descriptive if you use a byte array. Imagine someone who wants to consume your web service and looking at a method whose signature looks like this:

使用字节数组。通过SOAP协议,该字节数组将使用base64编码自动序列化。如果使用字节数组,您的Web服务也将更具描述性。想象一下,有人想要使用您的Web服务并查看其签名如下所示的方法:

string GetBinaryImageFromDatabase();

He might wonder why does this method returns a string and what am I going to do with this string, while if it returns a byte array it is much more clear.

他可能想知道为什么这个方法返回一个字符串以及我将如何处理这个字符串,而如果它返回一个字节数组则更清楚。

I've seen web service method that look like this:

我见过这样的web服务方法:

string SaveCustomer(string customer);

Guess what the customer and the return types were: they were some proprietary XML. There's absolutely no sense of using SOAP if people reinvent protocols over it.

猜猜客户和退货类型是什么:它们是一些专有的XML。如果人们重新使用SOAP,那么绝对没有使用SOAP的意识。

#2


0  

One consideration is that a Base64 encoded string enables you to use the "Test Form" to help debug any issues, etc. The Byte array is a complex type and therefore the test form will not work.

一个考虑因素是Base64编码的字符串使您可以使用“测试表单”来帮助调试任何问题,等等。字节数组是一个复杂的类型,因此测试表格不起作用。