简体   繁体   English

如何在iOS Objective C中将令牌添加到Rest Web服务请求

[英]How to add token to a Rest web service request in iOS Objective C

I have a rest web service request to be called in Objective C. How to add token to web service request for authentication ?? 我有一个在目标C中调用的其余Web服务请求。如何向Web服务请求中添加令牌以进行身份​​验证?

Thanks in advance 提前致谢

Add token in request as below 在请求中添加令牌,如下所示

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM