对于简单的 POST 请求,NSURLSession 比 NSURLConnection 花费的时间更长 [英] NSURLSession is taking longer time than NSURLConnection for simple POST request

查看:55
本文介绍了对于简单的 POST 请求,NSURLSession 比 NSURLConnection 花费的时间更长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的 post 服务,它接受一个 json 作为输入并给出一个 json 作为输出.当我使用 NSURLConnection 时,我在 500 毫秒内得到响应.但是如果我使用 NSURLSession 至少需要 4-5 秒来响应相同的请求.

I have a simple post service which takes a json as input and gives a json as output. When I am using NSURLConnection, I get the response within 500ms. But if I use NSURLSession it takes atleast 4-5s to respond for the same request.

还有 NSURLSession didCompleteWithError 总是触发,尽管没有错误并且错误为零.但是在 NSURLConnection 的情况下 didFailWithError 会触发.

Also with NSURLSession didCompleteWithError always firing though there is no error and error is nil. But in case of NSURLConnection didFailWithError is tot firing.

请告诉我我在使用 NSURLSession 时做错了什么.

Please tell me what I am doing wrong with NSURLSession.

使用 NSURLConnection 的 POST

-(void) postData: (NSString *)requestBody toAddress: (NSString *)url{
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];

   [request setHTTPMethod:@"POST"];
   [request setValue:[NSString stringWithFormat:@"%lu",(unsigned long)requestBody.length] forHTTPHeaderField:@"Content-Length"];
   [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
   [request setHTTPBody:[requestBody dataUsingEncoding:NSUTF8StringEncoding]];
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
 }

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)aResponse
{
  data = [NSMutableData data];
  [data setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)bytes
{
    [data appendData:bytes];
}

 - (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
   NSLog(@"Response http status code %ld", (long)[httpResponse statusCode]);
   NSLog(@"response %@",[data getString]);
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
   NSLog(@"Error in service");
}

POST 使用 NSURLSession

-(void) postData: (NSString *)requestBody toAddress: (NSString *)url{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];

   [request setHTTPMethod:@"POST"];
   [request setValue:[NSString stringWithFormat:@"%lu",(unsigned long)requestBody.length] forHTTPHeaderField:@"Content-Length"];
   [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
   [request setHTTPBody:[requestBody dataUsingEncoding:NSUTF8StringEncoding]];

    NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration ephemeralSessionConfiguration];

  NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: self delegateQueue: [NSOperationQueue mainQueue]];

   NSURLSessionDataTask *dataTask = [defaultSession dataTaskWithRequest:request];
[dataTask resume];
 }

- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask
didReceiveResponse:(NSURLResponse *)response
completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler
 {
   httpResponse = (NSHTTPURLResponse*)response;
   completionHandler(NSURLSessionResponseAllow);
}

- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask
didReceiveData:(NSData *)data
{
   NSLog(@"Response http status code %ld", (long)[httpResponse statusCode]);
   NSLog(@"response %@",[data getString]);
}
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
didCompleteWithError:(NSError *)error
{
   if (error != nil)
   {
      NSLog(@"Error in service call");
   }
}

推荐答案

最佳教程:关注:http://www.raywenderlich.com/51127/nsurlsession-tutorial

另请参阅此问题:处理多个 NSURL 连接的最佳方法

尝试使用完成处理程序:

Try to use completion handler:

 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
  [request setHTTPMethod:@"POST"];
   [request setValue:[NSString stringWithFormat:@"%lu",(unsigned long)requestBody.length] forHTTPHeaderField:@"Content-Length"];
   [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
   [request setHTTPBody:[requestBody dataUsingEncoding:NSUTF8StringEncoding]];

 NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration ephemeralSessionConfiguration];

 NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: self delegateQueue: [NSOperationQueue mainQueue]];

NSURLSessionDataTask *task = [defaultSession dataTaskWithURL:yourNSURL
  completionHandler:^(NSData *data,
                      NSURLResponse *response,
                      NSError *error) {
    // handle NSData
}];
[task resume];

这篇关于对于简单的 POST 请求,NSURLSession 比 NSURLConnection 花费的时间更长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