2

I had been using CFHttp and NSUrlConnection. These classes create a new connection every time to do a http send and receive.

Basically i need a single connection to perform all my send and receive.

Open connection -> send http request -> receive http response ->send http request -> receive http response-> Close connection.

Is there any way to do this.

nswamy
  • 1,041
  • 9
  • 16

1 Answers1

2

NSUrlConnection will automagically keep open and re-use a connection for you, via the HTTP 1.1 protocol. (See this accepted answer). It should do this out of the box, unless you're doing something to modify its default behaviour.

I recommend using a network sniffer to verify that connection re-use is happening (or not), and to verify after what amount of time the connection might be dropped (and hence re-opened on next request). Wireshark is a superb network analyzer with good protocol support.

You could also use a third party library; AFNetworking is nice, well designed, and gets good press. (I used to use ASIHttpRequest but it's recently been retired from active development, and its code structure is more monolithic.)

Community
  • 1
  • 1
occulus
  • 16,959
  • 6
  • 53
  • 76
  • using iOS 9.3 , im not able to use http keep alive it seems. Please take a look http://stackoverflow.com/questions/38773688/ios-doesnt-to-use-http-keep-alive-very-high-latency – harveyslash Aug 05 '16 at 06:02