How to send data for base64Binary when calling asmx webservice

I need to call asmx webservice, which takes an AttachmentData as a parameter. It has a member with type base64Binary.

<s:complexType name="AttachmentData"> <s:sequence> <s:element minOccurs="0" maxOccurs="1" name="FileName" type="s:string" /> <s:element minOccurs="0" maxOccurs="1" name="UploadedUserName" type="s:string" /> <s:element minOccurs="0" maxOccurs="1" name="Attachment" type="s:base64Binary" /> </s:sequence> </s:complexType> 

I send the contents of the file for the Attachment element as follows:

 //read the file contents byte[] buffer = null; try { FileInfo attachment = new FileInfo(filepath); using (FileStream stream = attachment.OpenRead()) { if (stream.Length > 0) { buffer = new byte[stream.Length]; stream.Read(buffer, 0, (int)stream.Length); } } } catch { buffer = null; } //create AttachmentData object WebSrvc.AttachmentData att = new WebSrvc.AttachmentData(); att.FileName = fileName; att.Attachment = buffer; 

Is this the right way to send base64Binary? Do I need to encode the contents of the file to base64 or is it done on the fly? I am trying to see if I unnecessarily inflate the size of the webservice request using the code above.

+4
source share
1 answer

A Base 64 encoding requires 4 bytes in output for every 3 bytes in input, so there is a bit of overhead, but that is not so much.

The good thing about Base 64 encoding is that it only uses printed characters, so it can be easily integrated into the HTTP protocol, which was created to work with human-readable text characters.

In your code example, the byte [] buffer is automatically converted to Base 64 before it is placed on the wire (that is, it is deployed and transmitted over HTTP).

+3
source

Source: https://habr.com/ru/post/1469249/


All Articles