【发布时间】:2016-03-17 21:22:57
【问题描述】:
我需要为 Visual Studio Team Services 升级旧的 TFS 2013 类。 为了获得 Burndown-Chart,我使用 HttpWebRequest 直接从 url 下载图像。
不知何故,我无法在VSTS 中执行此操作。我总是收到错误消息“无效参数”。其他一切正常。 (我必须在我的个人资料中设置 备用身份验证凭据 才能使其适用于我的应用程序)
这是我的代码:
public Image GetChart(string uri)
{
HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(uri);
httpWebRequest.Credentials = new NetworkCredential("MyUserNameForApplication", "MyPWForApplication");
using (HttpWebResponse httpWebReponse = (HttpWebResponse)httpWebRequest.GetResponse())
{
using (Stream stream = httpWebReponse.GetResponseStream())
{
return Image.FromStream(stream); //Error occourse
}
}
}
作为参数传递的 url 通常如下所示:
https://YourVSName.visualstudio.com/DefaultCollection/a5d2310b-d3f8-4365-b693-3826ab60e939/_api/_teamChart/Burndown?chartOptions={%22Width%22%3A1248%2C%22Height%22%3A161%2C%22ShowDetails%22%3Atrue%2C%22Title%22%3A%22%22}&counter=1&iterationPath=Developing \Sprint+1&__v=5
我认为问题是:
首先我认为这可能是一个安全问题,因为这段代码能够下载正常的谷歌图片。当我尝试获取 url 的内容时,它会返回很多代码,其中包含一条消息:
Microsoft Internet Explorer 的增强安全配置当前已在您的环境中启用。这种增强的安全级别会阻止我们的 Web 集成体验正确显示或执行。要继续您的操作,请禁用此配置或联系您的管理员
我将 Internet 安全设置设置为最低级别,结果仍然相同。
这可能不起作用的另一个原因是,链接到燃尽图的网址不包含图片扩展名。我不太确定这会影响结果。
或者是url中传过来的参数不正确...
到目前为止我所做的尝试:
我使用了许多其他代码从该链接获取图像。例如使用 WebClient 或尝试将 cookie(凭据)上传到 tfs 并尝试连接。
我的问题
是否可以通过 url 从图表中获取该图像,如果可以,如何获取?
感谢您的任何帮助:)。
编辑
目前我正在使用此代码(感谢@Eddie - MSFT):
public static async void GetChart(string uri,string username, string password)
{
try
{
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Add(
new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
Convert.ToBase64String(
System.Text.ASCIIEncoding.ASCII.GetBytes(
string.Format("{0}:{1}", username, password))));
using (HttpResponseMessage response = client.GetAsync(uri).Result)
{
response.EnsureSuccessStatusCode();
var responseStream = await response.Content.ReadAsStreamAsync();
var img = Image.FromStream(responseStream);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
static void Main(string[] args)
{
string uri = "https://Name.visualstudio.com/DefaultCollection/a5d2310b-d3f8-4365-b693-3826ab60e939/_api/_teamChart/Burndown?chartOptions=%7B%22Width%22%3A1248%2C%22Height%22%3A636%2C%22ShowDetails%22%3Atrue%2C%22Title%22%3A%22%22%7D&counter=1&iterationPath=Developing%5CSprint+1&__v=5";
TFSHelper.TFSHelper.GetChart(uri, username,pw)
}
【问题讨论】:
-
那么,您使用的是 TFS 2015 还是 VSTS?它们是不同的东西。
-
我正在尝试访问 Visual Studio Team Services 上的数据。
标签: c# .net download azure-devops