Ios 授权标头在NSMutableURLRequest中不起作用

Ios 授权标头在NSMutableURLRequest中不起作用,ios,objective-c,iphone,api,cocoa-touch,Ios,Objective C,Iphone,Api,Cocoa Touch,我正在尝试使用NSMutableURLRequest将cid=@“value”作为授权标头发送。但它抛出了一个错误。任何帮助都将不胜感激。 这是我的密码 NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; [request setURL:[NSURL URLWithString:@"url"]]; [request setHTTPMethod:@"GET"]; [request setValue:@"applica

我正在尝试使用NSMutableURLRequest将cid=@“value”作为授权标头发送。但它抛出了一个错误。任何帮助都将不胜感激。 这是我的密码

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"url"]];
[request setHTTPMethod:@"GET"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"value" forHTTPHeaderField:@"APIAccessKey"];
[request setValue:[NSString stringWithFormat:@"cid=\"%@\"", @"hdemPsqxVSVSJCRSt8PHmUHUyUtXk="]    forHTTPHeaderField:@"Authorization"];
但是api返回这个错误

 <NSHTTPURLResponse: 0x7fff0a41de80> { URL: http://url } { status code: 401, headers {
    "Cache-Control" = "no-cache";
    "Content-Length" = 31;
    "Content-Type" = "application/json; charset=utf-8";
    Date = "Tue, 26 May 2015 11:36:50 GMT";
    Expires = "-1";
    "App-Authenticate" = "Basic Scheme='API' location='https:url";
    Pragma = "no-cache";
    Server = "Microsoft-IIS/7.5";
    "X-AspNet-Version" = "4.0.30319";
    "X-Powered-By" = "ASP.NET";
} }
{URL:http://url }{状态代码:401,标题{
“缓存控制”=“无缓存”;
“内容长度”=31;
“内容类型”=“应用程序/json;字符集=utf-8”;
日期=“2015年5月26日星期二11:36:50 GMT”;
Expires=“-1”;
“应用程序验证”=“基本方案='API'位置='https:url”;
Pragma=“无缓存”;
Server=“Microsoft IIS/7.5”;
“X-AspNet-Version”=“4.0.30319”;
“X-Powered-By”=“ASP.NET”;
} }
或添加标题:

[request addValue:VALUE forHTTPHeaderField:@"field to set"];

没有名为“APIAccessKey”的HTTP头。因此,与其写作

[request setValue:@"value" forHTTPHeaderField:@"APIAccessKey"];
你应该用

[request addValue:@"value" forHTTPHeaderField:@"APIAccessKey"];

如果您使用“”而不是您正在使用的,它是否有效。NSString*authorizationHeaderValue=[NSString stringWithFormat:@“cid='%@',authorizationToken]不,它不适用于NSMutableURLRequest,并且不鼓励使用,但我不知道为什么。适用于
授权、连接、主机、代理身份验证、代理身份验证、WWW-Authenticate
。我不知道为什么会泄气,所以如果有人知道,就大声说出来。:)您在这里的建议只是将“value”附加到“APIAccessKey”头的现有值(如果有的话)。它不会“添加”新标题。
[request addValue:@"value" forHTTPHeaderField:@"APIAccessKey"];