Using  C# to call Odata api in S4 HANA CLOUD Method 1

           In some situation like integrating with S4 hana cloud, we need to use C# to call odata in S4 hana cloud(s4hc). For example we need to integration a third party Mes with S4HC and the Mes system is developed in C# . Customer decided to use point to point integration without integration middle ware .Then we need to use C# to call odata in  S4HC .


The assumption is that the reader has done the following part :

1, Have created communication system , communication user and communication arrangement in S4HC .

2, Have tested the odata api from postman for the odata api .


The following is the step to  call odata api in s4hc by coding in c#  with visual studio 2012 .

1 Create a new project in VS2012

[File] > [New]>[Project]

Or Ctrl+ Shift+N

The following is the C# code :

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Net;

using System.IO;

using System.Xml;


namespace ConsoleApplication2


    class Program


        static void Main(string[] args)




                string url = "";

                HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);

                httpRequest.Method = "GET";

                string authorization = "UserName" + ":" + "PassWord";

                string base64 = Convert.ToBase64String(Encoding.Default.GetBytes(authorization));

                httpRequest.Headers.Add("Authorization", "Basic " + base64);

                using (HttpWebResponse myres = (HttpWebResponse)httpRequest.GetResponse())


                    StreamReader sr = new StreamReader(myres.GetResponseStream(), Encoding.UTF8);

                    string resstring = sr.ReadToEnd();

                    XmlDocument doc = new XmlDocument();


                    XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);

                    nsmgr.AddNamespace("a", "");

                    nsmgr.AddNamespace("m", "");

                    nsmgr.AddNamespace("d", "");


                    XmlNodeList nl = doc.SelectNodes("//m:properties", nsmgr);


                    foreach (XmlNode n in nl)


                        Console.WriteLine(n.SelectSingleNode("//d:BillingDocument", nsmgr).InnerText);

                        Console.WriteLine(n.SelectSingleNode("//d:CreationDate", nsmgr).InnerText);

                        Console.WriteLine(n.SelectSingleNode("//d:CreationTime", nsmgr).InnerText);






                Console.WriteLine("press any key to continue....");



            catch (Exception e)












After change the correct URL , user name and password, execute the program .


Explanations to the coding :

 nsmgr.AddNamespace("m", "");


It is name space in the message respond .

Best regards!

Jacky Liu