首页 > 解决方案 > HTTP 请求异常 Xamarin 表单

问题描述

我正在使用HttpClientPostAsync向我的服务器发送一个大型视频文件(平均文件大小为 50mb)。我遇到了一个异常(有关完整的异常详细信息,请参见下文)。我发现一篇文章说可以处理的最大文件大小为 1mb 或更小,这是真的吗?如果是这样,我怎样才能增加最大文件大小?

{System.Net.Http.HttpRequestException:发送请求时发生错误---> System.IO.IOException:写入请求时出错---> System.Net.Sockets.SocketException:套接字已在系统关闭。 Net.WebConnection.EndWrite (System.Net.HttpWebRequest request, System.Boolean throwOnError, System.IAsyncResult result) [0x000cc] in <2016f34fb49d404d9e9f5c5e62749987>:0 at System.Net.WebConnectionStream.WriteAsyncCB (System.IAsyncResult r) [0x00013] in <2016f34fb49d404d9e9f5c5e62749987>:0 --- 内部异常堆栈跟踪结束 --- 在 System.Net.WebConnectionStream.EndWrite (System.IAsyncResult r) [0x000b8] in <2016f34fb49d404d9e9f5c5e62749987>:0 在 System.IO.Stream+<>c。 b__53_1(System.IO.Stream 流,System.IAsyncResult asyncResult)[0x00000] 在:0 at (wrapper delegate-invoke) System.Func3[System.IO.Stream,System.IAsyncResult,System.Threading.Tasks.VoidTaskResult].invoke_TResult_T1_T2(System.IO.Stream,System.IAsyncResult) at System.Threading.Tasks.TaskFactory1+FromAsyncTrimPromise1[TResult,TInstance].Complete (TInstance thisRef, System.Func3[T1,T2,TResult] endMethod, System.IAsyncResult asyncResult, System.Boolean requiresSynchronization) [0x00000] in :0 --- 从先前引发异常的位置结束堆栈跟踪---在 System.Runtime.ExceptionServices。 ExceptionDispatchInfo.Throw () [0x0000c] 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task 任务) [0x0003e] 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System. Threading.Tasks.Task 任务)[0x00028] 在 System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd 的 0 中(System.Threading.Tasks.Task 任务)[0x00008] 在 System.Runtime.CompilerServices.ConfiguredTaskAwaitable+ConfiguredTaskAwaiter 的 0 中。 GetResult () [0x00000] in :0 at System.Net.Http.HttpClientHandler+d__64.MoveNext () [0x0036e] in :0 --- 内部异常堆栈跟踪结束 --- 在 System.Net.Http.HttpClientHandler+d__64.MoveNext () [0x00489] in :0 --- 堆栈跟踪从先前抛出异常的位置结束 ---在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] 在:0 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task 任务) [0x0003e] 在:0 在 System.Runtime.CompilerServices .TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task 任务) [0x00028] 在 System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task 任务) [0x00008] 在 System.Runtime:0 .CompilerServices.ConfiguredTaskAwaitable0 --- 从先前引发异常的位置结束堆栈跟踪 --- 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in :0 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading .Tasks.Task 任务)[0x0003e] 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification 的 0 处(System.Threading.Tasks.Task 任务)[0x00028] 在 System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd 的 0 处(系统.Threading.Tasks.Task 任务)[0x00008] in :0 at System.Runtime.CompilerServices.ConfiguredTaskAwaitable0 --- 从先前引发异常的位置结束堆栈跟踪 --- 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in :0 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading .Tasks.Task 任务)[0x0003e] 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification 的 0 处(System.Threading.Tasks.Task 任务)[0x00028] 在 System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd 的 0 处(系统.Threading.Tasks.Task 任务)[0x00008] in :0 at System.Runtime.CompilerServices.ConfiguredTaskAwaitableThreading.Tasks.Task 任务)[0x0003e] 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification 的 0 中(System.Threading.Tasks.Task 任务)[0x00028] 在 System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd 的 0 中( System.Threading.Tasks.Task 任务)[0x00008] in :0 在 System.Runtime.CompilerServices.ConfiguredTaskAwaitableThreading.Tasks.Task 任务)[0x0003e] 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification 的 0 中(System.Threading.Tasks.Task 任务)[0x00028] 在 System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd 的 0 中( System.Threading.Tasks.Task 任务)[0x00008] in :0 在 System.Runtime.CompilerServices.ConfiguredTaskAwaitable1+ConfiguredTaskAwaiter[TResult].GetResult () [0x00000] in <df077aeeed724748974ac70c763795f3>:0 at System.Net.Http.HttpClient+<SendAsyncWorker>d__49.MoveNext () [0x000ca] in <e4f3fb4cf2054f718c1099b587ec6f1e>:0 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <df077aeeed724748974ac70c763795f3>:0 at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0003e] in <df077aeeed724748974ac70c763795f3>:0 at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in <df077aeeed724748974ac70c763795f3>:0 at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in <df077aeeed724748974ac70c763795f3>:0 at System.Runtime.CompilerServices.TaskAwaiter1[TResult].GetResult () [0x00000] in :0 at TBSMobile.View.MainMenu+d__22.MoveNext () [0x00b96] 在 C:\Users\lawre\source\repos\TBSMobile\TBSMobile\TBSMobile\View\MainMenu .xaml.cs:1088 }

byte[] crVideoData;

if (!string.IsNullOrEmpty(crvideo))
{
    crVideoData = File.ReadAllBytes(crvideo);
}
else
{
    crVideoData = null;
}

try
{
    if (!string.IsNullOrEmpty(crvideo))
    {
        var vidlink = "http://" + ipaddress + Constants.requestUrl + "Host=" + host + "&Database=" + database + "&Contact=" + contact + "&Request=Lqr9fy";
        string vidcontentType = "application/json";
        JObject vidjson = new JObject
        {
           { "CAFNo", crcafNo },
           { "CAFDate", crcafDate },
           { "Video", crVideoData }
        };

        HttpClient vidclient = new HttpClient();
        var vidresponse = await vidclient.PostAsync(vidlink, new StringContent(vidjson.ToString(), Encoding.UTF8, vidcontentType));

        if (vidresponse.IsSuccessStatusCode)
        {
            await conn.QueryAsync<CAFTable>("UPDATE tblCaf SET LastSync = ? WHERE CAFNo = ?", DateTime.Parse(current_datetime), crcafNo);
        }
    }
    else
    {
         await conn.QueryAsync<CAFTable>("UPDATE tblCaf SET LastSync = ? WHERE CAFNo = ?", DateTime.Parse(current_datetime), crcafNo);
    }
}
catch(Exception ex)
{
    Crashes.TrackError(ex);
}

标签: c#xamarinxamarin.forms

解决方案


如果您使用 api 将上传的视频保存在服务器中,则您的最大请求长度可能小于您上传的视频大小。如果要上传大尺寸视频,请在 webconfig 文件中增加最大请求长度。这是在 web api 项目中增加最大请求长度的示例代码。

  <httpRuntime targetFramework="4.5.2" maxRequestLength="15200" />

推荐阅读