How to send WS XML file from Safe Harbour (made in PHP) using Restsharp C#

Asked

Viewed 674 times

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 within array. Or you move up the file C:\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.

  • I’ve done this test too, it didn’t work

  • 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, I made this change, but returns from Porto Seguro’s WS as if I had not added any file to the request.

2 answers

1

Instead of doing all this process you can simply use the method addFile ex:

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);
        restRequest.AddHeader("Content-Type", "multipart/form-data");
        request..AddFile("content", "C:\\XML0702794600019004-05-20188001.xml");

        IRestResponse response = client.Execute(request);
    }
  • And the fields comp, mod, file, path and dump as he passes in this requisition?

  • 1

    I just changed the inside of the if to give the example, the rest Voce already set up there, just keep

  • It was the first attempt, but doing so with addFile returns from the safe port WS as if there were no files in the request, so I tried to copy exactly the Postman code that works correctly on it, so it is this way. And for comp, mod, file, path and dump?

1


It was solved by not using Restsharp.

public async void Testar()
    {

        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);
        var restResponseCookie = new RestResponseCookie();
        if (login.StatusCode == HttpStatusCode.OK)
        {
            var cookies = login.Cookies;


            restResponseCookie = cookies[0];
        }


        System.Net.WebClient oWeb = new System.Net.WebClient();

        oWeb.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
        oWeb.Headers.Add(HttpRequestHeader.Cookie, restResponseCookie.Name + "=" + restResponseCookie.Value);
        NameValueCollection parameters = new NameValueCollection();
        parameters.Add("comp", "5");
        parameters.Add("mod", "Upload");
        parameters.Add("path", "eguarda/php/");
        parameters.Add("dump", "1");
        parameters.Add("Content-Type", "application/xml");

        oWeb.QueryString = parameters;

        var responseBytes = oWeb.UploadFile(link, @"C:\Teste\XML0702794600019004-05-20188001.xml");
        string response = Encoding.ASCII.GetString(responseBytes);

    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.