我在Objective C中调用了一个休息Web服务请求。如何将标记添加到Web服务请求进行身份验证?
提前致谢
答案 0 :(得分:1)
在请求中添加令牌,如下所示
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:yourURL] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setHTTPMethod:@"GET"]; // Set your method
[request addValue:@"token_value" forHTTPHeaderField:@"Authorization"];
// ...... your code
// ........ add data
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
//HideProcess;
if (error)
{
//NSLog(@"Error : %@\n", error);
return;
}
if (data != nil)
{
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
NSLog(@"Response :\n%@\n", dict);
}
}];
[task resume];