AFNetworking似乎没有使用AFJSONRequestOperation发送授权标头

时间:2012-11-05 08:02:01

标签: header authorization afnetworking

我正在尝试使用AFNetworking和AFJSONRequestOperation发送授权标头。如果我在setAuthorizationHeaderWithToken之后NSLog httpClient,它将显示它在标题中,但是当我检查它发送到的服务器时,它似乎没有收到Authorization标题(它接收其他部分)。

AFJSONRequestOperation是否对未添加授权部分的标题执行某些操作?

NSURL *url = [NSURL URLWithString:kBaseURL];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];

 NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:@"/" parameters:params];   

[httpClient setAuthorizationHeaderWithToken:@"test"];

AFJSONRequestOperation *operation = nil;

operation = [AFJSONRequestOperation
             JSONRequestOperationWithRequest:request
             success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {

             }
             failure:^(NSURLRequest *request , NSHTTPURLResponse *response, NSError *error , id JSON ){

             }];

[operation start];

NSURL *url = [NSURL URLWithString:kBaseURL]; AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url]; NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:@"/" parameters:params]; [httpClient setAuthorizationHeaderWithToken:@"test"]; AFJSONRequestOperation *operation = nil; operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { } failure:^(NSURLRequest *request , NSHTTPURLResponse *response, NSError *error , id JSON ){ }]; [operation start];

1 个答案:

答案 0 :(得分:2)

您是否尝试在使用httpClient创建NSMutableURLRequest(在此代码块中似乎不需要是可变的)之前添加标头?在您创建请求之前,您似乎没有添加令牌标头,在这种情况下,AFJSONRequestOperation甚至从未看到标头,因为真正位于AFHTTPClient的{​​{1}}该功能只是一个添加名为“Authorization”

的HTTP标头的便捷方法