NSURLConnection ignores Keep-Alive timeout?

I am converting my application to use HTTPS and would like to avoid the cost of a handshake as much as possible while maintaining an open, long-term connection.

From reading web pages and other stack overflow responses, the NSURLConnection message appears should transparently keep the underlying socket open if the server responds with Connection: keep-alive and Keep-Alive: timeout = N.

However, I see that my connections remain open for only about 10 seconds. My Keep-Alive reaction is bigger than this. I also post Connection: keep-alive in my request headers.

Can anyone shed some light on this? I really don't want to use CFNetwork to achieve this.

+6
source share
2 answers

You have two ways:

As Tyler says in his answer here :

You can specify a timeout in your NSURLRequest object. One way to do this is to build it through requestWithURL:cachePolicy:timeoutInterval: (You can pass NSURLRequestUseProtocolCachePolicy cachePolicy by default if you don't want to worry about this part.) Timeout is a floating point value in seconds, since basically all the time intervals are in the iPhone SDK.

Also, make sure your NSURLConnection delegate NSURLConnection installed and responds to the connection:didFailWithError: method. A connection always calls this method or connectionDidFinishLoading: on terminating the connection.

Or do what Chris suggests in his answer here :

ASIHTTPRequest has an expirePersistentConnections method. It can do what you are looking for.

This is not a replacement for NSURLConnection, but it is not too difficult to port the code from NSURLConnection to ASIHTTPRequest.

+2
source

10s issue has been fixed in iOS7. I had the same problem, and I followed the error. IOS 6 fixed some version.

0
source

Source: https://habr.com/ru/post/909077/


All Articles