将SOAP信封发送到MS Dynamics NAV Web服务

本文关键字:Dynamics MS NAV Web 服务 SOAP | 更新日期: 2023-09-27 18:18:42

我正在尝试使用HttpWebRequest, HttpWebResponse直接将SOAP信封发布到Dynamics NAV Webservices。

代码:

        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;
    }

SOAP信封格式:

<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>

但是当我试图在HttpWebRequest中获得没有头部的响应时,它以状态OK的xml格式返回整个web服务,但项目没有插入NAV。

当我试图在HttpWebRequest中获得报头响应时,它的{"远程服务器返回了一个错误:(500)内部服务器错误。"System.Net.WebExceptionStatus.ProtocolError}

我想使用soap信封在NAV中创建Item,而不是通过直接引用服务。

任何帮助都对我有帮助。

与问候Kishore K

将SOAP信封发送到MS Dynamics NAV Web服务

看起来您使用SOAPui来创建请求xml。这个应用程序总是在每行的末尾添加不可见的字符。删除它们。

也试着改变你的请求的格式,例如把它放在一行。由于未知原因,Nav威胁某些标签之间的换行为错误(抛出http 500)。我只是忘记了哪些标签(标题和正文可能是)。其他换行符都没问题。

和SOAPAction头是强制性的,所以使用它,否则你会得到wsdl的响应。

注。SOAPui的测试版可以很好地与Nav一起工作,并支持NTLM,因此您可以使用它来测试不同的请求xml并找到正确的格式。

我是使用集成导航web服务的新手,我试图使用简单的c#控制台应用程序发送xml请求,但它总是返回401(未经授权)

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;
    }