seth
seth

Reputation: 1389

SOAP with Attachments in 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.

Upvotes: 6

Views: 2784

Answers (3)

Tom Studee
Tom Studee

Reputation: 10452

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)

Upvotes: 1

Carl Onager
Carl Onager

Reputation: 4122

As far as I can tell from here: http://www.w3.org/TR/SOAP-attachments the attachments need to go inline within the body of the message which means that you need to combine the answers from Kevin and Tom to build your message.

I did note that SOAP with attachments appears to only be a submission and not a standard, moreover it is rather dated.

Assuming that you have to use the web service 'as is' I would try and get some additional data or examples from the service creator/supplier if you can.

Upvotes: 0

Kevinc
Kevinc

Reputation: 166

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, ..

Upvotes: 1

Related Questions