I have 2 different tenants, and I want to communicate between these tenants using a web service.
The web service itself is working fine since i have tested it with SOAPUI already.
The problem now is building a request in NAV itself which can be called in tenant 1 to request to tenant 2.
Currently I always get an 401 Unauthorized error. I have tried UseDefaultCredentials(TRUE), Credentials.DefaultCredentials and Credentials.DefaultNetworkCredentials but nothing worked.
See my code below:
.net Parameters:
sb
System.Text.StringBuilder.'mscorlib,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089'
uriObj
System.Uri.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
lgRequest
System.Net.HttpWebRequest.'System,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089'
stream
System.IO.StreamWriter.'mscorlib, Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089'
lgResponse
System.Net.HttpWebResponse.'System,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089'
str
System.IO.Stream.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
reader
System.Xml.XmlTextReader.'System.Xml,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089'
document
System.Xml.XmlDocument.'System.Xml,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089'
ascii
System.Text.Encoding.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
credentials
System.Net.CredentialCache.'System,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089'
Code:
soapActionUrl := 'urn:microsoft-dynamics-schemas/codeunit/WS_SharedTables:GetMaintenanceStatus';
sb := sb.StringBuilder();
sb.Append('<soap:Envelope xmlns:xsi="www.w3.org/.../XMLSchema-instance" xmlns:xsd="www.w3.org/.../XMLSchema" xmlns:soap="schemas.xmlsoap.org/.../">');
sb.Append('<soap:Body>');
sb.Append('<ws:GetMaintenanceStatus>');
sb.Append('<ws:tableNo>' + FORMAT(TableNo) + '</ws:tableNo>');
sb.Append('<ws:fieldNo>' + FORMAT(FieldNo) + '</ws:fieldNo>');
sb.Append('<ws:recordNo>' + RecordNo + '</ws:recordNo>');
sb.Append('<ws:status>0</ws:status>');
sb.Append('</ws:GetMaintenanceStatus>');
sb.Append('</soap:Body>');
sb.Append('</soap:Envelope>');
uriObj := uriObj.Uri(url);
lgRequest := lgRequest.CreateDefault(uriObj);
lgRequest.Method := 'POST';
lgRequest.ContentType := 'text/xml';
lgRequest.Headers.Add('SOAPAction', soapActionUrl);
lgRequest.UseDefaultCredentials(TRUE);
lgRequest.Timeout := 120000;
stream := stream.StreamWriter(lgRequest.GetRequestStream(), ascii.ASCII);
stream.Write(sb.ToString());
stream.Close();
lgResponse := lgRequest.GetResponse();
str := lgResponse.GetResponseStream();
reader := reader.XmlTextReader(str);
document := document.XmlDocument();
document.Load(reader);
reader.Close();
str.Close();
0
Answers
for details follow
https://moxie4nav.wordpress.com/2015/09/10/call-external-web-service-using-new-cu-1290/
https://moxie4nav.wordpress.com/2016/07/03/call-external-web-service-using-cu-1290-part-2/
Franz Kalchmair, MVP
Alias: Jonathan Archer
please like / agree / verify my answer, if it was helpful for you. thx.
Blog: http://moxie4nav.wordpress.com/
"ContentType
in fct. BuildWebRequest (in cu 1290) replace
HttpWebRequest.ContentType := ContentTypeTxt;
by
HttpWebRequest.ContentType := 'text/xml;charset=utf-8';"
Franz Kalchmair, MVP
Alias: Jonathan Archer
please like / agree / verify my answer, if it was helpful for you. thx.
Blog: http://moxie4nav.wordpress.com/
The next error I faced was "Error 401 - Unauthorized", so I implemented specific credentials like described in blog post 1.
After getting around the Unathorized error now I get following error:
Based on blog post 2 the code I use for reqText looks as following:
I have tested the Web Service with SOAP UI already and there it is working fine:
simply write '<GetMaintenanceStatus>'
Franz Kalchmair, MVP
Alias: Jonathan Archer
please like / agree / verify my answer, if it was helpful for you. thx.
Blog: http://moxie4nav.wordpress.com/
Try removing all "WS:" you use in the function and fields.
Regards