I have a few lines of code using automations.
I would like to change the code by using dot net functionality but I'm having difficulties figuring out which dotnet class(es) I need to use.
The code is basically trying to do an http post and replying on the response.
How would the code look like using dot net and which classes would I use?
//--- Start
//XmHttp is using: ''Microsoft XML, v6.0'.XMLHTTP'.
//XmlDoc is using: 'Microsoft XML, v6.0'.DOMDocument
//CurrNode is using: 'Microsoft XML, v6.0'.IXMLDOMNode
CREATE(XmlHttp);
XmlHttp.open('POST',WebServiceURL,FALSE);
XmlHttp.setRequestHeader('Content-Type','text/xml');
XmlHttp.setRequestHeader('SOAPAction','"
http://tempuri.org/' + WebFunc + '"');
XmlHttp.send(XMLDoc);
CLEAR(XMLDoc);
CREATE(XMLDoc);
XMLDoc.load(XmlHttp.responseXML);
IF XmlHttp.status <> 200 THEN BEGIN
ErrorNote := 'HTTP ' + FORMAT(XmlHttp.status) + ': ' + XmlHttp.statusText;
EXIT(FALSE);
END;
CurrNode := XMLDoc.documentElement.selectSingleNode('soap:Body/' + WebFunc + 'Response/' + WebFunc + 'Result');
IF NOT ISCLEAR(CurrNode) THEN BEGIN
ReturnMessage := CurrNode.text;
IF STRPOS(ReturnMessage,'Success') > 0 THEN
EXIT(TRUE);
END;
ErrorNote := COPYSTR(ReturnMessage,1,MAXSTRLEN(ErrorNote));
EXIT(FALSE);
//--- End
Comments
{
OBJECT-PROPERTIES
{
Date=17.08.12;
Time=10:05:02;
Modified=Yes;
Version List=Dynamics.is;
}
PROPERTIES
{
OnRun=BEGIN
END;
}
CODE
{
VAR
Text001@1100408000 : TextConst 'ENU=IC Partner Code %1 not found;ISL=Mf. félagakóti %1 finnst ekki';
Text002@1100408001 : TextConst 'ENU=Responsibility Center Code mismatch;ISL=Ábyrgðastöðvarkóti stemmir ekki';
Convert@1100408004 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Convert";
DocumentFile@1100408003 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.File";
Bytes@1100408010 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Array";
MemoryStream@1100408008 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.MemoryStream";
FileMgt@1100408009 : Codeunit 419;
TempFile@1100408007 : File;
TempFileName@1100408006 : Text[1024];
InStr@1100408002 : InStream;
OutStr@1100408005 : OutStream;
Text003@1100408011 : TextConst 'ENU=No data received;ISL=Engin gögn móttekin';
Text004@1100408012 : TextConst 'ENU=Transaction no. %1 is already imported;ISL=Færsla nr. %1 er þegar innflutt';
PROCEDURE LoadTransaction@1100408001(FromPartnerCode@1100408004 : Code[10];FromRespCenterCode@1100408005 : Code[10];ToPartnerCode@1100408006 : Code[10];ToRespCenterCode@1100408007 : Code[10];Transaction@1100408000 : BigText;PDFInvoice@1100408001 : BigText;PDFDetails@1100408002 : BigText;XMLInvoice@1100408003 : BigText;VAR ResponseMessage@1100408009 : Text[1024]) Success : Boolean;
VAR
ICPartner@1100408010 : Record 413;
TempBlob@1100408024 : TEMPORARY Record 99008535;
TempICOutboxTrans@1100408020 : TEMPORARY Record 414;
TempICOutBoxJnlLine@1100408019 : TEMPORARY Record 415;
TempICIOBoxJnlDim@1100408018 : TEMPORARY Record 423;
TempICOutBoxSalesHdr@1100408017 : TEMPORARY Record 426;
TempICOutBoxSalesLine@1100408016 : TEMPORARY Record 427;
TempICOutBoxPurchHdr@1100408015 : TEMPORARY Record 428;
TempICOutBoxPurchLine@1100408014 : TEMPORARY Record 429;
TempICDocDim@1100408013 : TEMPORARY Record 442;
ICInboxTransaction@1100408022 : Record 418;
ICInboxTransaction2@1100408032 : Record 418;
ICInboxJnlLine@1100408030 : Record 419;
ICInboxSalesHdr@1100408029 : Record 434;
ICInboxSalesLine@1100408028 : Record 435;
ICInboxPurchHdr@1100408027 : Record 436;
ICInboxPurchLine@1100408026 : Record 437;
ICInboxJnlLineDim@1100408025 : Record 423;
ICInboxDocDim@1100408023 : Record 442;
ICInboxOutboxMgt@1100408021 : Codeunit 427;
FromICPartnerCode@1100408012 : Code[20];
ToICPartnerCode@1100408011 : Code[20];
ICOutboxExportXML@1100408008 : XMLport 12;
NewTableID@1100408031 : Integer;
BEGIN
IF NOT ICPartner.GET(FromPartnerCode) THEN BEGIN
ResponseMessage := Text001;
EXIT(FALSE);
END;
IF ICPartner."Responsibility Center" <> FromRespCenterCode THEN BEGIN
ResponseMessage := Text002;
EXIT(FALSE);
END;
IF Transaction.LENGTH > 0 THEN BEGIN
Bytes := Convert.FromBase64String(Transaction);
MemoryStream := MemoryStream.MemoryStream(Bytes);
TempBlob.Blob.CREATEOUTSTREAM(OutStr);
MemoryStream.WriteTo(OutStr);
TempBlob.Blob.CREATEINSTREAM(InStr);
ICOutboxExportXML.SETSOURCE(InStr);
ICOutboxExportXML.IMPORT;
ICOutboxExportXML.GetICOutboxTrans(TempICOutboxTrans);
ICOutboxExportXML.GetICOutBoxJnlLine(TempICOutBoxJnlLine);
ICOutboxExportXML.GetICIOBoxJnlDim(TempICIOBoxJnlDim);
ICOutboxExportXML.GetICOutBoxSalesHdr(TempICOutBoxSalesHdr);
ICOutboxExportXML.GetICOutBoxSalesLine(TempICOutBoxSalesLine);
ICOutboxExportXML.GetICOutBoxPurchHdr(TempICOutBoxPurchHdr);
ICOutboxExportXML.GetICOutBoxPurchLine(TempICOutBoxPurchLine);
ICOutboxExportXML.GetICSalesDocDim(TempICDocDim);
ICOutboxExportXML.GetICSalesDocLineDim(TempICDocDim);
ICOutboxExportXML.GetICPurchDocDim(TempICDocDim);
ICOutboxExportXML.GetICPurchDocLineDim(TempICDocDim);
FromICPartnerCode := ICOutboxExportXML.GetFromICPartnerCode;
ToICPartnerCode := ICOutboxExportXML.GetToICPartnerCode;
ICInboxTransaction2.SETRANGE("Transaction No.",TempICOutboxTrans."Transaction No.");
ICInboxTransaction2.SETRANGE("IC Partner Code",FromICPartnerCode);
ICInboxTransaction2.SETRANGE("Transaction Source",TempICOutboxTrans."Transaction Source");
IF ICInboxTransaction2.FINDFIRST THEN BEGIN
ResponseMessage := STRSUBSTNO(Text004,TempICOutboxTrans."Transaction No.");
EXIT(FALSE);
END;
IF TempICOutboxTrans.FIND('-') THEN BEGIN
ICInboxOutboxMgt.OutboxTransToInbox(TempICOutboxTrans,ICInboxTransaction,FromICPartnerCode);
TempICOutBoxJnlLine.SETRANGE("Transaction No.",TempICOutboxTrans."Transaction No.");
TempICOutBoxJnlLine.SETRANGE("IC Partner Code",TempICOutboxTrans."IC Partner Code");
TempICOutBoxJnlLine.SETRANGE("Transaction Source",TempICOutboxTrans."Transaction Source");
IF TempICOutBoxJnlLine.FIND('-') THEN
REPEAT
ICInboxOutboxMgt.OutboxJnlLineToInbox(ICInboxTransaction,TempICOutBoxJnlLine,ICInboxJnlLine);
TempICIOBoxJnlDim.SETRANGE("Transaction No.",TempICOutboxTrans."Transaction No.");
TempICIOBoxJnlDim.SETRANGE("IC Partner Code",TempICOutboxTrans."IC Partner Code");
TempICIOBoxJnlDim.SETRANGE("Transaction Source",TempICOutboxTrans."Transaction Source");
TempICIOBoxJnlDim.SETRANGE("Line No.",ICInboxJnlLine."Line No.");
IF TempICIOBoxJnlDim.FIND('-') THEN
REPEAT
ICInboxOutboxMgt.OutboxJnlLineDimToInbox(
ICInboxJnlLine,TempICIOBoxJnlDim,ICInboxJnlLineDim,DATABASE::"IC Inbox Jnl. Line");
UNTIL TempICIOBoxJnlDim.NEXT = 0;
UNTIL TempICOutBoxJnlLine.NEXT = 0;
TempICOutBoxSalesHdr.SETRANGE("IC Transaction No.",TempICOutboxTrans."Transaction No.");
TempICOutBoxSalesHdr.SETRANGE("IC Partner Code",TempICOutboxTrans."IC Partner Code");
TempICOutBoxSalesHdr.SETRANGE("Transaction Source",TempICOutboxTrans."Transaction Source");
IF TempICOutBoxSalesHdr.FIND('-') THEN
REPEAT
ICInboxOutboxMgt.OutboxSalesHdrToInbox(ICInboxTransaction,TempICOutBoxSalesHdr,ICInboxPurchHdr);
UNTIL TempICOutBoxSalesHdr.NEXT = 0;
TempICOutBoxSalesLine.SETRANGE("IC Transaction No.",TempICOutboxTrans."Transaction No.");
TempICOutBoxSalesLine.SETRANGE("IC Partner Code",TempICOutboxTrans."IC Partner Code");
TempICOutBoxSalesLine.SETRANGE("Transaction Source",TempICOutboxTrans."Transaction Source");
IF TempICOutBoxSalesLine.FIND('-') THEN
REPEAT
ICInboxOutboxMgt.OutboxSalesLineToInbox(ICInboxTransaction,TempICOutBoxSalesLine,ICInboxPurchLine);
UNTIL TempICOutBoxSalesLine.NEXT = 0;
TempICOutBoxPurchHdr.SETRANGE("IC Transaction No.",TempICOutboxTrans."Transaction No.");
TempICOutBoxPurchHdr.SETRANGE("IC Partner Code",TempICOutboxTrans."IC Partner Code");
TempICOutBoxPurchHdr.SETRANGE("Transaction Source",TempICOutboxTrans."Transaction Source");
IF TempICOutBoxPurchHdr.FIND('-') THEN
REPEAT
ICInboxOutboxMgt.OutboxPurchHdrToInbox(ICInboxTransaction,TempICOutBoxPurchHdr,ICInboxSalesHdr);
UNTIL TempICOutBoxPurchHdr.NEXT = 0;
TempICOutBoxPurchLine.SETRANGE("IC Transaction No.",TempICOutboxTrans."Transaction No.");
TempICOutBoxPurchLine.SETRANGE("IC Partner Code",TempICOutboxTrans."IC Partner Code");
TempICOutBoxPurchLine.SETRANGE("Transaction Source",TempICOutboxTrans."Transaction Source");
IF TempICOutBoxPurchLine.FIND('-') THEN
REPEAT
ICInboxOutboxMgt.OutboxPurchLineToInbox(ICInboxTransaction,TempICOutBoxPurchLine,ICInboxSalesLine);
UNTIL TempICOutBoxPurchLine.NEXT = 0;
TempICDocDim.SETRANGE("Transaction No.",TempICOutboxTrans."Transaction No.");
TempICDocDim.SETRANGE("IC Partner Code",TempICOutboxTrans."IC Partner Code");
TempICDocDim.SETRANGE("Transaction Source",TempICOutboxTrans."Transaction Source");
IF TempICDocDim.FIND('-') THEN
REPEAT
CASE TempICDocDim."Table ID" OF
DATABASE::"IC Outbox Sales Header": NewTableID := DATABASE::"IC Inbox Purchase Header";
DATABASE::"IC Outbox Sales Line": NewTableID := DATABASE::"IC Inbox Purchase Line";
DATABASE::"IC Outbox Purchase Header": NewTableID := DATABASE::"IC Inbox Sales Header";
DATABASE::"IC Outbox Purchase Line": NewTableID := DATABASE::"IC Inbox Sales Line";
END;
ICInboxOutboxMgt.OutboxDocDimToInbox(
TempICDocDim,ICInboxDocDim,NewTableID,FromICPartnerCode,ICInboxTransaction."Transaction Source");
UNTIL TempICDocDim.NEXT = 0;
END;
IF XMLInvoice.LENGTH > 0 THEN BEGIN
Bytes := Convert.FromBase64String(XMLInvoice);
MemoryStream := MemoryStream.MemoryStream(Bytes);
ICInboxTransaction."XML Document".CREATEOUTSTREAM(OutStr);
MemoryStream.WriteTo(OutStr);
END;
IF PDFInvoice.LENGTH > 0 THEN BEGIN
Bytes := Convert.FromBase64String(PDFInvoice);
MemoryStream := MemoryStream.MemoryStream(Bytes);
ICInboxTransaction."PDF Document".CREATEOUTSTREAM(OutStr);
MemoryStream.WriteTo(OutStr);
END;
IF PDFDetails.LENGTH > 0 THEN BEGIN
Bytes := Convert.FromBase64String(PDFDetails);
MemoryStream := MemoryStream.MemoryStream(Bytes);
ICInboxTransaction."Details Document".CREATEOUTSTREAM(OutStr);
MemoryStream.WriteTo(OutStr);
END;
ICInboxTransaction.MODIFY;
EXIT(TRUE);
END ELSE BEGIN
ResponseMessage := Text003;
EXIT(FALSE);
END;
END;
BEGIN
END.
}
}
And you can continue error handling and whatever.
Variables:
http://www.dynamics.is/?s=web+service will help you
http://objects4nav.com/web-service-ping-function-for-nav-2013-r2/ will give you code samples that you can use to save you some work.
Gunnar Gestsson
Microsoft Certified IT Professional
Dynamics NAV MVP
http://www.dynamics.is
http://Objects4NAV.com
This is my new code, but some lines give an error (see NOTE):
//>>
Variables:
XmlHttpHandler2 System.Net.Http.HttpClient.'System.Net.Http, Version=4.0.0.0
XmlHttpHandler System.Net.Http.HttpClientHandler.'System.Net.Http, Version=4.0.0.0
XmlHttpRequestMessage System.Net.Http.HttpRequestMessage.'System.Net.Http, Version=4.0.0.0
XmlHttpMethod DotNet System.Net.Http.HttpMethod.'System.Net.Http, Version=4.0.0.0
XmlHttpContent System.Net.Http.StringContent.'System.Net.Http, Version=4.0.0.0
XmlHttpRequestHeader System.Net.Http.Headers.HttpRequestHeaders.'System.Net.Http, Version=4.0.0.0
XmlHttpClient System.Net.Http.HttpClient.'System.Net.Http, Version=4.0.0.0
XmlHttpResponseMessage: System.Net.Http.HttpResponseMessage.'System.Net.Http, Version=4.0.0.0
CurrNode System.Xml.XmlNode.'System.Xml, Version=4.0.0.0
Code:
XmlHttpHandler := XmlHttpHandler.HttpClientHandler();
XmlHttpMethod := XmlHttpMethod.HttpMethod('POST');
XmlHttpRequestMessage := XmlHttpRequestMessage.HttpRequestMessage(XmlHttpMethod,WebServiceURL);
{
Testing:
XmlHttpClient.DefaultRequestHeaders := ('Content-Type','text/xml');
XmlHttpClient.DefaultRequestHeaders('SOAPAction','"http://tempuri.org/' + WebFunc + '"');
XmlHttpRequestMessage.Headers := XmlHttpClient.DefaultRequestHeaders;
}
XmlHttpContent := XmlHttpContent.StringContent(XmlDoc.ToString);
XmlHttpRequestMessage.Content(XmlHttpContent);
XmlHttpHandler2 := XmlHttpHandler2.HttpClient(XmlHttpHandler);
XmlHttpHandler2.SendAsync(XmlHttpRequestMessage);
CLEAR(XmlDoc);
XmlDoc := XmlDoc.XmlDocument;
XmlHttpResponseMessage := XmlHttpResponseMessage.HttpResponseMessage;
XmlHttpResponseMessage.RequestMessage := XmlHttpRequestMessage.HttpRequestMessage(XmlHttpMethod,WebServiceURL);
IF XmlHttpResponseMessage.StatusCode <> 200 THEN BEGIN
ErrorNote := 'HTTP ' + FORMAT(XmlHttpResponseMessage.StatusCode) + ': ' + XmlHttpResponseMessage.ReasonPhrase;
EXIT(FALSE);
END;
XmlHttpContent := XmlHttpResponseMessage.Content;
XmlDoc.LoadXml(XmlHttpContent.ToString); NOTE: This line gives and error.
How can I store the data from the Response back in this XMlDoc?
CurrNode := XmlDoc.DocumentElement.SelectSingleNode('soap:Body/' + WebFunc + 'Response/' + WebFunc + 'Result');
IF NOT ISNULL(CurrNode) THEN
ReturnMessage := CurrNode.InnerText;
IF XmlHttpResponseMessage.IsSuccessStatusCode THEN
EXIT(TRUE);
ErrorNote := COPYSTR(ReturnMessage,1,MAXSTRLEN(ErrorNote));
EXIT(FALSE);
//<<
That Code works Perfectly. Many Thanks