簡體   English   中英

如何在iOS Objective C中將令牌添加到Rest Web服務請求

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

我有一個在目標C中調用的其余Web服務請求。如何向Web服務請求中添加令牌以進行身份​​驗證?

提前致謝

在請求中添加令牌,如下所示

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