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

2023-09-04 03:49:52 作者:春梦总被尿憋醒

我想用后直接SOAP信封到Dynamics NAV的Web服务的HttpWebRequest,HttpWebResponse。

code:

 私人无效的button1_Click(对象发件人,EventArgs的)
    {
        字符串requestString = LoadData();
        HttpWebRequest的请求;
        HttpWebResponse响应=无效;
        字符串的URL =HTTP://本地主机:7047 / DynamicsNAV70 / WS /页/ nav_Item;
        byte []的requestBuffer = NULL;
        流postStream = NULL;
        流responseStream = NULL;
        StreamReader的responseReader = NULL;
        请求=(HttpWebRequest的)WebRequest.Create(URL);
        request.ProtocolVersion =新版本(1,1);
        request.Method =POST;

        //request.Headers.Add("SOAPAction,@瓮:微软动态图式/页/ nav_item:创建);
        request.Headers.Add(行动,@瓮:微软动态图式/页/ nav_item);
        //request.Headers.Add("Content-Type,@为text / xml;字符集= UTF-8);
        request.ContentType = @应用程序/ XML;字符集= 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();

        响应=(HttpWebResponse)request.GetResponse();
        responseStream = response.GetResponseStream();
        字符串response_result =的String.Empty;
        如果(responseStream!= NULL)
        {
            responseReader =新的StreamReader(responseStream);
            response_result = responseReader.ReadToEnd();
        }
        的MessageBox.show(response_result);
    }

    私人字符串LoadData()
    {
       //抛出新的NotImplementedException();
        XmlDocument的oCustomer =新的XmlDocument();
        oCustomer.Load(@C:\ Users \用户kishore.LOCAL.000 \桌面\ NAV_DEMO \ NAV_DEMO \斌\调试\输入\ item.xml);
        返回oCustomer.InnerXml;
    }
 

SOAP信封的格式如下:

 < soapenv:信封的xmlns:soapenv =htt​​p://schemas.xmlsoap.org/soap/envelope/的xmlns:插件​​=瓮:微软动态图式/页/ nav_item>
< soapenv:页眉/>
< soapenv:身体与GT;
  <插件:创建>
     <插件:nav_Item>
        <  - 可选: - >
        <插件:重点>< / INS:重点>
        <  - 可选: - >
        <插件:无> 1234< /插件:无>
        <  - 可选: - >
        <插件:说明>测试项目< / INS:说明>
        < / INS:nav_Item>
  < / INS:创建>
 < / soapenv:身体与GT;
< / soapenv:信封>
 

但是,当我试图让HttpWebRequest的响应无头,与状态OK XML格式返回其整个网络的服务,但项目不在NAV插入。

构建ceRNA网络的神器 circlncRNAnet

当我试图让HttpWebRequest的响应与头,其{远程服务器返回错误:(500)内部服务器错误 System.Net.WebExceptionStatus.ProtocolError}

我想使用SOAP信封不是通过直接引用服务于净资产值创建项目。

任何帮助将帮助我。

与问候 纪氏/ P> 解决方案

它看起来像你使用SOAPUI创建XML请求。这个应用程序总是在每行的末尾增加了无形的字符。删除它们。

也试图UNFORMAT您的要求,例如,使其在一行。对于不明原因的资产净值威胁某些标签之间linebreake的错误(抛出HTTP 500)。我只是忘了这标签(标题和正文而定)。其余的换行符都很好。

和SOAPAction头是必需的,以便使用它,或者你会得到WSDL响应所有的时间。

P.S。 SOAPUI的测试工作正常导航,并且支持NTLM,所以你可以用它来测试不同的XML请求,并找到该格式是正确的。

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

解决方案

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.