2014-09-24 118 views
2

我一直在iOS 7.1中使用NSUrlSession一段时间,上传和下载工作正常。 将测试设备升级到iOS 8后,下载和上传工作已经结束。Xamarin.iOS 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上它显示出一些奇怪的行为。我使用的是完全相同的端点,并没有修改创建上传/下载任务的部分。

   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 Result。

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 下载问题已解决。问题是由登录机制造成的,无法获得下载流的结束。

上传文件保持不起作用。

更新2:解决方案 终于找到了问题,是什么原因导致上传iOS中8

request["Transfer-Encoding"] = "chunked" 

失败使用这个标题中的iOS 7不会造成问题。在iOS 8中使用此标头会导致对服务器的错误请求。

+1

您是否正在使用Xamarin.iOS(8.0.0.62)的最新稳定版本? – 2014-09-24 12:34:25

+0

我使用的是8.0.0.52。没有收到8.0.0.62的任何更新。也面对这个:http://forums.xamarin.com/discussion/24733/xamarin-ios-versions-are-not-in-sync-on-the-latest-pc-version-versus-the-latest-mac- book-pro – 2014-09-24 12:42:33

+0

你在Mac或Windows上有8.0.0.52吗? (重要的是在mac上) – 2014-09-24 13:13:09

回答

2

问题解决了通过删除这一行:

request["Transfer-Encoding"] = "chunked" 

在iOS8上从事过iOS7,但不是。