iphone - Problems with caching using NSURLConnection and querying HTTP Headers -
i trying write crude app loop round querying of http header fields come server in order find out servers responding ok. servers respond header this: server = server1 (or server2, server 3 etc).
unfortunately when same server name time , time again app unless stop , run again @ point different server back. how can stop happening? need app treat each connection if brand new, seems cache , appear come same place, load balancer returns same server name.
i can replicate similar behaviour(expected) in safari going url repeatedly without shutting down safari - gets same server each time, if shut down safari go new server.
the code using follows:
for (int = 0; < 999; i++) { request = [nsurlrequest requestwithurl:url cachepolicy:nsurlrequestreloadignoringcachedata timeoutinterval:10]; [nsurlconnection sendsynchronousrequest: request returningresponse: &response error: nil]; if ([response respondstoselector:@selector(allheaderfields)]) { dictionary = [response allheaderfields]; fullheaderstring = [dictionary description]; } }
how can make connection treated new 1 every time?
cheers, tom
you can make sure content freshly loaded each time in @ least 2 ways:
- add "pragma: no-cache" header http response. should make http client not cache response.
- add random query parameter request like: url = "http://myhost.com/controller/blabla?rand=" + math.random(...). make url unique each time server not care parameter still safe.
hope helps
Comments
Post a Comment