' Collection of code snippets by Arne Vajhøj 
' (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
﻿Imports System
Imports System.Net
Imports System.Xml

Namespace HandCoded
    Public Class T1
        Public Sub New()
            Me.New(0, "")
        End Sub
        Public Sub New(f1 As Integer, f2 As String)
            Me.f1 = f1
            Me.f2 = f2
        End Sub
        Public Property f1() As Integer
        Public Property f2() As String
    End Class
    Public Class Program
        Private Shared Function [Call](url As String, hdrs As Tuple(Of String, String)(), msg As XmlDocument) As XmlDocument
            Dim xmlin As String = msg.OuterXml
            Dim wc As New WebClient()
            For Each hdr As Tuple(Of String, String) In hdrs
                wc.Headers.Add(hdr.Item1, hdr.Item2)
            Next
            Dim xmlout As String = wc.UploadString(url, xmlin)
            wc.Dispose()
            Dim res As New XmlDocument()
            res.LoadXml(xmlout)
            Return res
        End Function
        Private Shared Function GetEnvelope() As XmlDocument
            Dim req As New XmlDocument()
            req.AppendChild(req.CreateElement("soap", "Envelope", "http://schemas.xmlsoap.org/soap/envelope/"))
            req.DocumentElement.AppendChild(req.CreateElement("soap", "Body", "http://schemas.xmlsoap.org/soap/envelope/"))
            Return req
        End Function
        Private Shared Function GetHeaders(action As String, actionprefix As String, useaction As Boolean) As Tuple(Of String, String)()
            If useaction Then
                Return New Tuple(Of String, String)() {Tuple.Create("Content-Type", "text/xml"), Tuple.Create("SOAPAction", "http://soap.ws/" & actionprefix & action)}
            Else
                Return New Tuple(Of String, String)() {Tuple.Create("Content-Type", "text/xml")}
            End If
        End Function
        Public Shared Sub Test(url As String, actionprefix As String, paramns As String, fldns As String, useaction As Boolean, usenames As Boolean)
            Dim req As XmlDocument
            Dim operation As XmlElement
            Dim resp As XmlDocument
            ' getOne
            req = GetEnvelope()
            operation = req.CreateElement("getOne", "http://soap.ws")
            Dim f1getone As XmlElement = req.CreateElement(If(usenames, "f1", "arg0"), paramns)
            f1getone.AppendChild(req.CreateTextNode(123.ToString()))
            operation.AppendChild(f1getone)
            req.DocumentElement.LastChild.AppendChild(operation)
            resp = [Call](url, GetHeaders("getOne", actionprefix, useaction), req)
            Dim xo As XmlElement = DirectCast(resp.DocumentElement.LastChild.FirstChild.FirstChild, XmlElement)
            Dim nsmgetone As New XmlNamespaceManager(resp.NameTable)
            nsmgetone.AddNamespace("tns", fldns)
            Dim o As New T1(Integer.Parse(xo.SelectSingleNode("tns:f1/text()", nsmgetone).Value), xo.SelectSingleNode("tns:f2/text()", nsmgetone).Value)
            Console.WriteLine(o.f1 & " " & o.f2)
            ' getAll
            req = GetEnvelope()
            operation = req.CreateElement("getAll", "http://soap.ws")
            req.DocumentElement.LastChild.AppendChild(operation)
            resp = [Call](url, GetHeaders("getAll", actionprefix, useaction), req)
            Dim xall As XmlElement = DirectCast(resp.DocumentElement.LastChild.FirstChild.FirstChild, XmlElement)
            Dim nsmgetall As New XmlNamespaceManager(resp.NameTable)
            nsmgetall.AddNamespace("tns", fldns)
            For Each xone As XmlElement In xall.ChildNodes
                Dim one As New T1(Integer.Parse(xone.SelectSingleNode("tns:f1/text()", nsmgetall).Value), xone.SelectSingleNode("tns:f2/text()", nsmgetall).Value)
                Console.WriteLine(one.f1 & " " & one.f2)
            Next
            ' save
            req = GetEnvelope()
            operation = req.CreateElement("save", "http://soap.ws")
            Dim f1save As XmlElement = req.CreateElement(If(usenames, "f1", "arg0"), paramns)
            f1save.AppendChild(req.CreateTextNode(123.ToString()))
            operation.AppendChild(f1save)
            Dim f2save As XmlElement = req.CreateElement(If(usenames, "f2", "arg1"), paramns)
            f2save.AppendChild(req.CreateTextNode("ABC"))
            operation.AppendChild(f2save)
            req.DocumentElement.LastChild.AppendChild(operation)
            resp = [Call](url, GetHeaders("save", actionprefix, useaction), req)
            Dim xressave As XmlElement = DirectCast(resp.DocumentElement.LastChild.FirstChild.FirstChild, XmlElement)
            Dim ressave As Boolean = Boolean.Parse(xressave.FirstChild.Value)
            Console.WriteLine(ressave)
            ' saveObject
            req = GetEnvelope()
            operation = req.CreateElement("saveObject", "http://soap.ws")
            Dim osaveobj As XmlElement = req.CreateElement(If(usenames, "o", "arg0"), paramns)
            operation.AppendChild(osaveobj)
            Dim f1saveobj As XmlElement = req.CreateElement("f1", fldns)
            f1saveobj.AppendChild(req.CreateTextNode(123.ToString()))
            osaveobj.AppendChild(f1saveobj)
            Dim f2saveobj As XmlElement = req.CreateElement("f2", fldns)
            f2saveobj.AppendChild(req.CreateTextNode("ABC"))
            osaveobj.AppendChild(f2saveobj)
            req.DocumentElement.LastChild.AppendChild(operation)
            resp = [Call](url, GetHeaders("saveObject", actionprefix, useaction), req)
            Dim xressaveobj As XmlElement = DirectCast(resp.DocumentElement.LastChild.FirstChild.FirstChild, XmlElement)
            Dim ressaveobj As Boolean = Boolean.Parse(xressaveobj.FirstChild.Value)
            Console.WriteLine(ressaveobj)
            ' delete
            req = GetEnvelope()
            operation = req.CreateElement("delete", "http://soap.ws")
            Dim f1delete As XmlElement = req.CreateElement(If(usenames, "f1", "arg0"), paramns)
            f1delete.AppendChild(req.CreateTextNode(123.ToString()))
            operation.AppendChild(f1delete)
            req.DocumentElement.LastChild.AppendChild(operation)
            resp = [Call](url, GetHeaders("delete", actionprefix, useaction), req)
            Dim xresdelete As XmlElement = DirectCast(resp.DocumentElement.LastChild.FirstChild.FirstChild, XmlElement)
            Dim resdelete As Boolean = Boolean.Parse(xresdelete.FirstChild.Value)
            Console.WriteLine(resdelete)
        End Sub
        Public Shared Sub TestWithHandler(url As String, actionprefix As String, paramns As String, fldns As String, useaction As Boolean, usenames As Boolean)
            Try
                Test(url, actionprefix, paramns, fldns, useaction, usenames)
            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try
        End Sub
        Public Shared Sub Main(args As String())
            TestWithHandler("http://localhost:8080/axis/services/TestService", "", "http://soap.ws", "", True, False)
            TestWithHandler("http://localhost:8080/wssoap/Test", "", "", "", False, False)
            TestWithHandler("http://localhost/TestService.asmx", "", "http://soap.ws", "http://soap.ws", True, True)
            TestWithHandler("http://localhost/Test.svc", "Test/", "http://soap.ws", "http://soap.ws", True, True)
            TestWithHandler("http://localhost/TestAlt.svc", "Test/", "", "http://soap.ws", True, True)
        End Sub
    End Class
End Namespace
