0

I am using NSURLConnection to communicate and pass/get datas to server. I am sending the request only once but server's access log shows that it is receiving multiple requests.I tried to log the request which i am sending to server but it is printing only once. The code to send request to server is :

 NSURL *url=[NSURL URLWithString:[[NSString stringWithFormat:@"%@/services/OtherProcess/Status",serverIP]stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
            NSLog(@"Sending URL -> %@",url);
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];

    if(connection)
        [connection cancel];
    connection=[NSURLConnection connectionWithRequest:theRequest delegate:self];

    responseData = [[NSMutableData alloc] init];

I even tried to add breakpoint and found it is called only once. Any help will be useful.

vishnuvarthan
  • 492
  • 1
  • 6
  • 23

3 Answers3

0

I had the same problem. I called a webpage with an iFrame. This iFrame also called the NSURLConnetion delegates.

a_JAguar
  • 68
  • 7
0

Why don't you use the delegate to log the connection NSURLConnectionDataDelegate? The connection can stay active for a period of time redirecting and such which can cause your server to log multiple requests. You can use this method connection:willSendRequest:redirectResponse: to see if a redirect took place.

Redirects can happen if auth is needed. Also, if not sure, you can always test (only for testing since they will block your main thread) with a synchronous request using this method sendSynchronousRequest:returningResponse:error:

Callistino
  • 1,075
  • 11
  • 14
  • I am using the NSURLConnections delegates, but it also logs once in finishLoading.There is also no redirection happening, anyway ill check it once again. Is there a possibility that multiple open tcp connections to the same address might result in such a problem ? All the multiple logs are printed instantly in access logs. – vishnuvarthan Feb 19 '15 at 13:17
  • every time you init a connection it will do a tcp request to your endpoint. – Callistino Feb 19 '15 at 13:18
  • Will the connection close automatically on a success ?? – vishnuvarthan Feb 19 '15 at 13:20
  • Last time I checked, the connection leaves a socket open for a small amount of time (I think 12s?) so it can reuse the same connection. Now, every time you create a new connection, only that one should send the request. Also, `[connection cancel]` doesn't have much effect since the delegates remain active. – Callistino Feb 19 '15 at 13:30
  • What exactly should i do to prevent it. Am i creating multiple connections or is it some kind of network problem. – vishnuvarthan Feb 19 '15 at 13:37
  • I will try commenting out/deleting the `cancel` line and continue to log on the delegate functions. Canceling a connection only stops the calls to the delegates, the connection can still remain open. That way you may be able to tell if there is a redirect or retry. Check this [question](http://stackoverflow.com/questions/13944527/nsurlconnection-retry-on-401-status) and if all else fails use [AFNetworking](https://github.com/AFNetworking/AFNetworking) – Callistino Feb 19 '15 at 13:48
0

For me it was the socket timeout in our server. Our server was closing the socket and iOS internally was retying 3 more times before it gives up. Increasing the timeout/changing the setting worked for me.

dinmab
  • 53
  • 1
  • 5