简体   繁体   English

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

[英]Post SOAP envelop to MS Dynamics NAV Web Service

I am trying to post SOAP Envelope directly to Dynamics NAV Webservices using HttpWebRequest, HttpWebResponse. 我正在尝试使用HttpWebRequest,HttpWebResponse将SOAP信封直接发布到Dynamics导航Web服务。

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

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. 但是,当我尝试在HttpWebRequest中获取没有Header的响应时,它以xml格式返回状态为OK的整个Web服务,但是Item没有插入到NAV中。

When I am trying to get response with Header in HttpWebRequest, its {"The remote server returned an error: (500) Internal Server Error." 当我尝试在HttpWebRequest中获得Header的响应时,其{“远程服务器返回错误:(500)Internal Server Error。”。 System.Net.WebExceptionStatus.ProtocolError} System.Net.WebExceptionStatus.ProtocolError}

I want to create Item in NAV using soap envelope not by direct referencing the service. 我想使用肥皂信封在NAV中创建项目,而不是直接引用服务。

Any help will helpful to me. 任何帮助都会对我有所帮助。

With Regards Kishore K 谨启Kishore K

It looks like you using SOAPui to create request xml. 看起来您在使用SOAPui创建请求xml。 This app always adds invisible chars at the end of each line. 此应用程序总是在每行末尾添加不可见的字符。 Delete them. 删除它们。

Also try to unformat your request, eg make it in one line. 还要尝试取消格式化您的请求,例如将其排成一行。 For unknown reason Nav threats linebreake between certain tags as error (throws http 500). 出于未知原因,Nav会威胁某些标签之间的换行错误(抛出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. 而且SOAPAction标头是强制性的,因此请使用它,否则您将始终得到wsdl作为响应。

Ps 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. SOAPui的PS beta与Nav配合良好,并支持NTLM,因此您可以使用它来测试不同的请求xml并找出正确的格式。

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) 我是与导航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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM