NSURLConnection sendSynchronousRequest taking too

2019-09-12 10:41发布

问题:

I am trying to access data from web services created for the project I have been working on. I am creating a string to create a request

 NSMutableString *sRequest = [[NSMutableString alloc] init];
 [sRequest appendString:@"<soapenv:Envelope xmlns:soapenv=\"http:blah blah blah /messages\">"];
 [sRequest appendString:@"<soapenv:Header/>"];
 [sRequest appendString:@"<soapenv:Body>"];
 [sRequest appendString:@"<mes:processActionRequest>"];
 [sRequest appendString:@"<ProcessAction>"];
 [sRequest appendString:@"</mes:processActionRequest>"];
 [sRequest appendString:@"</soapenv:Body>"];
 [sRequest appendString:@"</soapenv:Envelope>"];

Then I am creating a URL and request

 NSURL *ServiceURL = [NSURL URLWithString:[[NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"webservices" ofType:@"plist"]] valueForKey:@"EndPointURL"]];



 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:ServiceURL];

 [request addValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];



 [request setHTTPMethod:@"POST"];
 [request setHTTPBody:[sRequest dataUsingEncoding:NSUTF8StringEncoding]];

 [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

 NSTimeInterval start = [NSDate timeIntervalSinceReferenceDate];

 NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:NULL error:NULL];

 NSTimeInterval duration = [NSDate timeIntervalSinceReferenceDate] - start;
 NSLog(@"Response Time%.4f",duration);

 [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;


 return responseData; 

The problem is, the response time that I printed in Log always comes to be above 20seconds somehow. But when I hit the same link using eviware/SoapUI project, it executes within a second, also when the same link is being used in blackberry project, it works smoothly. I am not getting, where am I going wrong. Why the response is very slow for iPhone only. Please help. If I am not sounding clear, please let me know. I will try to elaborate more.

回答1:

There was a new line character at the end of url which was causing the problem. Thanks for the support Andrew.



回答2:

Unless you are already running the code from a background thread, you'd be better off using the asynchronous calls on NSURLConnection.

Do you see the same problem with the asynchronous API?