任何帮助或见识将不胜感激。
我设法获得的问题是该问题与HTTPS有关,因为我在发布到HTTP端点时能够收到200的回扣,类似于以下线程here
当前很难解决以下问题:
我将AndroidClientHandler HttpClient实现以及NativeTLS 1.2+用于SSL / TLS实现
当我在自己的应用上录制了超过1分钟或超过20MB的视频时,出现以下错误: “无法访问已处置的对象。\ n对象名称: 'System.Net.Sockets.NetworkStream'。”
这是方法
public async Task<HttpResponseMessage> UploadVideo(string endPoint)
{
HttpResponseMessage responseMessage = null;
try
{
//Stopwatch stopWatch = new Stopwatch();
IList<KeyValuePair<string, string>> formFields = new List<KeyValuePair<string, string>>();
messageHandlerService = Mvx.Resolve<IMessageHandlerService>();
//Declare a scope whereby you can use resources and once you leave the scope the resources are disposed of.
using (StreamReader reader = pathService.GetStreamReaderByPlatform())
{
client = await MyHttpClient.GetClientAsync();
client.Timeout = TimeSpan.FromSeconds(500);
var bytes = default(byte[]);
using (var memstream = new MemoryStream())
{
reader.BaseStream.CopyTo(memstream);
bytes = memstream.ToArray();
using (var content = new MultipartFormDataContent())
{
var fileContent = new ByteArrayContent(bytes);
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = ".mp4",
};
content.Add(fileContent);
content.Add(EncodeFields(formFields));
await Policy.Handle<TimeoutException>()
.Or<HttpRequestException>()
.Or<IOException>()
.OrResult<HttpResponseMessage>(r => httpStatusCodesToRetry.Contains(r.StatusCode))
.WaitAndRetryAsync
(retryCount: 3,
sleepDurationProvider: retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)),
onRetry: (ex, time) =>
{
Debug.WriteLine($"Something went wrong: {ex.Exception.Message}, retrying...");
}).ExecuteAsync(async () =>
{
Debug.WriteLine($"Trying to fetch remote data...");
responseMessage = await client.PostAsync(endPoint, content);
return responseMessage;
});
}
}
}
}
catch (WebException webx)
{
if (responseMessage == null)
{
responseMessage = new HttpResponseMessage();
}
responseMessage.StatusCode = HttpStatusCode.InternalServerError;
responseMessage.ReasonPhrase = string.Format("RestHttpClient.SendRequest failed: {0}", webx);
}
catch (Exception ex)
{
if (responseMessage == null)
{
responseMessage = new HttpResponseMessage();
}
responseMessage.StatusCode = HttpStatusCode.InternalServerError;
responseMessage.ReasonPhrase = string.Format("RestHttpClient.SendRequest failed: {0}", ex);
}
return responseMessage;
}
下面是堆栈跟踪:
System.Net.WebConnectionStream.EndWrite(System.IAsyncResult r)[0x000b8]在<5a97d41d36694fb19855c17429527b10>:0 \ n在System.IO.Stream + <> c.b__53_1(System.IO.Stream流,系统。 IAsyncResult asyncResult)[0x00000]在(包装委托调用)System.Func3[System.IO.Stream,System.IAsyncResult,System.Threading.Tasks.VoidTaskResult].invoke_TResult_T1_T2(System.IO.Stream,System.IAsyncResult)\n at System.Threading.Tasks.TaskFactory
1 + FromAsyncTrimPromise 1[TResult,TInstance].Complete (TInstance thisRef, System.Func
3 [T1,T2,TResult]上为0 \ n,endMethod,System.IAsyncResult asyncResult,System .Boolean RequiresSynchronization)在:0中的[0x00000] \ n ---从上一个引发异常的位置开始的堆栈结束跟踪--- \ n在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()在[0x0000c] n在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task任务)[System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task任务)[0x0003e]中,位于:0 \ n在System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd:0 \ n中为[0x00028](System.Threading.Tasks.Task任务)在System.Runtime.CompilerS:0 \ n中为[0x00008] ervices.ConfiguredTaskAwaitable + ConfiguredTaskAwaiter.GetResult()在System.Net.Http.MultipartContent + d__8.MoveNext()在<996a681f30a44cd685a4da54e11956e2>:0中的[0x00000]在\ n上的位置:0 \ n--从堆栈跟踪结束在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()[0x0000c]在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task任务:0 \ n中的[0x0000c] )[0x0003e]在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task任务)的[0x00028] [0x00028]在System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(0。 Threading.Tasks.Task任务)[0x00008]在System.Runtime.CompilerServices.ConfiguredTaskAwaitable + ConfiguredTaskAwaiter.GetResult()在:0 \ n在System.Net.Http.HttpClientHandler + d__64.MoveNext(0x00008] )<996a681f30a44cd685a4da54e11956e2>中的[0x0036e]:0 \ n ---上一个位置的堆栈跟踪结束关于引发异常的位置--- \ n在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task任务)的\ 0处,位于\\ n的System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()[0x0000c] [0x0003e]在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task任务)处为\ 0的[0x00028]在System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(System.Threading .Tasks.Task任务)[0x00008]在System.Runtime.CompilerServices.ConfiguredTaskAwaitable 1+ConfiguredTaskAwaiter[TResult].GetResult () [0x00000] in <f32579baafc1404fa37ba3ec1abdc0bd>:0 \n at System.Net.Http.HttpClient+<SendAsyncWorker>d__49.MoveNext () [0x000ca] in <996a681f30a44cd685a4da54e11956e2>:0 \n--- End of stack trace from previous location where exception was thrown ---\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <f32579baafc1404fa37ba3ec1abdc0bd>:0 \n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0003e] in <f32579baafc1404fa37ba3ec1abdc0bd>:0 \n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in <f32579baafc1404fa37ba3ec1abdc0bd>:0 \n at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in <f32579baafc1404fa37ba3ec1abdc0bd>:0 \n at System.Runtime.CompilerServices.TaskAwaiter
1处的\ n处为\ 0
…
答案 0 :(得分:1)
我通过在Xamarin.Android的“项目构建设置”中设置一些标志来解决了该问题。
与Docs的建议相反,设置以下配置可让我使用HTTPS将文件发布到服务器。在使用本机TLS 1.2+时,大于20MB的上传会崩溃...是的,我们的服务器支持TLS 1.2