首页 > 解决方案 > 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通过查尔斯发送代码以查看发生了什么,然后它可以工作...... :(所以,我无法判断出了什么问题。酷。

标签: c#curl.net-corenetwork-programming

解决方案


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 -管道错误


推荐阅读