1
votes

I am trying to post SOAP Envelope directly to Dynamics NAV Webservices using HttpWebRequest, HttpWebResponse.

Code:

        private void button1_Click(object sender, EventArgs e)
    {
        string requestString = LoadData();
        HttpWebRequest request;
        HttpWebResponse response = null;
        string url = "http://localhost:7047/DynamicsNAV70/WS/Page/nav_Item";
        byte[] requestBuffer = null;
        Stream postStream = null;
        Stream responseStream = null;
        StreamReader responseReader = null;
        request = (HttpWebRequest)WebRequest.Create(url);
        request.ProtocolVersion = new Version(1,1);
        request.Method = "POST";

        //request.Headers.Add("SOAPAction", @"urn:microsoft-dynamics-schemas/page/nav_item:create");
        request.Headers.Add("Action", @"urn:microsoft-dynamics-schemas/page/nav_item");
        //request.Headers.Add("Content-Type", @"text/xml; charset=utf-8");
        request.ContentType = @"application/xml; charset=utf-8";
        requestBuffer = Encoding.ASCII.GetBytes(requestString);
        request.ContentLength = requestBuffer.Length;
        request.UseDefaultCredentials = true;
        postStream = request.GetRequestStream();
        postStream.Write(requestBuffer, 0, requestBuffer.Length); 
        postStream.Close();

        response = (HttpWebResponse)request.GetResponse();
        responseStream = response.GetResponseStream();
        string response_result=string.Empty;
        if (responseStream != null)
        {
            responseReader = new StreamReader(responseStream);
            response_result = responseReader.ReadToEnd();
        }
        MessageBox.Show(response_result);
    }

    private string LoadData()
    {
       // throw new NotImplementedException();
        XmlDocument oCustomer = new XmlDocument();
        oCustomer.Load(@"C:\Users\kishore.LOCAL.000\Desktop\NAV_DEMO\NAV_DEMO\bin\Debug\input\item.xml");
        return oCustomer.InnerXml;
    }

Format of SOAP Envelope is below:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"     xmlns:ins="urn:microsoft-dynamics-schemas/page/nav_item">
<soapenv:Header/>
<soapenv:Body>
  <ins:Create>
     <ins:nav_Item>
        <!--Optional:-->
        <ins:Key>?</ins:Key>
        <!--Optional:-->
        <ins:No>1234</ins:No>
        <!--Optional:-->
        <ins:Description>Test Item</ins:Description>
        </ins:nav_Item>
  </ins:Create>
 </soapenv:Body>
</soapenv:Envelope>

But when I am trying to get response without Header in HttpWebRequest, its returning whole web service in xml format with Status OK, but Item is not inserting in NAV.

When I am trying to get response with Header in HttpWebRequest, its {"The remote server returned an error: (500) Internal Server Error." System.Net.WebExceptionStatus.ProtocolError}

I want to create Item in NAV using soap envelope not by direct referencing the service.

Any help will helpful to me.

With Regards Kishore K

2

2 Answers

2
votes

It looks like you using SOAPui to create request xml. This app always adds invisible chars at the end of each line. Delete them.

Also try to unformat your request, e.g. make it in one line. For unknown reason Nav threats linebreake between certain tags as error (throws http 500). I just forgot which tags (header and body may be). The rest linebreaks are fine.

And SOAPAction header is mandatory so use it or you will get wsdl in response all the time.

P.s. beta of SOAPui works fine with Nav and supports NTLM so you can use it to test different request xml and find which format is correct.

-1
votes

I am new to using intergrating with nav web services, I am trying to send an xml request using a simple c# console application but it is always returning a 401(unauthorised)

static void Main(string[] args)
    {
        Console.WriteLine("We have started");                                    

        string pageName = "http://hrp-dmu.uganda.hrpsolutions.co.ug:9047/DynamicsNAV80/WS/Uganda%20Management%20Institute/Page/CustomerWS";            
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(pageName);
        req.Method = "POST";
        req.ContentType = "text/xml;charset=UTF-8";
        req.ProtocolVersion = new Version(1, 1);
        req.Headers.Add("SOAPAction", @"urn:microsoftdynamicsschemas/page/customerws:Create");            

        Console.WriteLine("After preparing request object");
        string xmlRequest = GetTextFromXMLFile("E:\\tst3.xml");
        Console.WriteLine("xml request : "+xmlRequest);
        byte[] reqBytes = new UTF8Encoding().GetBytes(xmlRequest);
        req.ContentLength = reqBytes.Length;
        try
        {
            using (Stream reqStream = req.GetRequestStream())
            {
                reqStream.Write(reqBytes, 0, reqBytes.Length);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("GetRequestStreamException : " + ex.Message);
        }
        HttpWebResponse resp = null;
        try
        {
            resp = (HttpWebResponse)req.GetResponse();
        }
        catch (Exception exc)
        {
            Console.WriteLine("GetResponseException : " + exc.Message);
        }
        string xmlResponse = null;
        if (resp == null)
        {
            Console.WriteLine("Null response");
        }
        else
        {
            using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
            {
                xmlResponse = sr.ReadToEnd();
            }
            Console.WriteLine("The response");
            Console.WriteLine(xmlResponse);
        }
        Console.ReadKey();
    }

    private static string GetTextFromXMLFile(string file)
    {
        StreamReader reader = new StreamReader(file);
        string ret = reader.ReadToEnd();
        reader.Close();
        return ret;
    }