如何使用API​​和C#将文件从天蓝色DevOps服务器下载到指定路径 [英] How download file form azure DevOps sever to a specified path using API and c#

查看:154
本文介绍了如何使用API​​和C#将文件从天蓝色DevOps服务器下载到指定路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用API​​从azure devops服务器下载文件.API给出了成功响应,但是没有文件被下载.如果我们删除format参数,我们将获得文件链接的响应.已下载.

I am trying to download a file from azure devops server using API.API give a success response.But no files getting downloaded.If we remove format parameter, we will get response with file link.by clicking that file is not getting downloaded.

示例代码

var personalaccesstoken = "ic2pqkzaeqv2dummyTokenDummypa";
                client1.BaseAddress = new Uri("https://dev.azure.com/organisatioTest/ProjectABC");
                client1.DefaultRequestHeaders.Accept.Clear();
                client1.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                client1.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
                   Convert.ToBase64String(
                       System.Text.ASCIIEncoding.ASCII.GetBytes(
                           string.Format("{0}:{1}", "", personalaccesstoken))));
                HttpResponseMessage response1 = client1.GetAsync("_apis/git/repositories/e9e2f082-6f6d-99999b0bcc737a/items?scopePath=/Vjjj/DB/Data/ABC.sql&download=true&$format=zip&api-version=5.1").Result;

是否应该添加其他C#代码来下载文件?我们如何指定下载路径?

Should we add additional c# code to get file download ? How can we specify the download path ?

推荐答案

在C#中,可以这样构造URL(假定变量是通过方法调用传递的):

In C# this URL can be constructed like this (assume the variables are being passed in via a method call):

    var url = $" https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/items?path={path}&api-version=5.1";

使用正确格式的URL,然后我们可以使用WebRequest类来调用API.例如:

With the properly formatted URL we can then make a call to the API using the WebRequest class. For example:

var request = (HttpWebRequest)WebRequest.Create(address);
request.UserAgent = "VSTS-Get";
request.Headers.Set(HttpRequestHeader.Authorization, GetAuthenticationHeaderValue(authentication).ToString());
request.ContentType = "application/json";
request.Method = httpMethod;
response = (HttpWebResponse)request.GetResponse();

使用HttpWebResponse对象,我们可以获得一个Stream对象,然后可以将其用于将API结果写到本地文件中.例如:

Using the HttpWebResponse object we can obtain a Stream object that we can then use to write out the API results to a local file. For example:

using (var responseStream = GetResponseStream(response))
{     var fileName = Path.Get Filename(fileToDownload ?? "");     using (var fileStream = File.Create(Path.Combine(destination, fileName)))     {         responseStream.CopyTo(fileStream);     }
}

请参考此链接以获取更多详细信息:样本博客

Please refer this link for more details: Get file API and sample blog

更新1

感谢sagar进行共享,有关更多详细信息,请参阅从azure devops服务器下载文件会将错误的数据写入该文件

Thanks sagar for sharing, for more details, please refer to Download a file from azure devops server writes wrong data to the file

我们应该尝试使用个人访问令牌(PAT).为了使用PAT进行身份验证,我们必须使用授权作为基本"身份验证.而不是将PAT单独添加到Request标头中,我们必须使用用户名和PAT的组合.说用户名:PAT的base-64编码字符串.

We should try to use Personal access token(PAT).In order to use PAT for authentication we have to use authorization as "Basic" instead of "Bearer".More over instead of adding PAT alone to the Request header we have to use combination of username and PAT.Say base-64-encoded-string of username:PAT.

示例代码:

var personalaccesstoken = "wwwwwwwwwwwwwwwwwwy47b7ugkz32bubi64bw7fqdyfpa";
 var base64Creds = Convert.ToBase64String(Encoding.ASCII.GetBytes("SPabbbal@ABCTech.com:"+ personalaccesstoken));
request.Headers.Set(HttpRequestHeader.Authorization, "Basic " + base64Creds);

这篇关于如何使用API​​和C#将文件从天蓝色DevOps服务器下载到指定路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