Iphone delayed response from server

As soon as I send a request to the server (via the NSURLConnection sendSynchronousRequest method), the server receives it after about 2 seconds, it processes and sends the response within 3-5 seconds. However, I only return a response after 30-35 seconds. This delay makes our communication very slow.

Even asynchronous APIs receive a delayed response.

Previously, everything worked fine, and the client received a response within 10 seconds. Anyone else with this question? What could be the reason?

EDIT here is a screenshot of Wireshark analysis:

Link for a better image

enter image description here

How should I see which packet says what? .. and why is it delayed?

EDIT2 Here is the code:

  NSHTTPURLResponse *response=nil; NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:nsURL] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:180.0]; [theRequest setHTTPMethod:@"POST"]; [theRequest setTimeoutInterval:180.0]; [theRequest setHTTPBody:[[NSString stringWithFormat:@"%@",sdata] dataUsingEncoding:NSASCIIStringEncoding]]; NSError *error= nil; NSData *result = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error]; if (error ) { NSLog(@"error sending synchronous request: %@", error); } NSLog(@"request completed with code:%d",response.statusCode); 
+4
source share
5 answers

Solution that worked for me:

In the request headers, iOS sets gzip to Accept-Encoding by default. Gzip compression took a long time and therefore a delayed response. To solve the problem, I did the following:

 [theRequest setValue:@"" forHTTPHeaderField:@"Accept-Encoding"]; 

NOTE. Check your headers for any delayed response.

Thanks to @occulus for directing me to the request headers!

+4
source

I would not call sendSynchronousRequest . Instead, use the asynchronous version if you are not already making a call in the background thread (you do not want to block the user interface thread).

How do you know when an iOS response is received? NSLog? UI state changed?

See also the following questions:

NSURLConnection sendSynchronousRequest - foreground background

NSURLConnection sendSynchronousRequest takes too long to respond

Update

If you're a bit stuck, one strategy might be to eliminate the use of NSURLConnection as a problem.

  • Strategy 1: try using NSURL asynchronous connection call instead of synchronous
  • Strategy 2: try using a different HTTP library such as AFNetworking

If you want to take a closer look at what happens with an HTTP connection, you can use tools like Charles , Fiddler, or Wireshark to debug what kind of data is being sent and received. In order to get the maximum benefit from this kind of analysis, you need to know some HTTP protocols (protocols). This is probably more time than the previously mentioned strategies.

See also questions such as How to track network calls made from iOS Simulator .

Update

Are you accessing your own web server, or is it someone else's?

Have you carefully examined the headers sent to your web server (and those that were returned)? Pay attention to the length of the content, for example. Incorrect content lengths may cause a delay, as described here .

To see the request and the returned headers, you can use Firebug or something like wget or curl on the command line.

Also, double check for a newline at the end of your URL, as described here .

+8
source

Do you do it via wifi or celluar?

Speaking from my experience, when my data usage is taken away, my ISP slows down my downloads. Sometimes this happens to me at the end of the month after I used a lot of mobile data.

0
source

I'm not sure, but it could be a network problem, first of all, check it out. is it right or down? ok Maybe I'm wrong .. but check this out first ...

After describing the code, it can solve your problem :)

 NSString *url = @"Your URL "; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0]; [request setHTTPMethod:@"POST"]; NSMutableData *body = [NSMutableData data]; . . /// Add Here Your NSMutableData Valuew . . [request setHTTPBody:body]; NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; if (theConnection) { self.responseData = [[NSMutableData alloc] init]; } else NSLog(@"Connection Failed!"); 
0
source

To alleviate your problems with an asynchronous HTTP request, you should consider using the AFNetworking infrastructure.

-1
source

All Articles