【问题标题】:NSURLConnection sendSynchronousRequest taking too much time to respondNSURLConnection sendSynchronousRequest 响应时间过长
【发布时间】:2010-12-23 07:22:09
【问题描述】:

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

【问题讨论】:

    标签: iphone performance nsurlconnection synchronous


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      您是否发现异步 API 存在同样的问题?

      【讨论】:

      • 我确实尝试过使用异步 API。它显示相同的延迟响应。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-24
      • 2016-07-20
      • 2018-11-19
      • 1970-01-01
      相关资源
      最近更新 更多