NSURLConnection sendSynchronousRequest需要太多时间来响应

时间:2010-12-23 07:22:09

标签: iphone performance nsurlconnection synchronous

我正在尝试从为我正在处理的项目创建的Web服务访问数据。 我正在创建一个字符串来创建请求

 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>"];

然后我正在创建一个URL并请求

 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; 

问题是,我在Log中打印的响应时间总是以某种方式超过20秒。但是当我使用eviware / SoapUI项目点击相同的链接时,它会在一秒钟内执行,当黑莓项目中使用相同的链接时,它也能顺利运行。我没有得到,我哪里错了。为什么iPhone的响应速度非常慢。请帮忙。 如果我听起来不清楚,请告诉我。我会尝试详细说明。

2 个答案:

答案 0 :(得分:3)

在url的末尾有一个新的行字符导致了问题。感谢Andrew的支持。

答案 1 :(得分:1)

除非您已经在后台线程中运行代码,否则最好在NSURLConnection上使用异步调用。

您是否看到异步API存在同样的问题?