ios - NSMutableURLRequest 设置授权头

标签 ios nsurlsession nsurlrequest

当设置 NSMutableURLRequest 的“授权” header 时,我的服务器对 header 的响应不包括该 header :

[Host] => myhost.com
[Content-Type] => application/x-www-form-urlencoded
[Connection] => keep-alive
[Accept] => */*
[User-Agent] => MyApp/1 CFNetwork/758.3.15 Darwin/15.4.0
[Content-Length] => 327
[Accept-Language] => en-gb
[Accept-Encoding] => gzip, deflate

我阅读了文档建议不要在这里设置它,那么我应该在哪里设置客户端的授权 header ?

我的目的Authorization header 是发送我的 Oauth 签名和其他 Oauth 相关信息

最佳答案

你有用户和密码 API

 // Create the request
 NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:0];

// New Create the connection
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];

NSURLSession *session = [NSURLSession sharedSession];//sessionWithConfiguration:defaultConfigObject delegate:self delegateQueue:[NSOperationQueue mainQueue]];

NSURLCredential *creds = [NSURLCredential credentialWithUser:self.username password:self.password persistence:NSURLCredentialPersistenceForSession];

NSString *authStr = [NSString stringWithFormat:@"%@:%@",self.username,self.password];// @"username:password";

NSData *authData = [authStr dataUsingEncoding:NSUTF8StringEncoding];

NSString *authValue = [NSString stringWithFormat: @"Basic %@",[authData base64EncodedStringWithOptions:0]];

// Part Important
[request setValue:authValue forHTTPHeaderField:@"Authorization"];

// Or Token
 NSString *authValueToken = @"OAuth UElJRFER1A5zcGkyW16T0";
 [request setValue:authValueToken forHTTPHeaderField:@"AuthenticatedToken"];// Authenticated API


NSString *postLength = [NSString stringWithFormat:@"327"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];

NSString *postLength = [NSString stringWithFormat:@"application/x-www-form-urlencoded"];
[request setValue:postLength forHTTPHeaderField:@"Content-Type"];

NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request     
    completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
           receivedData = [NSMutableData data];
           NSString* responseData = [[NSString alloc] initWithData:data encoding: NSUTF8StringEncoding];
           NSLog(@"%@",responseData);
           if (error) {
                   [self handleError: error];
           }
 }];

[dataTask resume]; // <- important

NSLog(@"Header Fields Request--->> %@",request.allHTTPHeaderFields);

关于ios - NSMutableURLRequest 设置授权头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36872673/

相关文章:

ios - 在自定义 UINavigationController 类中创建 UINavigationBar 时出错

ios - NSURLSession 错误域=NSPOSIXErrorDomain 代码=2 "No such file or directory"

swift - swift 从 http sendAsyncRequest 调用中返回数据

ios - 平移 UITableViewCell 时会显示什么 View ?

iphone - MailCore SMTP 服务器证书无效

ios - 我正在尝试使用 Twilio sdk 进行调用,但我的调用在 2 或 3 秒后断开

ios - 无法使用mockURLSession调用非函数类型 'URLSession'错误的值

ios - 如何在 swift iOS 中通过 nsurlsession 为 REST API 创建全局函数?

ios - 奇怪的NSURLConnection缓存行为

ios - session 配置超时间隔无法设置超过 60 秒