3

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

Community
  • 1
  • 1
Like a Fox
  • 49
  • 1
  • 6
  • 3
    In future, please show the code you've tried, ideally in a short but complete program. For example, I think I know why the async version *might* have ended with a 0 byte file, but I can't say for sure without seeing your code. – Jon Skeet Aug 26 '15 at 10:24

1 Answers1

5

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • OK, it works,and you are my hero! Please tell me what I did wrong/what you did right? What is user agent? Is that like making browser-like call to a link? As for Async, I read somewhere on SOF that it begins to write to a file before it starts to receve data and continues to write as data come... So it was my bad call to link... Thank you so much! – Like a Fox Aug 26 '15 at 16:54
  • @LikeaFox: The user agent is meant to be an indication of the software making the request - e.g. a browser identifier. It looks like GitHub requires *some* user agent to be specified, but doesn't care what it is. – Jon Skeet Aug 26 '15 at 16:55
  • I noticed it downloads fine in browser... I was thinking how can I do browser-like request but I've never done it before and didn't know how... Is there a way to know it requires user agent? Can you quickly give me an example of user agent that should work on most websites? Thanks again! – Like a Fox Aug 26 '15 at 17:04
  • I suggest you do research on commonly-used user agents... it really depends on what the server decides to enforce though. It's a little odd that GitHub needs this... – Jon Skeet Aug 26 '15 at 17:36