c# - C# HttpClient 管道损坏,但 curl 有效
问题描述
我有一个正常工作的 curl 命令。
curl \
-H "Authorization: token some-token" \
-H "Content-Type: video/mp4" \
--data-binary some-video.mp4 \
"https://somewhere.com/upload-handler"
这是我认为 .NET Core 中的等价物HttpClient
。
using (var stream = File.OpenRead("some-video.mp4"))
using (var httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.Add("Authorization", "token some-token");
var streamContent = new StreamContent(stream);
streamContent.Headers.ContentType = new MediaTypeHeaderValue("video/mp4");
var message = await httpClient.PostAsync(
$"https://somewhere.com/upload-handler",
streamContent);
message.EnsureSuccessStatusCode();
}
但是,在运行 .NET 示例时,我得到了这个。
Unhandled exception: System.Net.Http.HttpRequestException: Error while copying content to a stream.
---> System.IO.IOException: Unable to read data from the transport connection: Broken pipe.
---> System.Net.Sockets.SocketException (32): Broken pipe
--- End of inner exception stack trace ---
at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken)
at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 token)
at System.Net.Security.SslStream.<WriteSingleChunk>g__CompleteAsync|210_1[TWriteAdapter](ValueTask writeTask, Byte[] bufferToReturn)
at System.Net.Security.SslStream.WriteAsyncChunked[TWriteAdapter](TWriteAdapter writeAdapter, ReadOnlyMemory`1 buffer)
at System.Net.Security.SslStream.WriteAsyncInternal[TWriteAdapter](TWriteAdapter writeAdapter, ReadOnlyMemory`1 buffer)
at System.Net.Http.HttpConnection.WriteAsync(ReadOnlyMemory`1 source)
at System.IO.Stream.CopyToAsyncInternal(Stream destination, Int32 bufferSize, CancellationToken cancellationToken)
at System.Net.Http.HttpContent.CopyToAsyncCore(ValueTask copyTask)
--- End of inner exception stack trace ---
at System.Net.Http.HttpContent.CopyToAsyncCore(ValueTask copyTask)
at System.Net.Http.HttpConnection.SendRequestContentAsync(HttpRequestMessage request, HttpContentWriteStream stream, CancellationToken cancellationToken)
at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.SendWithNtConnectionAuthAsync(HttpConnection connection, HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
我正在运行 .NET Core 3.1、Ubuntu 18.04。
万一这很重要,我正在尝试通过 API 将二进制文件上传到 GitHub 版本:https ://developer.github.com/v3/repos/releases/#upload-a-release-asset
编辑:我HttpClient
通过查尔斯发送代码以查看发生了什么,然后它可以工作...... :(所以,我无法判断出了什么问题。酷。
解决方案
dotnet nuget push
尝试使用来自 AWS Code Build 中的 ubuntu 构建代理的命令将 nuget 包推送到 GitHub 包注册表时,我遇到了类似的错误。我推送了多个 nuget 包,这个错误只是偶尔(90% 的时间)发生在最大的包(~5MB)上。
error: Error while copying content to a stream.
error: Unable to read data from the transport connection: Broken pipe.
error: Broken pipe
我正在使用 dotnet sdk 2.2。
我在任何地方都找不到有关此错误的任何有用信息,但这篇文章给了我一些思考https://support.sonatype.com/hc/en-us/articles/360000228868-Artifact-uploads-fail-with-broken -管道错误
推荐阅读
- excel - 使用名称中带有“/”的 VBA 保存 excel 工作簿
- php - PHP用锚标记替换字符串中的URL
- c# - 无法检查 WinAppDriver 中的 Grids/StackPanels/Custom Controls 并通过 Appium 的 AccessibilityId 找到它们
- flutter - 具有标记人员功能的 Flutter TextField
- swift5 - 当我单击 UISearchController Swift 5 时,我的应用程序停止运行
- go - 用于服务间通信的基于 gRPC 的微服务架构
- javascript - 将函数 prop 传递给子组件(通过 Link 和 Route 组件)
- xamarin.forms - 如何在 Xamarin iOS 和 Android 在管道中构建任务之前替换 json 配置值
- python - 将二维向量多次添加到另一个二维向量的有效方法
- angular - 已选择 Angular 自动完成输入并模糊事件竞争条件