Welcome to OCS Inventory NG community support, where you can ask questions and receive answers from other members of the community.

Please ask questions only in English or French.

Release 2.6 available

The official documentation can be found on http://wiki.ocsinventory-ng.org. Read it before asking your question.

Problems with sign in - OCS Web Service (Using C#)

Hi all,

I'm trying to use the Ocs Inventory Soap Web sevice using C#, but i got some problems. Here's my code and my errors:

   class OCS
    {
        public static void CallWebService()
        {
            try
            {
                var _url = "http://test-url/ocsinterface";
                var _action = "http://test-url/ocsinterface?op=get_computers_V1";

                XmlDocument soapEnvelopeXml = CreateSoapEnvelope();
                HttpWebRequest webRequest = CreateWebRequest(_url, _action);
                InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);


                // begin async call to web request.
                IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null);

                // suspend this thread until call is complete. You might want to
                // do something usefull here like update your UI.
                asyncResult.AsyncWaitHandle.WaitOne();

                // get the response from the completed web request.
                string soapResult;
                using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
                {
                    using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
                    {
                        soapResult = rd.ReadToEnd();
                    }
                    Console.Write(soapResult);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        private static XmlDocument CreateSoapEnvelope()
        {
            XmlDocument soapEnvelop = new XmlDocument();
            soapEnvelop.LoadXml(@"<?xml version='1.0' encoding='UTF-8'?>
            <soap:Envelope 
                xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' 
                xmlns:soapenc='http://schemas.xmlsoap.org/soap/encoding/' 
                xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
                soap:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/' 
                xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>
                <soap:Body>
                    <get_computers_V1>
                        <REQUEST>
                            <ENGINE>FIRST</ENGINE>
                            <ASKING_FOR>META</ASKING_FOR>
                            <CHECKSUM>131071</CHECKSUM>
                            <OFFSET>0</OFFSET>
                            <WANTED>131071</WANTED>
                        </REQUEST>
                    </get_computers_V1>
                </soap:Body>
            </soap:Envelope>
            ");
            return soapEnvelop;
        }


        private static HttpWebRequest CreateWebRequest(string url, string action)
        {

            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
            webRequest.Headers.Add("SOAPAction", action);
            webRequest.ContentType = "text/xml;charset=\"utf-8\"";
            webRequest.Accept = "text/xml";
            webRequest.Method = WebRequestMethods.Http.Post;
            webRequest.UserAgent = "Mozilla/4.0+(compatible;+MSIE+5.01;+Windows+NT+5.0";
            webRequest.Credentials = new NetworkCredential("login", "pass");

            return webRequest;
        }

        private static void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)
        {
            using (Stream stream = webRequest.GetRequestStream())
                soapEnvelopeXml.Save(stream);

        }
    }

in Agent for Windows development by (200 points)

1 Answer

0 votes

And my errors:

"<?xml version=\"1.0\" encoding=\"UTF-8\"?>
    <soap:Envelope soap:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">
        <soap:Body>
            <soap:Fault>
                <faultcode>
                    soap:Client
                </faultcode>
                <faultstring>
                    Denied access to method (get_computers_V1) in class (main) at /usr/share/perl5/SOAP/Lite.pm line 2809.\n
                </faultstring>
            </soap:Fault>
        </soap:Body>
    </soap:Envelope>"

The error is the same like:
http://ask.ocsinventory-ng.org/3979/problems-with-ocs-web-service-using-c%23

by (200 points)
 
Powered by Question2Answer
...