Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SOAP with Attachments in VB.NET

Tags:

soap

vb.net

I need to use a web service that was provided to me and that uses SOAP with attachments as a means of communication. It also uses WS-Security 1.0 as a form of authencation (user and pass in plain text).

I will be working on VB.NET and my experience with web services is very slim, and goes as far as creating a WCF web service, adding the service reference in another project thus creating a proxy class and using the methods.

I have searched the internet and found that there is no native support for SwA in VB.NET, is this correct? An example on how i can approach the communication and some indication on where i can be more aware of these concepts would be superb.

like image 824
seth Avatar asked May 31 '12 14:05

seth


People also ask

Can we send SOAP messages with attachments?

You can send and receive SOAP messages that include binary data (such as PDF files or JPEG images) as attachments.

What is mime in SOAP?

The MIME multipart mechanism for encapsulation of compound documents can be used to bundle entities related to the SOAP 1.1 message such as attachments. Rules for the usage of URI references to refer to entities bundled within the MIME package are specified.


2 Answers

Why not just put your attachment data into a byte array and send that?

Public Class SoapPackage
    Public Property AttachmentName() As String
        Get
            Return m_AttachmentName
        End Get
        Set
            m_AttachmentName = Value
        End Set
    End Property
    Private m_AttachmentName As String

    ' put your file data in here 
    Public Property AttachmentData() As Byte()
        Get
            Return m_AttachmentData
        End Get
        Set
            m_AttachmentData = Value
        End Set
    End Property
    Private m_AttachmentData As Byte()
End Class

Streaming your file into the byte array with something along these lines (open a stream for the attachment, pass it into this function):

Public Shared Function StreamToByteArray(input As Stream) As Byte()
    Dim buffer As Byte() = New Byte(16 * 1024 - 1) {}
    Using ms As New MemoryStream()
        Dim read As Integer
        While (InlineAssignHelper(read, input.Read(buffer, 0, buffer.Length))) > 0
            ms.Write(buffer, 0, read)
        End While
        Return ms.ToArray()
    End Using
End Function

edit:

Just learned that my StreamToByteArray function can be replaced with simply:

Dim filePath as String = "Path\to\file"
Dim fileBytes as Byte() = System.IO.File.ReadAllBytes(filePath)
like image 38
Tom Studee Avatar answered Nov 15 '22 08:11

Tom Studee


In a recent project of mine I had to communicate in c# with a soap WebService. To deal with that I made my full soapenvelope in a string and send it to the web service with a HttpWebRequest. With the WebRespons you can then read out the response of the webservice and put it back into a string. This string you can convert to a XmlDocument to read out the object that you want.

You can easily make the soapenvelope as you want with a loop (if necessary) and also put the authencation in the header of the soapenvelope (did it this way)

To get the correct soapenvelope syntax of your web service I can advise you to use SOAPUI, a great small application that helped me a lot ! This way you will notice where to put the authencation, ..

like image 173
Kevinc Avatar answered Nov 15 '22 10:11

Kevinc