' Collection of code snippets by Arne Vajhøj 
' (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
program diyclient

external sub test(string, integer)

call test("localhost", 8001)
call test("localhost", 8002)

end program
!
sub test(string host, integer port)

external sub test1(string, integer)
external sub test2(string, integer)

print "http://" + host + ":" + str$(port) + "/ (Basic)"
call test1(host, port)
call test2(host, port)
call test3(host, port)
call test4(host, port)
call test5(host, port)
call test6(host, port)

end sub
!
sub test1(string host, integer port)

declare string req
map (resp) string resp = 1000
declare integer ctx, resplen, n, ixstart, ixend, v

external integer function vms_http_post(string, integer, string, string, string, string)
external integer function vms_http_recv_all(integer, string, integer)
external sub vms_http_close(integer)
external integer function grab(string, integer, integer, string, integer)

req = "<methodCall><methodName>Test.getInt</methodName><params></params></methodCall>"
ctx = vms_http_post(host, port, "/", "text/xml", "text/xml", req)
n = vms_http_recv_all(ctx, resp, resplen)
call vms_http_close(ctx)
ixstart = 1
ixend = len(resp)
if grab(mid$(resp, 1, resplen), ixstart, ixend, "value", 1) <> 0 then
    if grab(mid$(resp, 1, resplen), ixstart, ixend, "i4", 1) <> 0 then
        v = integer(mid$(resp, ixstart, ixend - ixstart))
        print v
    else
        if grab(mid$(resp, 1, resplen), ixstart, ixend, "int", 1) <> 0 then
            v = integer(mid$(resp, ixstart, ixend - ixstart))
            print v
        end if
    end if
end if

end sub
!
sub test2(string host, integer port)

declare string req, s
map (resp) string resp = 1000
declare integer ctx, resplen, n, ixstart, ixend

external integer function vms_http_post(string, integer, string, string, string, string)
external integer function vms_http_recv_all(integer, string, integer)
external sub vms_http_close(integer)
external integer function grab(string, integer, integer, string, integer)

req = "<methodCall><methodName>Test.getString</methodName><params></params></methodCall>"
ctx = vms_http_post(host, port, "/", "text/xml", "text/xml", req)
n = vms_http_recv_all(ctx, resp, resplen)
call vms_http_close(ctx)
ixstart = 1
ixend = len(resp)
if grab(mid$(resp, 1, resplen), ixstart, ixend, "value", 1) <> 0 then
    if grab(mid$(resp, 1, resplen), ixstart, ixend, "string", 1) <> 0 then
        s = mid$(resp, ixstart, ixend - ixstart)
        print s
    else
        s = mid$(resp, ixstart, ixend - ixstart)
        print s
    end if
end if

end sub
!
sub test3(string host, integer port)

declare string req, prtlin
map (resp) string resp = 1000
declare integer ctx, resplen, n, ixstart, ixend, ixstart2, ixend2, ixstart3, ixend3
declare integer a(1000)

external integer function vms_http_post(string, integer, string, string, string, string)
external integer function vms_http_recv_all(integer, string, integer)
external sub vms_http_close(integer)
external integer function grab(string, integer, integer, string, integer)

req = "<methodCall><methodName>Test.getListOfInts</methodName><params></params></methodCall>"
ctx = vms_http_post(host, port, "/", "text/xml", "text/xml", req)
n = vms_http_recv_all(ctx, resp, resplen)
call vms_http_close(ctx)
ixstart = 1
ixend = len(resp)
if grab(mid$(resp, 1, resplen), ixstart, ixend, "value", 1) then
    if grab(mid$(resp, 1, resplen), ixstart, ixend, "array", 1) then
        ixstart2 = ixstart
        ixend2 = 0
        n = 0
        while grab(mid$(resp, 1, resplen), ixstart2, ixend2, "value", 0)
            ixstart3 = ixstart2
            ixend3 = ixend2
            if grab(mid$(resp, 1, resplen), ixstart3, ixend3, "i4", 1) then
                n = n + 1
                a(n) = integer(mid$(resp, ixstart3, ixend3 - ixstart3))
            else
                if grab(mid$(resp, 1, resplen), ixstart3, ixend3, "int", 1) then
                    n = n + 1
                    a(n) = integer(mid$(resp, ixstart3, ixend3 - ixstart3))
                end if
            end if
            ixstart2 = ixend2
            ixend2 = ixend
        next
        prtlin = "["
        for i = 1 to n
            if i > 1 then
                prtlin = prtlin + ","
            end if
            prtlin = prtlin + str$(a(i))
        next i
        prtlin = prtlin + "]"
        print prtlin
    end if
end if

end sub
!
sub test4(string host, integer port)

declare string req, prtlin
map (resp) string resp = 1000
declare integer ctx, resplen, n, ixstart, ixend, ixstart2, ixend2, ixstart3, ixend3
declare string a(1000)

external integer function vms_http_post(string, integer, string, string, string, string)
external integer function vms_http_recv_all(integer, string, integer)
external sub vms_http_close(integer)
external integer function grab(string, integer, integer, string, integer)

