Download zipball from github in C#

Need to download a zipball from given link in C#

YES! I searched the net and stackoverflow and been trying to accomplish this seemingly impossible task for hours...

Ironically, in Curl it's single line and works like charm..

curl -L https://api.github.com/repos/username/reponame/zipball > repo.zip

I want to do in C# same thing curl does above...

Tried WebClient.DownloadFile() Gives

forbidden (403)

Tried async method too Gives

0 bye file (no error/exception)

Tried HttpClient Datadownload and File stream writer, give same errors as above. Seams like streamwirter is not invoked at all so it's failing to access the server that's the main part of my problem

I have Octokit.NET installed but it lacks documentation so I'm not even sure where to start doing this (probably it's WebClient version but I did try that in .NET libs)

Found this answer but didn't really understand it (Cannot get repository contents as .zip file (zipball) in Octokit.net)

Even tried to run .sh script in C# but it gives me exception that it can't run this kind of shell on this OS

Jon Skeet
people
quotationmark

When I tried this with WebClient, I didn't get a 403, but an exception:

System.Net.WebException: The server committed a protocol violation. Section=ResponseStatusLine

Looking up other questions with the same error, I found that the GitHub API server requires a user agent to be set. After that, it was trivial:

using System;
using System.Net;

class Test
{
    static void Main()
    {
        using (var client = new WebClient())
        {
            client.Headers.Add("user-agent", "Anything");
            client.DownloadFile(
                "https://api.github.com/repos/nodatime/nodatime/zipball",
                "nodatime.zip");
        }
    }
}

... that worked fine. I've tried it for a user repo instead of an organization, and that was fine too.

You definitely wouldn't want to do anything with a StreamWriter, as that's for text data - and a zip file isn't text data.

You haven't shown what you did with the async version - my guess is that you started downloading but didn't wait until it had completed before disposing the client.

people

See more on this question at Stackoverflow