Trying to build a Web Service request in NAV
BlackOutDev
Member Posts: 3
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();
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
-
did you try to use cu 1290?
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/
best regards
Franz Kalchmair, MVP
Alias: Jonathan Archer
please like / agree / verify my answer, if it was helpful for you. thx.
Blog: http://moxie4nav.wordpress.com/0 -
-
Acc. First Blog post:
"ContentType
in fct. BuildWebRequest (in cu 1290) replace
HttpWebRequest.ContentType := ContentTypeTxt;
by
HttpWebRequest.ContentType := 'text/xml;charset=utf-8';"best regards
Franz Kalchmair, MVP
Alias: Jonathan Archer
please like / agree / verify my answer, if it was helpful for you. thx.
Blog: http://moxie4nav.wordpress.com/0 -
I changed the content type in CU 1290, which worked for the UTF-8 error.
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:
0 -
remove the xmlns part from first reqText line.
simply write '<GetMaintenanceStatus>'best regards
Franz Kalchmair, MVP
Alias: Jonathan Archer
please like / agree / verify my answer, if it was helpful for you. thx.
Blog: http://moxie4nav.wordpress.com/0 -
Hi
Try removing all "WS:" you use in the function and fields.sb.Append('<GetMaintenanceStatus>'); sb.Append('<tableNo>' + FORMAT(TableNo) + '</tableNo>'); sb.Append('<fieldNo>' + FORMAT(FieldNo) + '</fieldNo>'); sb.Append('<recordNo>' + RecordNo + '</recordNo>'); sb.Append('<status>0</status>'); sb.Append('</GetMaintenanceStatus>');
Regards0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.7K Microsoft Dynamics NAV
- 18.8K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 328 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions

