Android Question Passing xml in to web service

Alwinl

Member
Licensed User
Longtime User
Hi,
I am using HTPUtils2 to communicate with a php web service, all is working great when passing in simple string args like this:

job1.PostString("http://srv/web/service.php", "arg1=aaa&arg2=bbb")

The problem is that I need to pass in an xml structure for one of the arguments (including all the usual </> tags and crlf's) - any ideas?
 

imbault

Well-Known Member
Licensed User
Longtime User
You can construct a string variable :

Here the variables :
Ws_Url is the URL of your web service
WS_Usertest the name of the service
WS_Labo, WS_sID parameters

B4X:
Dim jobtest As HttpJob
Dim endPoint As String
Dim requestSoapXML As String
ProgressDialogShow("login...")

   
    endPoint = WS_Url
   
    requestSoapXML = _
        "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>" & _
          "<soap:Body>" & _
            "<" & WS_Usertest & " xmlns='http://patrick.imbault.org/'>" & _
               "<sLab>" & WS_Labo & "</sLab>" & _
              "<sId>" & WS_sID & "</sId>" & _
            "</" & WS_Usertest & ">" & _
          "</soap:Body>" & _
        "</soap:Envelope>"
    Log(requestSoapXML )
   
    jobtest.Initialize("jobtestuser",Me)
    jobtest.PostString ( endPoint, requestSoapXML)
    jobtest.GetRequest.SetHeader("SOAPAction", "http://patrick.imbault.org/"  & WS_Usertest)
    jobtest.GetRequest.SetHeader("Content-Type", "text/xml; charset=utf-8")
    jobtest.GetRequest.Timeout = 15000
 
Upvote 0

Alwinl

Member
Licensed User
Longtime User
Thanks for the reply - lets say its not soap though. It is just a bunch of xml I want to pass as an argument to the web service.
Actually, the question may have more to do with escaping, say

dim arg2 as string
arg2 = "aa=b&b"
job1.PostString("http://srv/web/service.php", "arg1=aaa&" & arg2)

How would I escape arg2 to use it in this fashion?
 
Upvote 0

MotoMusher

Active Member
Licensed User
Longtime User
Depending on the webservice, they may accept the post as an attachment which will make your life much easier. The restful services I have dealt with all do. Write your xml string to a file and save it, then post the attachment. Here's what I do:

B4X:
Dim j as httpjob
j.PostFile(MyURL, MyFilePath, "MyFile.xml")
 
Upvote 0

Alwinl

Member
Licensed User
Longtime User
Thanks MotoMusher, uploading an xml file may be an option although I'd like to do everything in memory.

realblue - beside passing xml I still have the issue of how to escape, assuming I want to pass the value "aa&b=b" as below, surely the & in my data would conflict with the html escaping?

arg2 = "aa&b=b"
job1.PostString("http://srv/web/service.php", "arg1=aaa&arg2=" & arg2)

Also - am I right in assuming that although the arguments are built in a similar way to a GET, it is doing a POST ie. there is no limit on the length of data
 
Upvote 0

Alwinl

Member
Licensed User
Longtime User
Thanks Erel, I have tested and it appears that escaping is required. With xml crlf's cause trouble and with simple text, embedded '&' or '=' are problematic. I don't know the internals of POST payloads but I think the issue is that with PostString the args are passed in a GET-style string, and as per my example above you would get unexpected behaviour without escaping.

Further tests have shown that using the URL type escaping ie. using '%20%' for spaces and '%0D%%0A%' for crlf. Seems to work better than the ampersand method eg. '&lt'
 
Upvote 0

Alwinl

Member
Licensed User
Longtime User
I have attached my routines here if anyone is interested in using them, one escapes xml and the other the post string. These seem to work just fine, usage is as follows:

xmldata = "<var1>" & EscapeXML("Greater>Less<") & "</var1>"
strdata = "This is a problem % & ="
post = "data1=" & EscapeURL(strdata) & "&data2=" & EscapeURL(xmldata)
job1.PostString("http://.......", post)

Please let me know if I am doing this the wrong way but I can't see any other way of achieving the code above without escaping
 

Attachments

  • Escape.bas
    1.5 KB · Views: 186
Upvote 0

Alwinl

Member
Licensed User
Longtime User
Thanks Erel, the xml builder looks great. Assuming it does escape the xml data as I have done, you'd still need to use my 'EscapeURL' as per the snippit above for the post string.
 
Upvote 0
Top