C#webclient.downloaddata()获取损坏的数据和固定大小的数据,而与该URI托管的数据的大小无关



我们正在尝试在VSTS的HTML字段中获取内联图像的附件。

代码在C#中。我们正在使用Visual Studition 2015 Community Edition和Microsoft Team Foundation Server对象模型2012和.NET Framework 4.0或更高。

附件uri变量值下面的代码:https://opshubautomation.visualstudio.com/workitemtracking/v1.0/attachfilehandler.ashx?filenameguid=20157b52-3c9a-4bbbb7-bc63-bc63-bc63-f6b38fc1d54fc1d54fc1d54cc1d54cc11d54cm am

下面提到了代码的摘要。

{
WebClient webClient = new WebClient();
webClient.Credentials = (ICredentials)connectionInfo.tfsServerConfig.Credentials;
    // 1. Approach 1 using webClient.DownloadFile() which downloads the file in current directory locally
    webClient.DownloadFile(attachmentURI, "aoaob.png");
    // 2. Approach 2 using webClient.DownloadData() which loads the byte array of the attachment hosted at given // attachment URI
    byte[] data = webClient.DownloadData(attachmentURI);
    return data;
}

使用上述代码,我们尝试访问 tfs版本2013、2015和2017 中托管的相同的内联图像(在实体的任何HTML字段中)。对于TFS的每个版本,我们都可以加载正确的内线图像附件输入流。但是相反,使用相同的代码和其他基础架构,我们尝试在 Microsoft VSTS (相同的实体和相同的HTML字段)中加载附件输入流,并且每次获得损坏的PNG文件无论使用上述附件URI获取图像的大小如何,大约14 kb的尺寸。托有上方附件URI的PNG图像的原始大小为113 kb。

但是,我们仍然使用上述方法无法在Microsoft VST中获得正确的输入流(而在TFS中加载的所有版本的正确图像)。

请帮助我们解决此问题或告诉我们任何我们做错了的事情。

这可能是由使用VST的身份验证引起的。TFS不同。要使用WebClient对VST进行身份验证,您需要启用替代凭据或创建个人访问令牌,然后使用基本AUTH,以下是您的参考的代码示例:

    WebClient wc = new WebClient();
    string altusername = "xxxxxx";
    string altpassword = "xxxxxx";
    string auth = altusername + ":" + altpassword;
    auth = Convert.ToBase64String(Encoding.Default.GetBytes(auth));
    wc.Headers["Authorization"] = "Basic" + auth;
    Uri uri = new Uri("https://xxxxxxxx");
    wc.DownloadFile(uri, "aoaob.png");

最新更新