简体   繁体   English

C#WebClient.DownloadData()提取损坏的数据和固定大小的数据,而不管该URI上托管的数据的大小如何

[英]C# WebClient.DownloadData() fetches corrupted data and a data of fixed size irrespective of the size of the data hosted at that URI

We are trying to fetch the attachment inputstream of an inline image in HTML field of VSTS. 我们正在尝试在VSTS的HTML字段中获取嵌入式图像的附件输入流。

The code is in C#. 该代码在C#中。 We are using Visual Studion 2015 community edition and Microsoft Team Foundation Server Object Model 2012 and .NET framework 4.0 or higher. 我们正在使用Visual Studion 2015社区版和Microsoft Team Foundation Server对象模型2012和.NET Framework 4.0或更高版本。

Attachment URI variable value for below code : https://opshubautomation.visualstudio.com/WorkItemTracking/v1.0/AttachFileHandler.ashx?FileNameGuid=20157b52-3c9a-4bb7-bc63-f6b38fc1d54c&FileName=Att.png 以下代码的附件URI变量值: https : //opshubautomation.visualstudio.com/WorkItemTracking/v1.0/AttachFileHandler.ashx?FileNameGuid= 20157b52-3c9a-4bb7-bc63-f6b38fc1d54c &FileName=Att.png

The snippet of the code is mentioned below. 下面是该代码的片段。

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

Using the above code, we have tried accessing the same inline image (in any HTML field of an entity) hosted in TFS versions 2013, 2015, and 2017 . 使用上面的代码,我们尝试访问托管在TFS 2013、2015和2017版本中的相同内联图像(在实体的任何HTML字段中)。 For each versions of TFS, we are able to load a correct inline image attachment input stream. 对于每个版本的TFS,我们都能够加载正确的嵌入式图像附件输入流。 But on the contrary, using the same code and other infrastructures, we have tried loading the attachment input stream in Microsoft VSTS (same entity and same HTML field), and every time we get a corrupted PNG file of approximately 14 KB size irrespective of the size of the image being fetched by using the above attachment URI. 但是相反,使用相同的代码和其他基础结构,我们尝试将附件输入流加载到Microsoft VSTS (相同的实体和相同的HTML字段)中,并且每次我们收到大约14 KB大小的损坏的PNG文件时,无论使用上述附件URI获取的图像的大小。 The original size of the PNG image hosted at above attachment URI is 113 KB. 上述附件URI上托管的PNG图像的原始大小为113 KB。

But, still we are not able to get a correct input stream in entity in Microsoft VSTS using the above method (while correct image loaded in TFS all versions mentioned above). 但是,仍然不能使用上述方法在Microsoft VSTS中的实体中获得正确的输入流(而在上述所有版本的TFS中加载正确的图像)。

Please help us to resolve this issue or tell us anything that we are doing wrong. 请帮助我们解决此问题,或告诉我们我们做错了什么。

This could be caused by the authentication with VSTS. 这可能是由VSTS身份验证引起的。 It is different with TFS. 与TFS不同。 To authenticate to VSTS with WebClient, you need to enable alternate credential or create a Personal Access Token and then use basic auth, following is the code sample for your reference: 要使用WebClient向VSTS进行身份验证,您需要启用备用凭据或创建个人访问令牌,然后使用基本身份验证,以下是代码示例供您参考:

    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");

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM