ios 授权标头在NSMutableURLRequest中不工作

mwkjh3gx  于 11个月前  发布在  iOS
关注(0)|答案(2)|浏览(96)

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

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";
} }

km0tfn4u

km0tfn4u1#

NSMutableURLRequest* request = [[[NSMutableURLRequest alloc] initWithURL:url]
                                autorelease];

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

字符串
或者添加一个header:

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

os8fio9y

os8fio9y2#

没有名为“APIPLICATION KEY”的HTTP头。因此,

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

字符串
你应该使用,

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

相关问题