Error -1005 "The network connection was lost."

Hello,



I have a problem using HTTP PUT with iOS 10 beta.

I use the code to make a PUT request. While it works fine with iOS 9.3.4 it doesn’t work in iOS 10 beta 6



   NSString *str = @"<myData>Some xml Data</myData>";
    NSData *xmlData = [str dataUsingEncoding:NSUTF8StringEncoding];


    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData ];
    [request setTimeoutInterval:30];
    NSString *aUrl = @"https:/myServer
    [request setURL:[NSURL URLWithString: aUrl ]];
    [request setHTTPMethod:@"PUT"];
    [request setValue:@"text/xml" forHTTPHeaderField:@"Content-type"];


    NSMutableData *postBody = [NSMutableData data];
    [postBody appendData:xmlData];
    [request setHTTPBody:postBody];
    NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: self delegateQueue: nil];
    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
     NSURLSessionDataTask *dataTask = [defaultSession dataTaskWithRequest:request completionHandler:^(NSData *taskData, NSURLResponse *taskResponse, NSError *taskError) {
     dispatch_semaphore_signal(semaphore);
     }];
    [dataTask resume];
    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
    if (dataTask.error == nil)
    {
        NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)dataTask.response;
        int code = (int)[httpResponse statusCode];
        if(code == 200)
        {
            NSLog(@"%@", @"OK");
        }
        else
        {
            NSLog(@"%d", code);
        }
    }
    else
    {
        NSLog(@"%@", dataTask.error);
    }


The data is stored correctly to the Server but the DataTask returns the error 1005 "The network connection was lost."

I tried to analyse the Response (by enabling CFNETWORK_DIAGNOSTICS) from and I have seen, that the Response retunes with code "200 The request completed successfully."

You can find the protocol (beginning from the Response part)



Thanks



Paul



