Calling Rest API

peterbbrain
Member Posts: 10
Failure to call a function on a payment provider API
I have created this three lines of code:
HttpWebRequest := HttpWebRequest.Create('https://sandbox.spryngpayments.com/v1/api_key');
HttpWebRequest.Method := 'GET';
HttpWebRequest.Headers.Add('X-APIKEY','DUMMYAPIKEY');
HttpWebResponse := HttpWebRequest.GetResponse;
Last Line raises error message:
A call to System.Net.HttpWebReqiuest.GetResponse failed with this message: An Existing connection was forcibly closed by the remote host.
Also when I use a valid APIKEY
When I try with postman, no problem whatsoever.
I have created this three lines of code:
HttpWebRequest := HttpWebRequest.Create('https://sandbox.spryngpayments.com/v1/api_key');
HttpWebRequest.Method := 'GET';
HttpWebRequest.Headers.Add('X-APIKEY','DUMMYAPIKEY');
HttpWebResponse := HttpWebRequest.GetResponse;
Last Line raises error message:
A call to System.Net.HttpWebReqiuest.GetResponse failed with this message: An Existing connection was forcibly closed by the remote host.
Also when I use a valid APIKEY
When I try with postman, no problem whatsoever.
0
Best Answer
-
Hi peterbbrain,
it's because SSL.
The HttpWebRequest-object tries to "handshake" the security-protocol with the server first.
The first attempt is SSL (because it is the "lowest" security-standard). But since it has been discovered that there is a big security-issue with SSL (heartbleed?), a lot of Webservices close the connection immediately as soon as a client tries to connect with SSL (mostly payment webservices).
You have to initiate the connection in TLS.
Try this here:HttpWebRequest := HttpWebRequest.Create('https://sandbox.spryngpayments.com/v1/api_key'); HttpWebRequest.Method := 'GET'; HttpWebRequest.Headers.Add('X-APIKEY','DUMMYAPIKEY'); ServicePointManager.SecurityProtocol(SecurityProtocol.Tls12); // new line HttpWebResponse := HttpWebRequest.GetResponse;
ServicePointManager is [System.Net.ServicePointManager.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089']
And SecurityProtocol is [System.Net.SecurityProtocolType.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089']
That should work.
Good luck,
HannesMy blog - https://www.HannesHolst.com/5
Answers
-
Use this
CallRESTWebService(BaseUrl : Text;Method : Text;RestMethod : Text;VAR HttpContent : DotNet "System.Net.Http.HttpContent";VAR HttpResponseMessage : DotNet "System.Net.Http.HttpResponseMessage") HttpClient := HttpClient.HttpClient(); HttpClient.BaseAddress := Uri.Uri(BaseUrl); CASE RestMethod OF 'GET': HttpResponseMessage := HttpClient.GetAsync(Method).Result; 'POST': HttpResponseMessage := HttpClient.PostAsync(Method,HttpContent).Result; 'PUT': HttpResponseMessage := HttpClient.PutAsync(Method,HttpContent).Result; 'DELETE': HttpResponseMessage := HttpClient.DeleteAsync(Method).Result; END;
BaseUrl – the first part of the url that is being called, e.g.: http://www.webserviceaddress.com/
Method – the resource on the web service, in other words, the second part of the url, e.g. somecoolapi/parameter?option=value
RestMethod – one of the request verbs, either GET, POST, PUT or DELETE
HttpContent – the content to be sent with the POST or PUT command
HttpResponseMessage – the response message containing the status code and the response body. This parameter is ByVar because the calling code needs to handle the specific response itself. That’s not part of the generic pattern.Best Regards
Zohaib Ahmed
Dynamics NAV ERP Technical Consultant.
please like / agree / verify my answer, if it was helpful for you. thanks.0 -
Thanks very much for your reply. Zohaib Ahmed
Unfortunately this code gives exactly the same result.
Did you try it yourself with the API I want to use ('https://sandbox.spryngpayments.com/v1/api_key')
them I am very curious for the actual code you are using.
Any other suggestions?
0 -
Hi peterbbrain,
it's because SSL.
The HttpWebRequest-object tries to "handshake" the security-protocol with the server first.
The first attempt is SSL (because it is the "lowest" security-standard). But since it has been discovered that there is a big security-issue with SSL (heartbleed?), a lot of Webservices close the connection immediately as soon as a client tries to connect with SSL (mostly payment webservices).
You have to initiate the connection in TLS.
Try this here:HttpWebRequest := HttpWebRequest.Create('https://sandbox.spryngpayments.com/v1/api_key'); HttpWebRequest.Method := 'GET'; HttpWebRequest.Headers.Add('X-APIKEY','DUMMYAPIKEY'); ServicePointManager.SecurityProtocol(SecurityProtocol.Tls12); // new line HttpWebResponse := HttpWebRequest.GetResponse;
ServicePointManager is [System.Net.ServicePointManager.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089']
And SecurityProtocol is [System.Net.SecurityProtocolType.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089']
That should work.
Good luck,
HannesMy blog - https://www.HannesHolst.com/5 -
Thanks very much for this answer.
I did manage to solve it earlier today with exact that line of code.
In the mean time discovered a lot about how to use fiddler in combination with NAV in order to get to the root cause of this problem.
It was the SSL TSL12 and yes a payment provider.
Cheers!0 -
peterbbrain wrote: »Thanks very much for this answer.
I did manage to solve it earlier today with exact that line of code.
In the mean time discovered a lot about how to use fiddler in combination with NAV in order to get to the root cause of this problem.
It was the SSL TSL12 and yes a payment provider.
Cheers!
Please accept my answer if it helps you.Best Regards
Zohaib Ahmed
Dynamics NAV ERP Technical Consultant.
please like / agree / verify my answer, if it was helpful for you. thanks.0 -
I cannot get Fiddler to work with NAV using similar code as you have above. How did you get Fiddler to work, there must be a trick to it?Jason Wilder
jwilder@stonewallkitchen.com0 -
Fiddler is a nice tool for sniffing network traffic.
It can be downloaded here: http://www.telerik.com/fiddler.
When I needed to sniff my network traffic (NAV Consume Webservice Black-Belt ) I discovered that Fiddler “out-of-the-box” does not show the Network packages from NAV.
This is cause NAV instance is running as a service and Fiddler only looks at processes within your current session.
If you need to sniff the network packages from NAV as well please follow these steps:
This is done like this:
1. Go to : C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config
2. locate the file machine.config.
3. open the file (first create a backup)
4. locate the section <system.net> (or create this section at the bottom , within configuration tag).
Update the section to:
<!-- The following section is to force use of Fiddler for all
applications, including those running in service accounts -->
<system.net>
<defaultProxy
enabled = "true"
useDefaultCredentials = "true">
<proxy autoDetect="false" bypassonlocal="false"
proxyaddress="http://127.0.0.1:8888"
usesystemdefault="false" />
</defaultProxy>
</system.net>
5. Restart NAV Service tier.
(do not forget to remove this section (use the backup from step 3) and restart NAV after sniffing.0 -
0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K 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
- 320 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