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

我有一个简单的post服务,它将json作为input,并提供一个json作为输出。 当我使用NSURLConnection时,我在500ms内得到响应。 但是如果我使用NSURLSession,则需要至less4-5秒来响应相同的请求。

也有NSURLSession didCompleteWithError总是触发,虽然没有错误,错误是零。 但是在NSURLConnection的情况下didFailWithError是tot射击。

请告诉我我在做什么NSURLSession错误。

POST使用NSURLConnection

-(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连接的最佳方法

尝试使用完成处理程序:

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