我已经在 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 上它表现出了一些奇怪的行为。我使用完全相同的端点,并且没有修改创建上传/下载任务的部分。
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 指向应上传的(本地)文件。 然后我在其他类(class)开始该任务。
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));
}
}
尝试上传文件时会出现两种不同的情况。 -上传非常小的图像:上传完成,我的委托(delegate)中没有错误。它显示它已成功上传,但是当我查看服务器响应时,我看到 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 委托(delegate)并显示一条错误消息,说明其超时。
对于下载部分;它给我一个错误:找不到端点。
在所有场景中;看起来我的服务器根本没有受到攻击。它不会创建任何发生的日志。
把所有事情说清楚;我使用完全相同的源代码来构建我的应用程序。我只是更改部署目标以匹配正确的设备。端点也没有被修改。
有人知道导致所有这些问题的原因吗?
更新 1 下载问题已解决。该问题是由登录机制引起的,无法获取下载流的结尾。
上传仍然无法正常工作。
更新 2:解决方案 终于找到了导致iOS 8上传失败的问题。
request["Transfer-Encoding"] = "chunked"
在 iOS 7 中使用此 header 不会出现任何问题。在 iOS 8 中使用此 header 会导致向服务器发出错误请求。
最佳答案
通过删除此行解决了问题:
request["Transfer-Encoding"] = "chunked"
适用于 iOS7,但不适用于 iOS8。
关于c# - Xamarin.iOS NSUrlSession 不适用于 iOS 8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26016674/