Connection Header with NSURLRequest

Hi,


While I'm developing an application, I constantly getting Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." which is not expected and happens intermittently.

While I was looking into some solutions on the web, there are few scenarios that could potentially cause this issue.

Eliminating all other possibilities, I've been left with only one possiblity which is "Connection: Keep-Alive" header in the response.


One note is, I do not have an access to the server to change the behvaiour.


Funny thing is that according to the RFC standard, if the client request with "Connection: close" in the request header, the response should return "Connection: close" respectively.


However, I've observed in iOS SDK, even with "Connection: close" header in the request, this connection header in the request keeps being ignored and the server returns a response with "Connection: Keep-Alive" header. I've also double checked with Android SDK, and other REST client by sending the same request to the same server with "Connection: close" header in the request, all other clients are getting response with respective response header "Connection: close".


I understand that this "The network connection was lost." error may not really be related to iOS SDK if the server respects RFC standard; however, as I do not have an access to the server environment, I am trying to resolve this issue on the client side.


Is this a bug on iOS SDK that "Connection" header in the request is not being respected in the response? or am I doing something wrong or missing something?


For a note, I've tested with iOS 9.1 to iOS 11 on Xcode 9.3.1, and all scenario provides the same result.

And I'm using NSURLSession along with NSURLSessionDataTask to make the request.


Please advise.


Thanks,

Answered by DTS Engineer in 313549022

I recommend that you start by reading QA1941 Handling “The network connection was lost” Errors. Occasional

NSURLErrorNetworkConnectionLost
errors are a fact of life, and you need a strategy for dealing with them.

I constantly getting … which … happens intermittently

So is it constant or intermittent? As mentioned in QA1941, occasional errors like this are expected. However, if you’re seeing a lot of such errors then it definitely warrants further investigation.

However, I've observed in iOS SDK, even with "Connection: close" header in the request …

You mean you’re manually setting the

Connection
header in the
NSURLRequest
via an API like
-setValue:forHTTPHeaderField:
? If so, that’s not something we support. Certain headers, including
Connection
, are fundamental to the operation of HTTP and we don’t support you setting them manually. You can find a list of these in the
NSMutableURLRequest
documentation.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"


WWDC runs Mon, 4 Jun through to Fri, 8 Jun. During that time all of DTS will be at the conference, helping folks out face-to-face.
Accepted Answer

I recommend that you start by reading QA1941 Handling “The network connection was lost” Errors. Occasional

NSURLErrorNetworkConnectionLost
errors are a fact of life, and you need a strategy for dealing with them.

I constantly getting … which … happens intermittently

So is it constant or intermittent? As mentioned in QA1941, occasional errors like this are expected. However, if you’re seeing a lot of such errors then it definitely warrants further investigation.

However, I've observed in iOS SDK, even with "Connection: close" header in the request …

You mean you’re manually setting the

Connection
header in the
NSURLRequest
via an API like
-setValue:forHTTPHeaderField:
? If so, that’s not something we support. Certain headers, including
Connection
, are fundamental to the operation of HTTP and we don’t support you setting them manually. You can find a list of these in the
NSMutableURLRequest
documentation.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"


WWDC runs Mon, 4 Jun through to Fri, 8 Jun. During that time all of DTS will be at the conference, helping folks out face-to-face.

Thanks for your reply.

That clarifies my doubt.


As for the connection lost error, it happens constantly around one or two times out of 10 attempts.

I believe that this is not just the client side issue, so I will investigate with whether server side is doing HTTP 1.1 properly as well.


Thanks,

As for the connection lost error, it happens constantly around one or two times out of 10 attempts.

OK, that’s much more often than I’d expect.

I will investigate with whether server side is doing HTTP 1.1 properly as well.

Cool. In situations like this I recommend an RVI packet trace (per QA1176 Getting a Packet Trace), which should allow you to determine whether it’s the client or server that’s not following the rules.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"


WWDC runs Mon, 4 Jun through to Fri, 8 Jun. During that time all of DTS will be at the conference, helping folks out face-to-face.

Hi James - can I ask if you ever resolved this old issue? I'm seeing very similar behaviour in our app and tried (and failed with) this same approach in an attempt to rule out issues with persistent connection configuration.

Connection Header with NSURLRequest
 
 
Q