B4J Question SOAP Error when Passing Authorization Header

bcampano

New Member
Licensed User
Longtime User
I am trying to make some SOAP API calls and am running into issues with authorization. The API requires you pass a basic authentication header. If I use the following:
B4X:
Dim job1 As HttpJob

Dim SoapXML  As String= $"<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:p="http://soap_api_uri"
    <soapenv:Header/>
    <soapenv:Body>
    <p:registerConnection>
    <p:appname>TestApp</p:appname>
    </p:registerConnection>
    </soapenv:Body>
    </soapenv:Envelope>"$
job1.Initialize("SOAP_API", Me)
job1.poststring ("http://soap_endpoint_address", SoapXML)
job1.GetRequest.SetContentType("text/xml")

job1.Username = usernamestring
job1.Password = passwordstring

wait for (job1) jobdone(job1 As HttpJob)   
If job1.Success Then
    Log(job1.GetString)
Else
    Log(job1.ErrorMessage )
End If

If I comment out the .username and .password properties the webservice returns a properly formatted XML response indicating the request must include authentication in the message header as expected.

However, when I include the two lines to set the .username and .password properties then I get the following error message:

B4X:
<?xml version='1.0' encoding='UTF-8'?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns0:Fault xmlns:ns0="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.w3.org/2003/05/soap-envelope">
<faultcode>ns0:Client</faultcode>
<faultstring>
Couldn't create SOAP message due to exception: XML reader error: 
com.ctc.wstx.exc.WstxParsingException: Unexpected '&lt;' character 
in element (missing closing '>'?) at [row,col {unknown-source}]: [3,2]
</faultstring>
</ns0:Fault>
</S:Body>
</S:Envelope>

I am actually trying to port a small VB.net application to B4J. In VB I used the following code to successfully pass the authorization headers to the same SOAP API:

B4X:
Dim mySOAPService As New WS_SOAP()

Dim myCredentials As New System.Net.CredentialCache()
Dim netCred As New NetworkCredential(username, password)
myCredentials.Add(New Uri(mySOAPService.Url), "Basic", netCred)
mySOAPService.Credentials = myCredentials

Any idea what i'm doing wrong here???
 

OliverA

Expert
Licensed User
Longtime User
Upvote 0

bcampano

New Member
Licensed User
Longtime User
Thanks for the find! It didn't solve the issue entirely but it did make me take another good look at the SOAPXML string. After fixing the ">" you noted as missing, I also noticed a stray:

B4X:
<soapenv:Header/>

Once I removed that I was good to go! Thanks for taking the time to put an extra set of eyes on a stupid little mistake...
 
Upvote 0
Top