我已经在iOS 7.1上使用NSUrlSession了一段时间,上传和下载工作正常。将测试设备升级到iOS 8后,下载或上传均正常进行。
我修改了NSUrlSessionConfiguration对象的创建。
if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
{
configUpload = NSUrlSessionConfiguration.CreateBackgroundSessionConfiguration(this.urlSessionIdUpload);
}
else
{
configDownload = NSUrlSessionConfiguration.BackgroundSessionConfiguration(this.urlSessionIdDownload);
}
configUpload.TimeoutIntervalForResource = 30.0;
configUpload.TimeoutIntervalForRequest = 30.0;
this.uploadSession = NSUrlSession.FromConfiguration(configUpload, new UploadDelegate(this), new NSOperationQueue());
进行此修改后,我的iOS 7应用仍可正常使用,完全没有问题。但是在iOS 8上,它表现出一些奇怪的行为。我使用的端点是完全相同的,并且没有修改创建Up / Download任务的部分。
NSUrl uploadHandlerUrl = NSUrl.FromString("MY URL IS HERE");
NSMutableUrlRequest request = new NSMutableUrlRequest(uploadHandlerUrl);
request.HttpMethod = "POST";
request["Content-Type"] = "application/octet-stream";
request["Transfer-Encoding"] = "chunked";
NSUrlSessionUploadTask uploadTask = uploadSession.CreateUploadTask(request, fileUrl);
fileUrl指向应上传的(本地)文件。然后,我在其他班级开始了这项任务。
public override void Start()
{
this.task.Resume();
}
上传完成后,将调用以下代码。
public async override void DidCompleteWithError(NSUrlSession session, NSUrlSessionTask task, NSError error)
{
if (error != null)
{
this.InvokeOnMainThread(() => this.controller.WriteLog("Completed TaskIdentifier: " + task.TaskIdentifier + " Error: " + error.LocalizedDescription));
this.InvokeOnMainThread(() => this.controller.TaskFailed("upload_" + task.TaskIdentifier));
this.InvokeOnMainThread(() => this.controller.UploadFailed(task));
}
else
{
this.InvokeOnMainThread(() => this.controller.TaskSucceeded("upload_" + task.TaskIdentifier));
this.InvokeOnMainThread(() => this.controller.WriteLog("Completed TaskIdentifier: " + task.TaskIdentifier));
}
}
尝试上传文件时,存在两种不同的情况。-上传非常小的图片:上传完成,我的委托中没有错误。它显示了它已成功上传,但是当我查看服务器响应时,看到了400 Http结果。
2014-09-24 11:33:20.441 BackgroundTransferService[346:17701] 2014-09-24:11.33.19 : Server Request: https://xxxxxxxxxx/json/UploadFile?name=file_dbafdcf2-c36e-47d7-9262-bcde8bfb316d.jpg&i=0
2014-09-24 11:33:20.441 BackgroundTransferService[346:17701] 2014-09-24:11.33.19 : Request Headers: [Transfer-Encoding, chunked]
2014-09-24 11:33:20.442 BackgroundTransferService[346:17701] 2014-09-24:11.33.19 : Request Headers: [Content-Type, application/octet-stream]
2014-09-24 11:33:20.442 BackgroundTransferService[346:17701] 2014-09-24:11.33.19 : Request Method: POST
2014-09-24 11:33:20.443 BackgroundTransferService[346:17701] 2014-09-24:11.33.19 : Server Response: <NSHTTPURLResponse: 0x16e364d0> { URL: https://xxxxxxxxxxxxxxx/json/UploadFile?name=file_dbafdcf2-c36e-47d7-9262-bcde8bfb316d.jpg&i=0 } { status code: 400, headers {
2014-09-24 11:33:20.444 BackgroundTransferService[346:17701] Connection = close;
2014-09-24 11:33:20.444 BackgroundTransferService[346:17701] "Content-Length" = 374;
2014-09-24 11:33:20.444 BackgroundTransferService[346:17701] "Content-Type" = "text/html; charset=us-ascii";
2014-09-24 11:33:20.445 BackgroundTransferService[346:17701] Date = "Wed, 24 Sep 2014 09:28:10 GMT";
2014-09-24 11:33:20.445 BackgroundTransferService[346:17701] Server = "Microsoft-HTTPAPI/2.0";
当我在iOS 7设备上运行此请求时,它正在运行:
2014-09-24 11:32:15.815 BackgroundTransferService[4219:60b] 2014-09-24:11.32.10 : Server Request: https://xxxxxxxxxxxx/json/UploadFile?name=file_c92bd2df-267a-4533-9e4d-1644685b6b91.jpg&i=0
2014-09-24 11:32:15.817 BackgroundTransferService[4219:60b] 2014-09-24:11.32.10 : Server Headers: [Transfer-Encoding, chunked]
2014-09-24 11:32:15.818 BackgroundTransferService[4219:60b] 2014-09-24:11.32.10 : Server Headers: [Content-Type, application/octet-stream]
2014-09-24 11:32:15.819 BackgroundTransferService[4219:60b] 2014-09-24:11.32.10 : Request Method: POST
2014-09-24 11:32:15.820 BackgroundTransferService[4219:60b] 2014-09-24:11.32.10 : Server Response: <NSHTTPURLResponse: 0x176f7040> { URL: https://xxxxxxxxxxxx/json/UploadFile?name=file_c92bd2df-267a-4533-9e4d-1644685b6b91.jpg&i=0 } { status code: 200, headers {
2014-09-24 11:32:15.821 BackgroundTransferService[4219:60b] "Cache-Control" = private;
2014-09-24 11:32:15.823 BackgroundTransferService[4219:60b] "Content-Encoding" = gzip;
2014-09-24 11:32:15.824 BackgroundTransferService[4219:60b] "Content-Type" = "application/json; charset=utf-8";
2014-09-24 11:32:15.825 BackgroundTransferService[4219:60b] Date = "Wed, 24 Sep 2014 09:30:44 GMT";
2014-09-24 11:32:15.826 BackgroundTransferService[4219:60b] Server = "Microsoft-IIS/8.0";
2014-09-24 11:32:15.827 BackgroundTransferService[4219:60b] "Transfer-Encoding" = Identity;
2014-09-24 11:32:15.828 BackgroundTransferService[4219:60b] Vary = "Accept-Encoding";
2014-09-24 11:32:15.829 BackgroundTransferService[4219:60b] "X-AspNet-Version" = "4.0.30319";
2014-09-24 11:32:15.830 BackgroundTransferService[4219:60b] "X-Powered-By" = "ASP.NET";
2014-09-24 11:32:15.831 BackgroundTransferService[4219:60b] } }
2014-09-24 11:33:20.446 BackgroundTransferService[346:17701] } }
当我尝试上传较大的文件时,会发生第二种情况。我的上传在某个时间点停止,然后超时。这将导致对DidCompleteWithError委托的调用,并显示一条错误消息,指出其超时。
对于下载部分;这给我一个错误,找不到端点。
在所有情况下 看来我的服务器根本没有命中。它根本没有创建日志,任何事情都发生了。
弄清楚一切;我正在使用完全相同的源代码来构建我的应用程序。我只是更改我的部署目标以匹配正确的设备。端点也未修改。
是否有人对导致所有这些问题的原因有任何线索?
更新1下载问题已解决。该问题是由loggin机制引起的,该机制无法结束下载流。
上载仍然无法正常运行。
更新2:解决方案最终找到了导致iOS 8中的上传失败的原因。
request["Transfer-Encoding"] = "chunked"
在iOS 7中使用此标头不会引起任何问题。在iOS 8中使用此标头会导致对服务器的错误请求。
通过删除此行解决了问题:
request["Transfer-Encoding"] = "chunked"
适用于iOS7,但不适用于iOS8。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句