处理“将内容复制到流时出错”异常

时间:2018-07-30 06:58:58

标签: c#

我的C#应用​​程序将文件上传到某个API,我正在使用多部分请求,即,我正在上传文件的json字符串和二进制连接,它适用于大多数文件,但很少有异常,我意味着我们尝试使用名为myFile128MB.dat的文件 我遇到了例外情况:

  

:::将内容复制到流时出错。 :::无法从传输连接中读取数据:不允许发送或接收数据的请求,因为套接字已被先前的关闭调用按该方向关闭。 ::: System.Net.Http.HttpRequestException:将内容复制到流时出错。 ---> System.IO.IOException:无法从传输连接中读取数据:不允许发送或接收数据的请求,因为套接字已被先前的关闭调用朝该方向关闭。 ---> System.Net.Sockets.SocketException:不允许发送或接收数据的请求,因为套接字已被先前的关闭调用按那个方向关闭      在System.Net.Sockets.Socket.BeginReceive(Byte []缓冲区,Int32偏移量,Int32大小,SocketFlags socketFlags,AsyncCallback回调,对象状态)      在System.Net.Sockets.NetworkStream.BeginRead(Byte []缓冲区,Int32偏移量,Int32大小,AsyncCallback回调,对象状态)      ---内部异常堆栈跟踪的结尾---      在System.Net.Sockets.NetworkStream.BeginRead(Byte []缓冲区,Int32偏移量,Int32大小,AsyncCallback回调,对象状态)      在System.Net.FixedSizeReader.StartReading()      在System.Net.Security._SslStream.StartFrameHeader处(字节[]缓冲区,Int32偏移量,Int32计数,AsyncProtocolRequest asyncRequest)      在System.Net.Security._SslStream.StartReading(Byte []缓冲区,Int32偏移量,Int32计数,AsyncProtocolRequest asyncRequest)      在System.Net.Security._SslStream.ProcessRead(Byte []缓冲区,Int32偏移量,Int32计数,AsyncProtocolRequest asyncRequest)      在System.Net.Security._SslStream.BeginRead(Byte []缓冲区,Int32偏移量,Int32计数,AsyncCallback asyncCallback,对象asyncState)      在System.Net.TlsStream.BeginRead(Byte []缓冲区,Int32偏移量,Int32大小,AsyncCallback asyncCallback,对象asyncState)      在System.Net.ConnectStream.BeginReadWithoutValidation(Byte []缓冲区,Int32偏移量,Int32大小,AsyncCallback回调,对象状态)      在System.Net.ConnectStream.BeginRead(Byte []缓冲区,Int32偏移量,Int32大小,AsyncCallback回调,对象状态)      在System.Net.Http.HttpClientHandler.WebExceptionWrapperStream.BeginRead(字节[]缓冲区,Int32偏移量,Int32计数,AsyncCallback回调,对象状态)      在System.Net.Http.StreamToStreamCopy.StartRead()      ---内部异常堆栈跟踪的结尾---      在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()      在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)      在System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()      在D:\ New文件夹\ myApp.cs中的Lib.CheckPoint.ThreatPrevention.d__19.MoveNext()

我的代码大致如下:

public async Task<Dictionary<string , string>> Upload(string filePath)
{   
    var fi             = new FileInfo(FilePath);
    var jsonString     = "some json string";
    var fileContents   = File.ReadAllBytes(fi.FullName);
    var webService     = new Uri(url);
    var requestMessage = new HttpRequestMessage(HttpMethod.Post , webService);

    requestMessage.Method = HttpMethod.Post;
    requestMessage.Headers.Add("Authorization" , "MyKey1234");

    const string boundry = "------------------My-Boundary";
    var multiPartContent = new MultipartFormDataContent(boundry);
    var byteArrayContent = new ByteArrayContent(fileContents);
    multiPartContent.Add(byteArrayContent);

    var.Content = multiPartContent;
    var httpClient = new HttpClient();

    HttpResponseMessage httpResponse = await httpClient.SendAsync(requestMessage , HttpCompletionOption.ResponseContentRead , CancellationToken.None);
    //exception in this line ^
    return new Dictionary<string , string>();
}

呼叫者:

myDictionary = await Upload(filePath);

控制台应用程序的结构如下:

public class Program
{
    public static void Main(string[] args) => MainAsync().Wait();
    private static async Task MainAsync()  => new MyClass().Start();
}

MyClass:

public async void Start() => myDictionary = await Upload(filePath);

0 个答案:

没有答案