对于简单的POST请求,NSURLSession比NSURLConnection花费更长的时间

时间:2015-07-22 10:50:39

标签: ios objective-c

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

同样使用NSURLSession didCompleteWithError总是触发,虽然没有错误,错误是nil。但是在NSURLConnection的情况下,didFailWithError被触发。

请告诉我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");
}

使用NSURLSession进行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]];

    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");
   }
}

1 个答案:

答案 0 :(得分:1)

有关最佳教程:请按照:http://www.raywenderlich.com/51127/nsurlsession-tutorial

另请参阅此问题:Best way to handle multiple NSURL connections

尝试使用完成处理程序:

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