简体   繁体   English

通过WebRequest下载“ Visual Studio Team Services”图表

[英]Download “Visual Studio Team Services” Charts via WebRequest

I need to upgrade an old TFS 2013 class for Visual Studio Team Services . 我需要为Visual Studio Team Services升级旧的TFS 2013类。 To get the Burndown-Chart I used to download the image via HttpWebRequest direcly from the url. 为了获得Burndown-Chart,我曾经通过HttpWebRequest从URL直接下载图像。

Somehow Iam not able to do this in VSTS . 我以某种方式无法在VSTS中做到这一点。 I always get the error message "invalid parameters" . 我总是收到错误消息“无效参数” Everything else works fine. 其他一切正常。 (I had to setup the Alternate authentication credentials in my profile to get it working for my application) (我必须在个人资料中设置备用身份验证凭据才能使其适用于我的应用程序)

Here my code: 这是我的代码:

    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
            }
        }
    }

The url which gets passed as a parameter usally looks like this: 通常作为参数传递的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 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

What I think the problem is: 我认为问题是:

First I thought this might be a security issue , because this code is able to download normal google images. 首先,我认为这可能是一个安全问题 ,因为此代码能够下载常规的Google图片。 And when I try to get the content of the url It returns a lot of code with a message in it: 当我尝试获取url的内容时,它将返回很多代码,并带有一条消息:

Microsoft Internet Explorer's Enhanced Security Configuration is currently enabled on your environment. 当前在您的环境中启用了Microsoft Internet Explorer的增强安全配置。 This enhanced level of security prevents our web integration experiences from displaying or performing correctly. 增强的安全级别阻止了我们的Web集成体验正确显示或执行。 To continue with your operation please disable this configuration or contact your administrator 要继续操作,请禁用此配置或与管理员联系

I set my Internet security settings to the lowest level and still the same result. 我将Internet安全设置设置为最低级别,但结果仍然相同。

Another reason why this might not working is, because the url linking to the burndown-chart doesnt contain an Image extension . 之所以不起作用的另一个原因是,链接到燃尽图的网址不包含Image扩展名 Iam not quite shure here this effects the result. 我不太清楚这会影响结果。

Or that the parameters which are getting past in the url are incorrect... 或网址中传递的参数不正确...

What I have tried so far: 到目前为止我尝试过的是:

I have used bunch of other code to get the image from that link. 我使用了许多其他代码来从该链接获取图像。 For example using WebClient or tried to upload cookies (credentials) to the tfs and than tried to connect. 例如,使用WebClient或尝试将cookie(凭证)上传到tfs,然后尝试进行连接。

My Question 我的问题

Is it possible to get that image from the chart via url, and if so, how? 是否可以通过url从图表中获取该图像,如果可以,如何?

Thanks for any kind of help :). 感谢您的任何帮助:)。

EDIT 编辑

Currently Iam using this code (Thanks to @Eddie - MSFT): 当前Iam使用此代码(感谢@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)
     }

I use "httpclient" with alternative credential to do this: 我使用带有替代凭据的“ 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");
                }
        }
    }
}

Did you try the authentication with your personal access token instead of username and password, something like this? 您是否使用个人访问令牌而不是用户名和密码来尝试身份验证? I am using the below code to download the attachments, inline images of the Work items from VSTS. 我正在使用以下代码从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());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Visual Studio Team Services程序集版本控制 - Visual Studio Team Services Assembly Versioning Visual Studio Team Services-将附件附加到工作项 - Visual Studio Team Services - Attaching an attachment to a workitem MVC 5构建在Visual Studio Team Services上失败 - MVC 5 build failing on Visual Studio Team Services Visual Studio Team Services使用外部dll构建 - Visual Studio Team Services build with external dll 扩展Visual Studio Team Services(在TFS上在线) - Extend Visual Studio Team Services (was TFS Online) 从 WebService 连接到 Visual Studio Team Services - Connect to Visual Studio Team Services from WebService 通过REST API概述(用于Visual Studio Team Services和Team Foundation Server)从Microsoft vso WorkItem访问注释 - Accessing the comments from a microsoft vso WorkItem via REST API Overview for Visual Studio Team Services and Team Foundation Server 检测我的测试是否在Visual Studio Team Services上运行 - Detect if my tests are running on Visual Studio Team Services Visual Studio Team Services:为.netcore 1.1创建构建定义 - Visual Studio Team Services: create build definition for .netcore 1.1 使用Visual Studio Team Services进行自动用户界面测试 - Automated User Interface Testing with Visual Studio Team Services
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM