web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Answered

HttpClient.Send with certificate

(0) ShareShare
ReportReport
Posted on by 60

Hi there,

I´m trying to do a HttpClient.Send() in a BC20 Sandbox scenario with the following code ;

procedure SendXml(pXmlDocument: XmlDocument)
var
    Client: HttpClient;
    Headers: HttpHeaders;
    RequestMessage: HttpRequestMessage;
    ResponseMessage: HttpResponseMessage;
    Content: HttpContent;
    Cert: text;
    Password: Text;
    myTxtXml: Text;
    CertificateEnabled: Boolean;
    lTxtWebServiceUrl: Text;
    lTxtStatusDescp: Text[250];
    lTxtResponse: Text;
begin
    GetAndCheckSetup();
    CertificateEnabled := GetCertificateInfo(Cert, Password);
    if not CertificateEnabled then begin
        ProcessResponseCommunicationError(NoCertificateErr, '');
        exit;
    end;


    lTxtWebServiceUrl := 'someValidUrlEndPoint';

    StoreRequestXml(pXmlDocument);
    Clear(RequestMessage);
    Clear(Content);
    Clear(Headers);
    Clear(Client);

    RequestMessage.SetRequestUri(lTxtWebServiceUrl);
    RequestMessage.Method := 'POST';

    pXmlDocument.WriteTo(myTxtXml);
    Content.WriteFrom(myTxtXml);
    Content.GetHeaders(Headers);
    Headers.Remove('Content-Type');
    Headers.Add('Content-Type', 'application/xml');

    RequestMessage.Content := Content;
    Client.AddCertificate(Cert);//Client.AddCertificate(Cert,Password);

    if not Client.Send(RequestMessage, ResponseMessage) then begin
       ProcessResponseCommunicationError(NoResponseErr, '');
        exit;
    end else begin
        Headers := ResponseMessage.Headers;
        Content := ResponseMessage.Content;
        lTxtStatusDescp := Format(ResponseMessage.HttpStatusCode());
        ResponseMessage.Content.ReadAs(lTxtResponse);
        if not ResponseMessage.IsSuccessStatusCode() then begin
            ProcessResponseCommunicationError(
              StrSubstNo(CommunicationErr, lTxtStatusDescp),
              lTxtResponse);
            exit;
        end;
       ProcessResponse(lTxtResponse);
    end;
end;    

The response returns a "Not valid identification or digital certificate not valid.", but gives me a 200 status. The certificate its added in Base64, working for another uses, and check with a decode tool with valid information ; 

pastedimage1654007922644v1.png

Can someone help me with the code review or if there is any problem in associating the certificate to the web service call?

Thank you,
I have the same question (0)
  • Suggested answer
    cperezsx Profile Picture
    60 on at
    RE: HttpClient.Send with certificate

    Hi,

    The problem its solved, only try another way ;

    - Load certificate as blob in a table field.

    - Create inStream from blob and convert to base64 (Client.AddCertificate(Base64Convert.ToBase64(InStream), CertificatePassword);)

    I hope that helps someone with the same problem.

    Regards,

  • Suggested answer
    Nitin Verma Profile Picture
    21,684 Moderator on at
    RE: HttpClient.Send with certificate

    Good. You can mark this thread as closed.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
Rishabh Kanaskar Profile Picture

Rishabh Kanaskar 4,237

#2
Nimsara Jayathilaka. Profile Picture

Nimsara Jayathilaka. 3,425

#3
Sumit Singh Profile Picture

Sumit Singh 2,907

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans