这个web服务调用代码是什么,具体发生了什么



下面的代码确实调用了web服务,但我并不完全理解它的结构。

NSError *error;
NSData *postData = [NSJSONSerialization dataWithJSONObject: requestData options:0 error:&error];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"private" forHTTPHeaderField:@"Cache-Control"];
[request setValue:@"Microsoft-IIS/7.5" forHTTPHeaderField:@"Server"];
[request setValue:@"Basic realm='TimeBoxWebAPI'" forHTTPHeaderField:@"WWW-Authenticate"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPBody:postData];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:request 
                                   queue:queue         
                       completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) 
{
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
    if ([data length] >0 && error == nil){
        dispatch_queue_t myQueue = dispatch_queue_create("My Queue", DISPATCH_QUEUE_SERIAL);
            dispatch_async(myQueue, ^{
                [self parseGetSchedulingData:data];
                dispatch_async(dispatch_get_main_queue(), ^{
                     [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
                         [tableHolidays reloadData];
                         [refreshControl endRefreshing];
                     if(activityIndicator.isAnimating)
                     {
                         [activityIndicator stopAnimating];
                         [activityIndicator removeFromSuperview];
                     }
                 });
             });
         }
         else if ([data length] == 0 && error == nil){
             NSLog(@"Empty Response, not sure why?");
             [[[UIAlertView alloc] initWithTitle: nil
                                         message:@"Error while trying to log in. Please try later"
                                        delegate:nil
                               cancelButtonTitle:@"OK"
                               otherButtonTitles:nil] show];
             dispatch_async(dispatch_get_main_queue(), ^{
                 [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
                 if(activityIndicator.isAnimating)
                 {
                     [activityIndicator stopAnimating];
                     [activityIndicator removeFromSuperview];
                 }
             });
         }

         else if (error != nil){
             NSLog(@"%@", error.description);
             dispatch_async(dispatch_get_main_queue(), ^{
                 [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
                 if(activityIndicator.isAnimating)
                 {
                     [activityIndicator stopAnimating];
                     [activityIndicator removeFromSuperview];
                 }
             });
         }
     }];
}

特别是,我不确定对setValue:forHTTPHeaderField:sendAsynchronousRequest:queue:completion:的调用到底做了什么。

正如文档所说,

参数

请求

要加载的URL请求。请求对象将作为初始化过程的一部分进行深度复制。此方法返回后对请求所做的更改不会影响用于加载过程的请求。

队列

当请求完成或失败时,处理程序块被调度到的操作队列。

处理程序

要执行的处理程序块。

讨论

如果请求成功完成,则处理程序块的数据参数包含资源数据,错误参数为nil。如果请求失败,则数据参数为nil,错误参数包含有关失败的信息。

如果下载请求需要身份验证,则必须将所需凭据指定为URL的一部分。如果身份验证失败或凭据丢失,连接将尝试在没有凭据的情况下继续。如果请求以401 Unauthorized状态代码结束,则响应参数为nil,数据参数包含资源数据,错误参数为NSError,NSURLErrorDomain错误域中的NSURLErrorUserCancelledAuthentication代码。

可用性

在iOS 5.0及更高版本中可用。

在iOS 9.0中弃用。

如果还有什么不清楚的地方,请告诉我。

编辑1

关于

[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
[request setValue:@"private" forHTTPHeaderField:@"Cache-Control"];
[request setValue:@"Microsoft-IIS/7.5" forHTTPHeaderField:@"Server"];
[request setValue:@"Basic realm='TimeBoxWebAPI'" forHTTPHeaderField:@"WWW-Authenticate"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPBody:postData];

它们为HTTP头字段设置了不同的值,最后为请求上的HTTP正文设置了值。

相关内容

最新更新