首页 > 解决方案 > C#下载文件并上传相同的文件作为流抛出SocketException

问题描述

我正在尝试重新上传一些文件。为此,我正在获取下载流并用 StreamContent 包装它。然后尝试上传该流。但我总是得到SocketException。我在这里错过了什么吗?甚至可以将流重定向到另一个地方吗?

_httpClient.DefaultRequestHeaders.Clear();
_httpClient.DefaultRequestHeaders.ConnectionClose = false;

var stream = await _httpClient.GetStreamAsync(fileDownloadInfo.Url);

var streamContent = new StreamContent(stream);

var response = await _httpClient.PutAsync(fileUploadUrl, streamContent);   // throws excepiton

这是完整的异常消息:

"InnerException": {
        "ClassName": "System.IO.IOException",
        "Message": "Unable to read data from the transport connection: An established connection was aborted by the software in your host machine..",
        "Data": null,
        "InnerException": {
            "ClassName": "System.Net.Sockets.SocketException",
            "Message": "An established connection was aborted by the software in your host machine.",
            "Data": null,
            "InnerException": null,
            "HelpURL": null,
            "StackTraceString": null,
            "RemoteStackTraceString": null,
            "RemoteStackIndex": 0,
            "ExceptionMethod": null,
            "HResult": -2147467259,
            "Source": null,
            "WatsonBuckets": null,
            "NativeErrorCode": 10053
        },
        "HelpURL": null,
        "StackTraceString": "   at System.Net.Security.SslStream.<WriteSingleChunk>g__CompleteAsync|210_1[TWriteAdapter](ValueTask writeTask, Byte[] bufferToReturn)\r\n   at System.Net.Security.SslStream.WriteAsyncInternal[TWriteAdapter](TWriteAdapter writeAdapter, ReadOnlyMemory`1 buffer)\r\n   at System.Net.Http.HttpConnection.WriteAsync(ReadOnlyMemory`1 source)\r\n   at System.Net.Http.HttpConnection.ChunkedEncodingWriteStream.<WriteAsync>g__WriteChunkAsync|2_0(HttpConnection connection, ReadOnlyMemory`1 buffer)\r\n   at System.Net.Http.HttpConnection.CopyFromBufferAsync(Stream destination, Int32 count, CancellationToken cancellationToken)\r\n   at System.Net.Http.HttpConnection.CopyToContentLengthAsync(Stream destination, UInt64 length, Int32 bufferSize, CancellationToken cancellationToken)\r\n   at System.Net.Http.HttpConnection.ContentLengthReadStream.CompleteCopyToAsync(Task copyTask, CancellationToken cancellationToken)\r\n   at System.Net.Http.StreamToStreamCopy.<CopyAsync>g__DisposeSourceAsync|0_0(Task copyTask, Stream source)\r\n   at System.Net.Http.HttpContent.CopyToAsyncCore(ValueTask copyTask)",
        "RemoteStackTraceString": null,
        "RemoteStackIndex": 0,
        "ExceptionMethod": null,
        "HResult": -2146232800,
        "Source": "System.Private.CoreLib",
        "WatsonBuckets": null
    },

标签: c#file-uploadhttpclient

解决方案


推荐阅读