【问题标题】:Download "Visual Studio Team Services" Charts via WebRequest通过 WebRequest 下载“Visual Studio Team Services”图表
【发布时间】: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


【解决方案1】:

我使用带有替代凭据的“httpclient”来执行此操作:

using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Net.Http;
using System.Net.Http.Headers;
using System.IO;

namespace GetImageA
{
    class Program
    {
        static void Main(string[] args)
        {
            Uri uri = new Uri("your image uri");
            GetImage(uri);
        }
        public static void GetImage(Uri uri)
        {
                var username = "username";
                var password = "password";

                using (HttpClient client = new HttpClient())
                {
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
                        Convert.ToBase64String(
                            ASCIIEncoding.ASCII.GetBytes(
                                string.Format("{0}:{1}", username, password))));
                    Stream str = client.GetStreamAsync(uri).Result;
                    Image im = Image.FromStream(str);
                    im.Save("E:\\image.png");
                }
        }
    }
}

【讨论】:

  • 还是同样的错误。当我使用我的正常凭据时,它会在“GetStreamAsync”处停止,并出现异常“System.AggregateException(响应状态代码不指示成功:401(未授权))”。当我使用我的应用程序身份验证时,我得到与以前相同的错误。它可能真的是一些 TFS 特定的东西。
  • @C0dingJammer 我也在使用此代码从 Visual Studio Team Service 获取图像。您是否使用通过此链接创建的替代凭据:visualstudio.com/en-us/integrate/get-started/auth/overview
  • 这就是我的做法。我将尝试使用 oauth 让它运行。 app.vssps.visualstudio.com/app/register。只需要在x之前做一些其他的事情)
  • @C0dingJammer 这很奇怪。你能分享一下你在 httpclient 中使用的代码吗?
  • @C0dingJammer 我刚刚注意到您在代码中使用的是“http”而不是“https”。你在问题中添加它时是否更改了它?当我尝试使用“http”时出现“参数无效”异常,但它适用于“https”。
【解决方案2】:

您是否尝试使用您的个人访问令牌而不是用户名和密码进行身份验证,类似这样的? 我正在使用以下代码从 VSTS 下载工作项的附件、内联图像。

try
   {
     var personalaccesstoken = "Your_VSTS_Personal_Access_Token";

     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}", "", personalaccesstoken))));

        using (HttpResponseMessage response = client.GetAsync(uri).Result)
        {
            response.EnsureSuccessStatusCode();
            string responseBody = await response.Content.ReadAsStringAsync();
            Console.WriteLine(responseBody);
        }
    }
}
catch (Exception ex)
{
    Console.WriteLine(ex.ToString());
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多