req = "<methodCall><methodName>Test.getListOfStrings</methodName><params></params></methodCall>"
ctx = vms_http_post(host, port, "/", "text/xml", "text/xml", req)
n = vms_http_recv_all(ctx, resp, resplen)
call vms_http_close(ctx)
ixstart = 1
ixend = len(resp)
if grab(mid$(resp, 1, resplen), ixstart, ixend, "value", 1) then
    if grab(mid$(resp, 1, resplen), ixstart, ixend, "array", 1) then
        ixstart2 = ixstart
        ixend2 = 0
        n = 0
        while grab(mid$(resp, 1, resplen), ixstart2, ixend2, "value", 0)
            ixstart3 = ixstart2
            ixend3 = ixend2
            if grab(mid$(resp, 1, resplen), ixstart3, ixend3, "string", 1) then
                n = n + 1
                a(n) = mid$(resp, ixstart3, ixend3 - ixstart3)
            else
                n = n + 1
                a(n) = mid$(resp, ixstart3, ixend3 - ixstart3)
            end if
            ixstart2 = ixend2
            ixend2 = ixend
        next
        prtlin = "["
        for i = 1 to n
            if i > 1 then
                prtlin = prtlin + ","
            end if
            prtlin = prtlin + a(i)
        next i
        prtlin = prtlin + "]"
        print prtlin
    end if
end if

end sub
!
sub test5(string host, integer port)

declare string req
map (resp) string resp = 1000
declare integer ctx, resplen, n, ixstart, ixend, v1, v2, v3

external integer function vms_http_post(string, integer, string, string, string, string)
external integer function vms_http_recv_all(integer, string, integer)
external sub vms_http_close(integer)
external integer function grab(string, integer, integer, string, integer)

v1 = 123
v2 = 456
req = "<methodCall><methodName>Test.add</methodName><params><param><value><i4>" + str$(v1) + "</i4></value></param><param><value><i4>" + str$(v2) + "</i4></value></param></params></methodCall>"
ctx = vms_http_post(host, port, "/", "text/xml", "text/xml", req)
n = vms_http_recv_all(ctx, resp, resplen)
call vms_http_close(ctx)
ixstart = 1
ixend = len(resp)
if grab(mid$(resp, 1, resplen), ixstart, ixend, "value", 1) <> 0 then
    if grab(mid$(resp, 1, resplen), ixstart, ixend, "i4", 1) <> 0 then
        v3 = integer(mid$(resp, ixstart, ixend - ixstart))
        print v3
    else
        if grab(mid$(resp, 1, resplen), ixstart, ixend, "int", 1) <> 0 then
            v3 = integer(mid$(resp, ixstart, ixend - ixstart))
            print v3
        end if
    end if
end if

end sub
!
sub test6(string host, integer port)

declare string req, s1, s2, s3
map (resp) string resp = 1000
declare integer ctx, resplen, n, ixstart, ixend

external integer function vms_http_post(string, integer, string, string, string, string)
external integer function vms_http_recv_all(integer, string, integer)
external sub vms_http_close(integer)
external integer function grab(string, integer, integer, string, integer)

s1 = "ABC"
s2 = "DEF"
req = "<methodCall><methodName>Test.concat</methodName><params><param><value><string>" + s1 + "</string></value></param><param><value><string>" + s2 + "</string></value></param></params></methodCall>"
ctx = vms_http_post(host, port, "/", "text/xml", "text/xml", req)
n = vms_http_recv_all(ctx, resp, resplen)
call vms_http_close(ctx)
ixstart = 1
ixend = len(resp)
if grab(mid$(resp, 1, resplen), ixstart, ixend, "value", 1) <> 0 then
    if grab(mid$(resp, 1, resplen), ixstart, ixend, "string", 1) <> 0 then
        s3 = mid$(resp, ixstart, ixend - ixstart)
        print s3
    else
        s3 = mid$(resp, ixstart, ixend - ixstart)
        print s3
    end if
end if

end sub
!
function integer grab(string xmlstr, integer xstart, integer xend, string tag, integer greedy)

declare string starttag, endtag
declare integer ixstart, ixend

starttag = "<" + tag + ">"
endtag = "</" + tag + ">"
ixstart = xstart
while (ixstart < (len(xmlstr) - len(starttag))) and (mid$(xmlstr, ixstart, len(starttag))) <> starttag
    ixstart = ixstart + 1
next
ixstart = ixstart + len(starttag)
if greedy <> 0 then
    ixend = xend - len(endtag)
    while (ixend > 1) and (mid$(xmlstr, ixend, len(endtag)) <> endtag)
        ixend = ixend - 1
    next
else
    ixend = ixstart
    while (ixend < (len(xmlstr) - len(starttag))) and (mid$(xmlstr, ixend, len(endtag)) <> endtag)
        ixend = ixend + 1
    next
end if
if (ixstart < len(xmlstr) - len(starttag)) and (ixend >= 1) then
    xstart = ixstart
    xend = ixend
    grab = 1
else
    grab = 0
end if

end function
