Xamarin.iOS NSUrlSession not working on iOS 8

I have been using NSUrlSession for iOS 7.1 for a while now, uploads and downloads are working properly. After upgrading a test device to iOS 8, downloads nor uploads are working anymore.

I modified the creation of the NSUrlSessionConfiguration object.

        if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
            configUpload = NSUrlSessionConfiguration.CreateBackgroundSessionConfiguration(this.urlSessionIdUpload);
            configDownload = NSUrlSessionConfiguration.BackgroundSessionConfiguration(this.urlSessionIdDownload);
        configUpload.TimeoutIntervalForResource = 30.0;
        configUpload.TimeoutIntervalForRequest = 30.0;
        this.uploadSession = NSUrlSession.FromConfiguration(configUpload, new UploadDelegate(this), new NSOperationQueue());

After this modification my iOS 7 app was still working, no problems at all. But on iOS 8 it is showing some weird behaviour. I'm using exactly the same endpoint and haven't modified the part which creates the Up/Download task.

                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);

The fileUrl is pointing to the (local)file which should be uploaded. Then I start the task in some other class.

    public override void Start()

The following code will be called when the upload has completed.

    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));
            this.InvokeOnMainThread(() => this.controller.TaskSucceeded("upload_" + task.TaskIdentifier));
            this.InvokeOnMainThread(() => this.controller.WriteLog("Completed TaskIdentifier: " + task.TaskIdentifier));

Two different scenario's are present when trying to upload a file. -Uploading a very small image: the upload completes without an error in my delegate. It shows me it uploaded succesfully, but when I look in the server response I see a 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";

When I run this request on my iOS 7 device it is working:

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] } }

The second scenario occurs when I try to upload a larger file. My upload stops at a certain point, and it times out afterwards. This will result in a call to the DidCompleteWithError delegate with an error message stating its timed-out.

For the download part; it's giving me an error the endpoint is not found.

In all scenario's; it looks like my server is not even hit at all. It's not creating logs anything happened at all.

To make all things clear; I'm using exactly the same source code to build my app. I'm only changing my deployment target for matching the correct device. The endpoints are not modified either.

Does anyone have any clue at what causes all these problems?

Update 1 The download issue has been resolved. The problem was caused by the loggin mechanism, which couldn't get the end of the download stream.

The uploads remain not functioning.

Update 2: Solution Finally found the issue what caused uploads to fail in iOS 8.

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

Using this header in iOS 7 causes no issues. Using this header in iOS 8 causes a bad request to the server.


Issue was solved by removing this line:

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

Worked on iOS7, but not in iOS8.

Need Your Help

Delete pattern from a variable with sed

linux bash variables sed

I am working with a script that has a variable called PRODUCT_VERSION.

Using Core Bluetooth to get Bluetooth LE peripheral advertisements in background

iphone objective-c ios bluetooth bluetooth-lowenergy

I'm having trouble figuring out how to receive Bluetooth LE peripheral device advertisements while my app is running in the background. That is, when my app is in the background it never reaches the

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.