使用校验和(C#)部署到Artifactory的问题 [英] Problem Deploying to Artifactory with Checksum (C#)
问题描述
好的,
好吧,我正在尝试弄清楚该怎么做.
我已经准备好了API,但这仍然行不通.
Okay,
Well, I'm trying to figure out how to do this.
I've ready the API and and yet this never works.
using (HttpClient client = new HttpClient())
{
if (!path.EndsWith("/")) path = $"{path}/";
string url = config.CreateRequest(client, null, $"{path}{file.Name}");
string sha1 = JFrogLoader.GetSha1Hash(file);
string sha256 = JFrogLoader.GetSha256Hash(file);
string md5 = JFrogLoader.GetMD5Hash(file);
using (Stream stream = file.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
StreamContent content = new StreamContent(stream);
client.DefaultRequestHeaders.Add("X-Checksum-Deploy", "true");
client.DefaultRequestHeaders.Add("X-Checksum-Sha1", sha1);
client.DefaultRequestHeaders.Add("X-Checksum-Sha256", sha256);
client.DefaultRequestHeaders.Add("X-Checksum", md5);
content.Headers.Add("Content-Type", "application/octet-stream");
HttpResponseMessage message = await client.PutAsync(url, content);
string response = await message.Content.ReadAsStringAsync();
return message.StatusCode == System.Net.HttpStatusCode.Created;
}
}
问题是,如果我不使用任何"X-Checksum-"
标头项目,则部署可以工作,但是当您导航到Artifactory中的页面时,它具有修复校验和"按钮.所以我想我应该提供给他们.
我生成校验和的方法使用"* CryptoServiceProvider"类,并从计算出的哈希字符串中修剪最终的"=".但是,每次我添加校验和标头时,都会收到一个多部分的异常:无法从传输连接读取数据:现有连接被远程主机强行关闭".
The thing is, if I don't use any of the "X-Checksum-"
header items, the deploy works, but when you navigate to the page in Artifactory, it has the "Fix Checksum" button. So I figured I should probably provide them.
My methods that generate the checksums use the "*CryptoServiceProvider" classes, and trim the final '=' from the computed hash string. But every time I add in the checksum headers, I get a multi-part exception for "Unable to read data from the transport connection : An existing connection was forcibly closed by the remote host".
我尝试使用content.Headers
和client.DefaultRequestHeaders
.
我尝试只提供SHa1
.
我尝试命名md5
X-Checksum-Md5
(它不在api中,但认为值得一试).
什么都行不通,并且主机断开了连接.
I've tried using the content.Headers
and client.DefaultRequestHeaders
.
I've tried only providing the SHa1
.
I've tried naming the md5
X-Checksum-Md5
(which is not in the api, but figured it was worth a shot).
Nothing works, and i get a connection closed by host.
有什么想法我应该如何解决呢? 预先感谢.
Any ideas how I should resolve this? Thanks in advance.
推荐答案
好吧,花了一段时间,但经过一些反复试验,我发现问题出在我使用的has算法中.
Well, took a while but using a little trial and error, i found the problem was in the has algorithm I was using.
通过带有校验和的C#加载此文件的正确解决方案如下:
The Correct Solution to load this via C# with checksums is as follows:
using (HttpClient client = new HttpClient())
{
if (!path.EndsWith("/")) path = $"{path}/";
string url = config.CreateRequest(client, null, $"{path}{file.Name}");
string sha1 = JFrogLoader.GetSha1Hash(file);
string sha256 = JFrogLoader.GetSha256Hash(file);
string md5 = JFrogLoader.GetMD5Hash(file);
using (Stream stream = file.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
StreamContent content = new StreamContent(stream);
content.Headers.Add("Content-Type", "application/octet-stream");
client.DefaultRequestHeaders.Add("X-Checksum-Deploy", "true");
client.DefaultRequestHeaders.Add("X-Checksum-Sha1", sha1);
client.DefaultRequestHeaders.Add("X-Checksum-Sha256", sha256);
client.DefaultRequestHeaders.Add("X-Checksum-Md5", md5);
HttpResponseMessage message = await client.PutAsync(url, content);
string response = await message.Content.ReadAsStringAsync();
return message.StatusCode == System.Net.HttpStatusCode.Created;
}
}
public static string GetSha1Hash(FileInfo file)
{
using (var sha1 = new SHA1CryptoServiceProvider())
{
return JFrogLoader.GetHash(file, sha1);
}
}
public static string GetSha256Hash(FileInfo file)
{
using (SHA256CryptoServiceProvider sha256 = new SHA256CryptoServiceProvider())
{
return JFrogLoader.GetHash(file, sha256);
}
}
public static string GetMD5Hash(FileInfo file)
{
using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider())
{
return JFrogLoader.GetHash(file, md5);
}
}
private static string GetHash(FileInfo file, HashAlgorithm hasher)
{
using (Stream stream = file.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
byte[] hash = hasher.ComputeHash(stream);
return BitConverter.ToString(hash).Replace("-", "").ToLower();
}
}
键最初是我使用的是Convert.ToBase64String而不是BitConverter的直十六进制字符串.可悲的是,Artifcatory的错误消息不清楚,并在Web请求中引发了异常.
The key was originally i was using a Convert.ToBase64String instead of the BitConverter to a straight hex string. Tragically, Artifcatory was not clear in it's error message, and triggered an exception in the web request.
谢谢
这篇关于使用校验和(C#)部署到Artifactory的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!