Error downloading zip files c # restsharp

0

I have a api rest that allows me to download through a URL records of each month, the month before the current one presents it in a .csv file and the others in zip format, the structure of the URL is for the previous month to the current:

And for the previous months:

I have a cycle that goes through and modifies the year and making the respective request to the API, the previous month that is in csv format I download it correctly, but the zip files download me in the wrong way, they go down with the name I put them, but they all weigh 1kb and obviously when opening they throw error, could someone tell me what the error is? This is my code:

for (int i = 0; i < totalMonths; i++)
{
    if (i==0)
    {
        RestClient clientMa;

        if ((i + 1) > 9)
        {
            clientMa = new RestClient("https://URL.com/" + networkId + "/files/csv/vw/" + cod + "-readings-" + añoAct + "-" + (mesAct - 1) + ".csv");
        }
        else
        {
            clientMa = new RestClient("https://url.com/" + networkId + "/files/csv/vw/" + cod + "-readings-" + añoAct + "-" + "0"+(mesAct - 1) + ".csv");
        }
        var requestMa = new RestRequest(Method.GET);
        requestMa.AddHeader("Cache-Control", "no-cache");
        requestMa.AddHeader("Authorization", "Basic XXXXXX");
        if ((i + 1) > 9)
        {
            clientMa.DownloadData(requestMa).SaveAs(cod + "-readings-" + añoAct + "-" +(mesAct - 1) + ".csv");
        }
        else
        {
            clientMa.DownloadData(requestMa).SaveAs(cod + "-readings-" + añoAct + "-" + "0" + (mesAct - 1) + ".csv");
        }
    }
    else
    {
        RestClient client;
        RestRequest request;

        if ((i + 1) > 9)
        {
            client = new RestClient("https://url.com/" + networkId + "/files/csv/vw/" + cod + "-readings-" + añoAct + "-" + (mesAct - (i-2)) + ".zip");
            client.Authenticator = new HttpBasicAuthenticator(username, password);
            request = new RestRequest(Method.GET);
            client.DownloadData(request).SaveAs(cod + "-readings-" + añoAct + "-" +(mesAct - 1) + ".zip");
        }
        else
        {
            client = new RestClient("https://url.com/" + networkId + "/files/csv/vw/" + cod + "-readings-" + añoAct + "-" + "0" + (mesAct - 1) + ".zip");
            client.Authenticator = new HttpBasicAuthenticator(username, password);
            request = new RestRequest(Method.GET);
            client.DownloadData(request).SaveAs(cod + "-readings-" + añoAct + "-" + "0" + (mesAct - (i - 2)) + ".zip");
        }
    }
}

As I repeat the csv file is downloaded correctly, but it is impossible to download the previous ones in .zip.

    
asked by DVertel 11.09.2018 в 16:07
source

0 answers