繁体   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

我们正在尝试在VSTS的HTML字段中获取嵌入式图像的附件输入流。

该代码在C#中。 我们正在使用Visual Studion 2015社区版和Microsoft Team Foundation Server对象模型2012和.NET Framework 4.0或更高版本。

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

下面是该代码的片段。

{
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字段)中,并且每次我们收到大约14 KB大小的损坏的PNG文件时,无论使用上述附件URI获取的图像的大小。 上述附件URI上托管的PNG图像的原始大小为113 KB。

但是,仍然不能使用上述方法在Microsoft VSTS中的实体中获得正确的输入流(而在上述所有版本的TFS中加载正确的图像)。

请帮助我们解决此问题,或告诉我们我们做错了什么。

这可能是由VSTS身份验证引起的。 与TFS不同。 要使用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