How to HTTP post in Dot Net

RoelofRoelof Member Posts: 364
edited 2014-12-02 in NAV Three Tier
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
Roelof de Jong
http://www.esopro.com

Comments

  • massimopasqualimassimopasquali Member Posts: 81
    BJECT Codeunit 50001 Inbox WebService
    {
    OBJECT-PROPERTIES
    {
    Date=17.08.12;
    Time=10:05:02;
    Modified=Yes;
    Version List=Dynamics.is;
    }
    PROPERTIES
    {
    OnRun=BEGIN
    END;

    }
    CODE
    {
    VAR
    [email protected] : TextConst 'ENU=IC Partner Code %1 not found;ISL=Mf. félagakóti %1 finnst ekki';
    [email protected] : TextConst 'ENU=Responsibility Center Code mismatch;ISL=Ábyrgðastöðvarkóti stemmir ekki';
    [email protected] : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Convert";
    [email protected] : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.File";
    [email protected] : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Array";
    [email protected] : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.MemoryStream";
    [email protected] : Codeunit 419;
    [email protected] : File;
    [email protected] : Text[1024];
    [email protected] : InStream;
    [email protected] : OutStream;
    [email protected] : TextConst 'ENU=No data received;ISL=Engin gögn móttekin';
    [email protected] : TextConst 'ENU=Transaction no. %1 is already imported;ISL=Færsla nr. %1 er þegar innflutt';

    PROCEDURE [email protected]([email protected] : Code[10];[email protected] : Code[10];[email protected] : Code[10];[email protected] : Code[10];[email protected] : BigText;[email protected] : BigText;[email protected] : BigText;[email protected] : BigText;VAR [email protected] : Text[1024]) Success : Boolean;
    VAR
    [email protected] : Record 413;
    [email protected] : TEMPORARY Record 99008535;
    [email protected] : TEMPORARY Record 414;
    [email protected] : TEMPORARY Record 415;
    [email protected] : TEMPORARY Record 423;
    [email protected] : TEMPORARY Record 426;
    [email protected] : TEMPORARY Record 427;
    [email protected] : TEMPORARY Record 428;
    [email protected] : TEMPORARY Record 429;
    [email protected] : TEMPORARY Record 442;
    [email protected] : Record 418;
    [email protected] : Record 418;
    [email protected] : Record 419;
    [email protected] : Record 434;
    [email protected] : Record 435;
    [email protected] : Record 436;
    [email protected] : Record 437;
    [email protected] : Record 423;
    [email protected] : Record 442;
    [email protected] : Codeunit 427;
    [email protected] : Code[20];
    [email protected] : Code[20];
    [email protected] : XMLport 12;
    [email protected] : 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.
    }
    }
  • zeonzeon Member Posts: 130
    Try this - works for me!
    HTTPRequest := HTTPRequestClass.XMLHTTPRequestClass;
    
    // I'm adding text to a bigtext variable here. Not important for you
    IF URL[1] <> '' THEN
      XMLTextNode.ADDTEXT(URL[1]);
    
    
    HTTPRequest.open('POST',WebaddressToPostTo, FALSE,'','');
    // You might also want to change your RequestHeader
    HTTPRequest.setRequestHeader('Content-type','application/x-www-form-urlencoded; charset=utf-8');
    HTTPRequest.send(XMLTextNode);
    
    IF NOT (HTTPRequest.status = 200) THEN
      MESSAGE(FORMAT(COPYSTR(HTTPRequest.statusText,1,250)));
    
    ResponseText := HTTPRequest.responseText;
    

    And you can continue error handling and whatever.

    Variables:
    Name	DataType	Subtype	Length
    HTTPRequest	      DotNet	      MSXML.XMLHTTPRequest.'Microsoft.MSXML, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'	
    HTTPRequestClass	DotNet	MSXML.XMLHTTPRequestClass.'Microsoft.MSXML, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'	
    XMLTextNode	BigText
    
  • thegunzothegunzo Member Posts: 274
    I suggest not using XMLHTTP. That requires that object to be installed and that is not a standard .net object that is installed everywhere.

    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
  • RoelofRoelof Member Posts: 364
    Thanks Zeon, TheGunzo.
    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);
    //<<
    Roelof de Jong
    http://www.esopro.com
  • Ndiwa_BKNdiwa_BK Member Posts: 1
    Zeon,

    That Code works Perfectly. Many Thanks
Sign In or Register to comment.