1
I am trying to integrate a Ct-e XML file with the Safe Harbor Web Service (done in PHP) using Restsharp in C# and always returns message saying that they could not open XML or that file is empty. Even putting the same code provided by Postman uses and does not work, it seems that the problem is in the way I send the file as they return error like if the file size was zero. Sending by Postman works.
I wanted to see if anyone has ever had this problem when doing the integration of XML CT-e file to avert via in the Safe Harbour JSON Web Service.
Below is an example of minimum code with my attempts and return received, as well as the link of the safe port manual.
I have made several attempts of different ways to include this XML in the request and without success. I hope you can help me. Thank you.
Code
// CÓDIGO
public static void testeIntegracaoRest()
{
var link = "https://www.averbeporto.com.br/websys/php/conn.php";
var client = new RestClient(link);
var request = new RestRequest(Method.POST);
var parametros = @"mod=login&comp=5&user=00234567000122&pass=0023";
request.AddHeader("cache-control", "no-cache");
request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddParameter("application/x-www-form-urlencoded", parametros, ParameterType.RequestBody);
var login = client.Execute(request);
if (login.StatusCode == HttpStatusCode.OK)
{
// continua
var cookies = login.Cookies;
var restResponseCookie = new RestResponseCookie();
restResponseCookie = cookies[0];
client = new RestClient("https://www.averbeporto.com.br/websys/php/conn.php");
request = new RestRequest(Method.POST);
request.AddCookie(restResponseCookie.Name, restResponseCookie.Value);
request.AddHeader("content-type", "multipart/form-data;boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
byte[] array = File.ReadAllBytes("C:\\XML0702794600019004-05-20188001.xml");
request.AddParameter("multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
"------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"comp\"\r\n\r\n5\r\n" +
"------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"mod\"\r\n\r\nUpload\r\n" +
"------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"path\"\r\n\r\neguarda/php/\r\n" +
"------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"file\"; " +
"filename=\"C:\\XML0702794600019004-05-20188001.xml"\r\nContent-Type: " + "application/xml\r\n\r\n\r\n" +
"------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"dump\"\r\n\r\n1\r\n",
ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
}
}
Comeback WS
// RETORNO WS
{"success":1,"S":{"P":0,"D":0,"R":0,"N":1},"prot":null,"error":{"msg":"Error opening XML","code":"03"},"dump":{"POST":{"comp":"5","mod":"Upload","path":"\/var\/www\/averbeporto.com.br\/web\/websys\/eguarda\/php\/","dump":"1","v":2,"ext":".php"},"COOKIE":{"portal":{"ses":"e002f119bb4d854b96ebd9c4c85b84f7"}},"FILES":{"file":{"name":"XML0702794600019004-05-20188001.xml","type":"application\/xml","tmp_name":"\/var\/www\/clients\/client0\/web124\/tmp\/phpqspsPj","error":0,"size":0}}}}
// LINK MANUAL PORTO SEGURO
https://docs.google.com/document/d/1da005UzBF1Wzm8LmiB4JJnaXaLXtFKgl6S_rErMlXF8/edit
When does
"filename=\"" + array +...
you are stating that the file name is the XML content withinarray
. Or you move up the fileC:\XML0702794600019004-05-20188001.xml
and makes"filename=\"" + [URL do Arquivo no Servidor]+...
or search the Safe Harbor API for a field to put this data.– Augusto Vasques
I’ve done this test too, it didn’t work
– Lucas Fernandes
And if you upload the file and change that line
""filename=\""..."
this way:"------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"[URL do Arquivo no Servidor];Content-Type: " + "application/xml\"; " +
– Augusto Vasques
@Augusto, I made this change, but returns from Porto Seguro’s WS as if I had not added any file to the request.
– Lucas Fernandes