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

时间:2013-06-15 05:55:45

标签: c# .net web-services soap dynamics-nav

我正在尝试使用HttpWebRequest,HttpWebResponse将SOAP Envelope直接发布到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中尝试获取没有Header的响应时,它以xml格式返回整个Web服务,状态为OK,但是Item没有插入NAV。

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

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

任何帮助对我都有帮助。

关心 Kishore K

2 个答案:

答案 0 :(得分:2)

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

还尝试取消格式化您的请求,例如把它放在一条线上。由于未知原因导航威胁在某些标记之间划线为错误(抛出http 500)。我忘记了哪些标签(标题和正文)。剩下的阵容很好。

并且SOAPAction标头是必需的,因此使用它或者您将始终获得wsdl响应。

P.S。 SOAP的beta版适用于Nav并支持NTLM,因此您可以使用它来测试不同的请求xml并找到正确的格式。

答案 1 :(得分:-1)

我是使用与nav 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;
    }