Post SOAP envelop to MS Dynamics NAV Web Service

a 夏天 提交于 2019-12-08 07:08:31

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.

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;
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!