2011-11-05 297 views
2

我想在每個請求的標頭中傳遞API密鑰。API密鑰必須在每個請求中作爲API密鑰標頭傳遞

NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]]; 
NSLog(@"%@",request); 
[request setHTTPMethod: @"POST"]; 

[request addValue:@"4eaa90d7175e98732543994eaa90d717" forHTTPHeaderField:@"API-Key"] ; 

request = [NSURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy: 
      NSURLRequestUseProtocolCachePolicy timeoutInterval:100]; 

NSLog(@"%@",url); 
NSLog(@"%@",[request allHTTPHeaderFields]); 

allHTTPHeaderFields爲空。我錯過了什麼?

回答

2

您首先使用具有標題字段的NSMutableURLRequest初始化請求,然後使用在標題字段中沒有API密鑰的NSURLRequest覆蓋它。 剛擺脫這條線:

request = [NSURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy: 
      NSURLRequestUseProtocolCachePolicy timeoutInterval:100];