简体   繁体   中英

C# HttpClient broken pipe, but curl works

I have a curl command that works properly.

curl \
    -H "Authorization: token some-token" \
    -H "Content-Type: video/mp4" \
    --data-binary some-video.mp4 \
    "https://somewhere.com/upload-handler"

Here is what I believe the equivilant is in .NET Core's 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();
}

However, when running the .NET example, I get this.

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)

I'm running .NET Core 3.1, Ubuntu 18.04.

In case it matters, I'm trying to upload a binary to a GitHub release via the API: https://developer.github.com/v3/repos/releases/#upload-a-release-asset

edit : I sent the HttpClient code through Charles to see what was going on, and then it works... :( So, there is no way for me to tell what is wrong. Cool.

I get a similar error when trying to push nuget package to GitHub Package Registry using dotnet nuget push command from an ubuntu build agent in AWS Code Build. I push multiple nuget packages and this error only sporadically (90% of the time) occurs on the largest one (~5MB).

error: Error while copying content to a stream.
error:   Unable to read data from the transport connection: Broken pipe.
error:   Broken pipe

I am using dotnet sdk 2.2.

I could not find any useful information on this error anywhere, but this article gave me some food for thoughts https://support.sonatype.com/hc/en-us/articles/360000228868-Artifact-uploads-fail-with-broken-pipe-errors

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM