Fórum Consumir API REST/JSON com certificado DIgital #590106

27/12/2017

0

Bom dia Pessoal!
Tenho que consumir um servidor REST, estou usando os componentes nativos do RAD Stúdio Berlin. (RESTClient, RESTRequest, RESTResponse).
Só que agora o servidor quer que envie os arquivos JSON por HTTPS assinado por um certificado digital, faz uns 15 dias que estou tentando e não obtive sucesso, alguém já fez isso utilizando o DELPHI?
Hoje faço dessa forma: (sem Assinatura)
RESTClient1.BaseURL := 'https://hom.api.ms.gov.br/apifrig/classificadores?ie_frigorifico=99999999&cpf_classificador=99999999999&ie_empresa=999999999';
RESTRequest1.Method := TRESTRequestMethod.rmPOST;
RESTRequest1.Body.Add(jObj.ToString,ContentTypeFromString('application/json'));
RESTRequest1.Execute;

Segue o exemplo em anexo em C# como deveria funcionar.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Security;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleCertificadoIF
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(CallApi(GetClientLocalCertificate()));
Console.ReadLine();
}
private static string CallApi(X509Certificate2 clientCertificate)
{
var urlApi = "https://hom.api.ms.gov.br/apifrig/classificadores?ie_frigorifico=283962682&cpf_classificador=06463215368&ie_empresa=284192775";
if (clientCertificate != null)
{
try
{
var handler = new WebRequestHandler();
handler.ClientCertificateOptions = ClientCertificateOption.Manual;
handler.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
handler.ClientCertificates.Add(clientCertificate);
var client = new HttpClient(handler);
var response = client.GetAsync(urlApi).Result;
if (response.IsSuccessStatusCode)
{
var dataObjects = response.Content.ReadAsStringAsync();
return dataObjects.Result;
}
else
{
return "Erro";
}
}
catch (Exception ex)
{
throw;
}
}
else
{
return "Certificate is null!";
}
}
private static X509Certificate2 GetClientLocalCertificate()
{
try
{
var certificate = new X509Certificate2(@"D:\\certificado.pfx", "senha", X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet);
return certificate;
}
catch (Exception ex)
{
throw;
}
}
}
}
Obrigado.
Leandro Melo

Leandro Melo

Responder

Posts

06/04/2018

Rub Z

Olá,

Estou pesquisando a mesma informação. Conseguiu resolver seu problema? Poderia informar como fez?

Muito obrigado!!
Responder

Gostei + 0

09/04/2018

Leandro Melo

Bom dia Rub.
Utilizei o C# mesmo. Não encontrei uma solução em Delphi.
Responder

Gostei + 0

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar