Error calling external webservice in nav 2018

dsrdevdsrdev Member Posts: 2
edited 2018-12-31 in NAV Three Tier
Hi all,

I am calling an external webservice from NAV with dotnet. I am using the same code in dynamics nav 2016 as in dynamics nav 2018, while in 2016 it's working nicely in 2018 it's giving me the following error:

A connection attempt failed because the connected party did not properly respond after a period of time, or established
connection failed because connected host has failed to respond

This error happens in the following line:

RequestStr := HttpWebRequest.GetRequestStream;


Apparently, it seems it's somtehing realted to dynamics nav 2018 but I can't find what it is, I will be very gratefull if someone could help me with any idea or tip to solve it.


The code:

Name DataType Subtype Length
HttpWebRequest DotNet System.Net.HttpWebRequest.'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
HttpWebResponse DotNet System.Net.WebResponse.'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
HttpStatusCode DotNet System.Net.HttpStatusCode.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
HttpWebException DotNet System.Net.WebException.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
NavWebRequest DotNet NAVWebRequest.NAVWebRequest.'NAVWebRequest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f53f0925d26e1382'
ServicePointManager DotNet System.Net.ServicePointManager.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
SecurityProtocol DotNet System.Net.SecurityProtocolType.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'


WSmngt.SendRequestToApi(UrlLogin,'POST','application/x-www-form-urlencoded','application/json',TRUE,'gestor','SGS',
'Cache-Control','no-cache','username=&password=&grant_type=password');
AuthToken := GetResponseValue('access_token');

SendRequestToApi(Url : Text;Method : Text;ContentType : Text;ReturnType : Text;DisableUI : Boolean;HeaderKey1 : Text;HeaderValue1 : Text;HeaderKey2 : Text;HeaderValue2 : Text;BodyText : Text)
//inicializa la conexión y envía la petición
InitConnection(HttpWebRequest,Url,Method,ContentType,ReturnType,DisableUI);
IF HeaderKey1 <> '' THEN
AddHeader(HttpWebRequest,HeaderKey1,HeaderValue1);
IF HeaderKey2 <> '' THEN
AddHeader(HttpWebRequest,HeaderKey2,HeaderValue2);
AddBody(HttpWebRequest,BodyText);
DoWebRequest(HttpWebRequest,HttpWebResponse,'');

InitConnection(VAR HttpWebRequest : DotNet "System.Net.HttpWebRequest";PostUrl : Text;Method : Text;ContentType : Text;ReturnType : Text;DisableUI : Boolean)
Uri := Uri.Uri(PostUrl);
HttpWebRequest := HttpWebRequest.Create(Uri);
IF DisableUI THEN
HttpWebRequestMgt.DisableUI;
HttpWebRequest.KeepAlive := FALSE;
HttpWebRequest.AllowAutoRedirect := TRUE;
HttpWebRequest.UseDefaultCredentials := TRUE;
HttpWebRequest.Timeout := 300000;
HttpWebRequest.Method := Method;
HttpWebRequest.ContentType := ContentType;
HttpWebRequest.Accept := ReturnType;

AddHeader(VAR HttpWebRequest : DotNet "System.Net.HttpWebRequest";key : Text;value : Text)
HttpWebRequest.Headers.Add(key,value);

AddBody(VAR HttpWebRequest : DotNet "System.Net.HttpWebRequest";XmlBodyText : Text)
RequestStr := HttpWebRequest.GetRequestStream;
StreamWriter := StreamWriter.StreamWriter(RequestStr,Encoding.UTF8);
StreamWriter.Write(XmlBodyText);
StreamWriter.Flush;
StreamWriter.Close;
StreamWriter.Dispose;

DoWebRequest(VAR HttpWebRequest : DotNet "System.Net.HttpWebRequest";VAR HttpWebResponse : DotNet "System.Net.WebResponse";IgnoreCode : Code[10])
ServicePointManager.SecurityProtocol(SecurityProtocol.Tls);
NAVWebRequest := NAVWebRequest.NAVWebRequest;
IF NOT NAVWebRequest.doRequest(HttpWebRequest,HttpWebException,HttpWebResponse) THEN
IF (IgnoreCode = '') OR (STRPOS(HttpWebException.Message,IgnoreCode) = 0) THEN
ERROR(HttpWebRequestError,HttpWebException.Status.ToString,HttpWebException.Message);

Answers

Sign In or Register to comment.