I have created two methods, one for authentication via certificate type A1 and A3 and another to send an xml, both of which make the request in a client apiect.
In the authentication method, I send the data from my certificate to the client and have the return of a "Set-Token" with user information, "X-CSRF-Token" CSRF and " CSRF-Expiration "with the expiration date.
Authentication method.
public void CarregaCertificado()
{
string destinationUrl = "https://val.portalunico.siscomex.gov.br/portal/api/autenticar";
string requestXml = "Aqui é a string do xml";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(destinationUrl);
X509Certificate2 oCertificado;
var oX509Cert = new X509Certificate2();
var store = new X509Store("MY", StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
var collection = store.Certificates;
var collection2 = collection.Find(X509FindType.FindByKeyUsage, X509KeyUsageFlags.DigitalSignature, false);
var scollection = X509Certificate2UI.SelectFromCollection(collection2,
"Certificado(s) Digital(is) disponível(is)", "Selecione o certificado digital para uso no aplicativo",
X509SelectionFlag.SingleSelection);
if (scollection.Count == 0)
{
var msgResultado =
"Nenhum certificado digital foi selecionado ou o certificado selecionado está com problemas.";
MessageBox.Show(msgResultado, "Advertência", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
oX509Cert = scollection[0];
oCertificado = oX509Cert;
request.ClientCertificates.Add(oCertificado);
request.ContentType = "application/json";
request.Headers.Add("Role-Type", "DEPOSIT");
request.Method = "POST";
HttpWebResponse response;
response = (HttpWebResponse)request.GetResponse();
string tokenResponse = string.Empty;
string csrfTokenResponse = string.Empty;
string csrfExpirionResponse = string.Empty;
string keepAlive = string.Empty;
string connection = string.Empty;
string contentLength = string.Empty;
string contentType = string.Empty;
string date = string.Empty;
string server = string.Empty;
if (response.StatusCode == HttpStatusCode.OK)
{
Stream responseStream = response.GetResponseStream();
tokenResponse = response.Headers["Set-Token"];
csrfTokenResponse = response.Headers["X-CSRF-Token"];
csrfExpirionResponse = response.Headers["X-CSRF-Expiration"];
keepAlive = response.Headers["Keep-Alive"];
connection = response.Headers["Connection"];
contentLength = response.Headers["Content-Length"];
contentType = response.Headers["Content-Type"];
date = response.Headers["Date"];
server = response.Headers["Server"];
string responseStr = new StreamReader(responseStream).ReadToEnd();
}
EnvioXML(requestXml, tokenResponse, csrfTokenResponse, csrfExpirionResponse);
}
}
Recebo os dados após autenticação sem problemas!
Depois tento enviar o xml.
Método de envio do xml.
public void EnvioXML(string requestXml, string tokenResponse, string csrfTokenResponse, string csrfExpirionResponse)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://val.portalunico.siscomex.gov.br/cct/api/ext/carga/entrega-conteiner");
byte[] bytes;
bytes = System.Text.Encoding.UTF8.GetBytes(requestXml);
request.ContentType = "application/xml";
request.Headers.Add("Authorization", tokenResponse);
request.Headers.Add("X-CSRF-Token", csrfTokenResponse);
request.ContentLength = bytes.Length;
request.ContentLength = 1;
request.SendChunked = true;
request.Method = "POST";
Stream requestStream = request.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Close();
try
{
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
if (response.StatusCode == HttpStatusCode.OK)
{
Stream responseStream1 = response.GetResponseStream();
}
}
}
catch (WebException e)
{
MessageBox.Show(e.Message);
}
}