我有以下curl命令,我需要将其转换为C#
curl -X PUT --data-binary @Invoice_AGL_20210408.003.xml --user [xxxx]:[xxxxxxxxxxxxxxx] https://sftp.voxelgroup.net:19922/outbox/Invoice_AGL_20210408.003.xml
我实现了以下代码,但出现错误。代码中可能存在什么错误。?
using (var httpClient = new HttpClient())
{
using (var request = new HttpRequestMessage(new HttpMethod("PUT"), https://sftp.voxelgroup.net:19922/outbox/Invoice_AGL_20210408.003.xml"))
{
var base64authorization = Convert.ToBase64String(Encoding.ASCII.GetBytes("[xxxx]:[xxxxxxxxxxxxxxx]"));
request.Headers.TryAddWithoutValidation("Authorization", $"Basic {base64authorization}");
request.Content = new StringContent(new StringContent(File.ReadAllText(@"C:\inetpub\AstroWeb\BavelExport\2022_10_29_03_16\Invoice20220016014.xml")););
request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/x-www-form- urlencoded");
var response = await httpClient.SendAsync(request);
}
}
尝试将 Curl 命令转换为 C# 代码。未成功建立连接
试试这个:
using System.Net.Http.Headers;
HttpClient client = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Put, "https://sftp.voxelgroup.net:19922/outbox/Invoice_AGL_20210408.003.xml))\n ");
request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes("[xxxx]:[xxxxxxxxxxxxxxx]")));
request.Content = new StringContent(File.ReadAllText("Invoice_AGL_20210408.003.xml").Replace("\n", string.Empty).Replace("\r", string.Empty));
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
HttpResponseMessage response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
您可以在浏览器上检查网络,将请求复制为 cURL 并在 https://hasdata.com/curl-to-csharp-converter
使用该服务