0

我正在尝试从为我一直在从事的项目创建的 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 的响应速度很慢。请帮忙。如果我听起来不清楚,请告诉我。我将尝试详细说明。

4

2 回答 2

3

导致问题的 url 末尾有一个换行符。感谢安德鲁的支持。

于 2011-01-07T11:52:57.363 回答
1

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

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

于 2010-12-23T11:44:06.377 回答