Options

Trying to build a Web Service request in NAV

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();

Answers

Sign In or Register to comment.