我在向NSMutableURlRequest添加标题时遇到问题,问题是当我在“授权”下面添加标题时,它不显示。但是,如果我用静态字符串exp(@“asdadsadsadga”)替换下面的实例变量“auth”,它将显示“Authorization”标题。我现在很失落。
NSURL *url = [NSURL URLWithString:@"https://www.google.com/analytics/feeds/accounts/default"];
NSMutableURLRequest *profileRequest = [NSMutableURLRequest requestWithURL:url];
NSLog(auth); //prints correctly
NSString *authString = [NSString stringWithFormat:@"GoogleLogin Auth=%@", auth];
[profileRequest addValue:authString forHTTPHeaderField:@"Authorization"];
NSDictionary *allheaders = [profileRequest allHTTPHeaderFields];
for (id key in allheaders)
{
//Nothing prints
NSLog(@"key: %@, value: %@", key, [allheaders objectForKey:key]);
}
NSLog(auth); //Prints correctly
答案 0 :(得分:1)
首先,将NSLog(auth)换成NSLog(@“%@”,auth)。前者很危险,可能会暴露一些正在发生的事情。您还应尽早尝试添加:
auth = @"asdasdasdad";
您还应该尝试直接转储所有标题:
NSLog(@"%@", [profileRequest allHTTPHeaderFields];
这可能会有所启发。我怀疑问题出在你的auth变量中,可能与内存管理有关,而不是在NSMutableURLRequest中。
答案 1 :(得分:0)
用于生成base64Encoded字符串的代码已损坏。
尝试使用页面底部列出的方法: http://cocoawithlove.com/2009/06/base64-encoding-options-on-mac-and.html