本文介绍了下载GitHub的私人回购协议的C#示例编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到的下载路径的GitHub库的形式为

I see that the download path for a GitHub repo is of the form

https://github.com/{username}/{reponame}/archive/{branchname}.zip

有关私人回购协议,理解你需要提供凭据才能下载回购,任何人都可以提供关于如何提供HTTPS基本身份验证,所以我可以编程下载回购一个C#示例?

For a private repo, understandably you need to provide credentials in order to download the repo, can anyone provide a C# example on how to provide a HTTPS basic authentication so I can download the repo programmatically?

谢谢

推荐答案

下面是一个纯C#的方式:

Here is a pure C# way:

var githubToken = "[token]";
var url = "https://github.com/[username]/[repository]/archive/[sha1|tag].zip";
var path = @"[local path]";

using (var client = new System.Net.Http.HttpClient())
{
    var credentials = string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0}:", githubToken);
    credentials = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(credentials));
    client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", credentials);
    var contents = client.GetByteArrayAsync(url).Result;
    System.IO.File.WriteAllBytes(path, contents);
}

这篇关于下载GitHub的私人回购协议的C#示例编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 10:45