【问题标题】:C# HttpClient broken pipe, but curl worksC# HttpClient 管道损坏,但 curl 有效
【发布时间】:2020-04-11 16:35:21
【问题描述】:

我有一个正常工作的 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

编辑:我通过 Charles 发送了 HttpClient 代码以查看发生了什么,然后它起作用了... :( 所以,我无法判断出了什么问题. 酷。

【问题讨论】:

  • 你发现问题出在哪里了吗?我相信 .NET Core 在发送长流时存在随机问题。
  • 我在写入 Azure 缓存时也遇到了同样的问题。你解决了这个问题吗?

标签: c# curl .net-core network-programming


【解决方案1】:

我在尝试使用来自 AWS Code Build 中的 ubuntu 构建代理的 dotnet nuget push 命令将 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-pipe-errors

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-27
    • 2010-10-28
    • 2011-03-19
    • 1970-01-01
    • 2016-01-08
    • 2012-05-15
    • 1970-01-01
    相关资源
    最近更新 更多