Protocol Received: request PUT https://myServer HTTP/1.1
         Response: HTTP/1.1 200 The request completed successfully.
         Received: dict [8] {
                                  Server: Microsoft-IIS/8.5
                            Content-Type: text/html; charset=utf-8
                            X-Powered-By: ASP.NET
                              Connection: close
                       Transfer-Encoding: Identity
                                    Date: Tue, 16 Aug 2016 06:21:45 GMT
                           Cache-Control: private
                        X-AspNet-Version: 2.0.50727
                   }
} [3:37]
2016-08-16 08:21:44.823908 myAppName[241:6711] CFNetwork Diagnostics [3:39] 08:21:44.823 {
touchConnection: (null)
          Loader: <CFMutableURLRequest 0x1701a6e40 [0x1a7b17bb8]> {url = https://myServer, cs = 0x0}
Timeout Interval: 30.000 seconds
} [3:39]
2016-08-16 08:21:44.823933 myAppName[241:6760] CFNetwork Diagnostics [3:38] 08:21:44.822 {
Response Error: (null)
       Request: <CFURLRequest 0x1741a5b00 [0x1a7b17bb8]> {url = https://myServer, cs = 0x0}
         Error: Error Domain=kCFErrorDomainCFNetwork Code=-1005 "(null)" UserInfo={NSErrorPeerAddressKey=<CFData 0x17409d830 [0x1a7b17bb8]>{length = 16, capacity = 16, bytes = 0x100220fbc0a802060000000000000000}, _kCFStreamErrorCodeKey=57, _kCFStreamErrorDomainKey=1}
} [3:38]
2016-08-16 08:21:44.824758 myAppName[241:6711] CFNetwork Diagnostics [3:40] 08:21:44.824 {
      Load Data: (null)
         Loader: <CFMutableURLRequest 0x1701a6e40 [0x1a7b17bb8]> {url = https://myServer, cs = 0x0}
         Length: 21
Original Length: 21
} [3:40]
2016-08-16 08:21:44.825743 myAppName[241:6711] CFNetwork Diagnostics [3:41] 08:21:44.825 {
touchConnection: (null)
          Loader: <CFMutableURLRequest 0x1701a6e40 [0x1a7b17bb8]> {url = https://myServer, cs = 0x0}
Timeout Interval: 30.000 seconds
} [3:41]
2016-08-16 08:21:44.827639 myAppName[241:6711] CFNetwork Diagnostics [3:42] 08:21:44.826 {
           Did Fail: (null)
             Loader: <CFMutableURLRequest 0x1701a6e40 [0x1a7b17bb8]> {url = https://myServer, cs = 0x0}
              Error: Error Domain=kCFErrorDomainCFNetwork Code=-1005 "(null)" UserInfo={NSErrorPeerAddressKey=<CFData 0x17409d830 [0x1a7b17bb8]>{length = 16, capacity = 16, bytes = 0x100220fbc0a802060000000000000000}, _kCFStreamErrorCodeKey=57, _kCFStreamErrorDomainKey=1}
init to origin load: 0.00566101s
         total time: 0.348197s
        total bytes: 21
} [3:42]
2016-08-16 08:21:44.837258 myAppName[241:6631] Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." UserInfo={NSUnderlyingError=0x170249e70 {Error Domain=kCFErrorDomainCFNetwork Code=-1005 "(null)" UserInfo={NSErrorPeerAddressKey=<CFData 0x17409d830 [0x1a7b17bb8]>{length = 16, capacity = 16, bytes = 0x100220fbc0a802060000000000000000}, _kCFStreamErrorCodeKey=57, _kCFStreamErrorDomainKey=1}}, NSErrorFailingURLStringKey=https://myServer, NSErrorFailingURLKey=https://myServer, _kCFStreamErrorDomainKey=1, _kCFStreamErrorCodeKey=57, NSLocalizedDescription=The network connection was lost.}
2016-08-16 08:21:44.837408 myAppName[241:6631] -1005
2016-08-16 08:21:44.838055 myAppName[241:6760] CFNetwork Diagnostics [3:43] 08:21:44.836 {
destroyReadStream: request PUT https://myServer HTTP/1.1
          Request: <CFURLRequest 0x1741a5b00 [0x1a7b17bb8]> {url = https://myServer, cs = 0x0}
             sent: <CFNumber 0xb0000000000013e3 [0x1a7b17bb8]>{value = +318, type = kCFNumberSInt64Type}
         received: <CFNumber 0xb000000000001323 [0x1a7b17bb8]>{value = +306, type = kCFNumberSInt64Type}
        cell sent: <CFNumber 0xb000000000000003 [0x1a7b17bb8]>{value = +0, type = kCFNumberSInt64Type}
    cell received: <CFNumber 0xb000000000000003 [0x1a7b17bb8]>{value = +0, type = kCFNumberSInt64Type}
} [3:43]
2016-08-16 08:21:44.839311 myAppName[241:6760] CFNetwork Diagnostics [3:44] 08:21:44.838 {
~HTTPProtocol: nullptr request
      Request: null
         sent: 318
     received: 306
    cell sent: 0
cell received: 0
} [3:44]
2016-08-16 08:21:46.489237 myAppName[241:6760] CFNetwork Diagnostics [3:45] 08:21:46.488 {
DiskCookieStorage Sync Request: (null)
                        Forced: no
                       isDirty: no
                     isWriting: no
                          File: <CFURL 0x1702a52e0 [0x1a7b17bb8]>{string = file:///private/var/mobile/Containers/Data/Application/A67E2D5F-6E27-4169-B6B8-4CE67B747BF3/Library/Cookies/Cookies.binarycookies, encoding = 134217984, base = (null)}
                       Journal: yes
                     Mutations: 0
} [3:45]

Replies

Are you seeing this with iOS 10 GM?


I just noticed that one of my apps is returning lots of -1005 The network connection was lost errors from NSURLConnection. It isn't 100%, and seems to be worse when debugging. I didn't notice this in the betas, but I see it in the GM sporadically.


Most of my calls are using NSURLSession now, and they seem to be OK.

Hi sparky,

it happens also with iOS 10 GM and XCode 8 GM.

I discoverd that it happens only if the Server respons code is 200

If the Server returns 201, 202, 203, 204 or error codes like 404 it works fine.

It doesn't make mutch sens for me.