如何使用AFClient设置http标头

时间:2012-11-17 16:08:03

标签: ios http-headers afnetworking

如何使用AFClient设置http标头,在下面的代码中,我应该在哪里设置标头?

我知道AFHTTPClient可以设置http标头。 AFNetworking提供的示例代码如下:

AFAppDotNetAPIClient.h

#import <Foundation/Foundation.h>
#import "AFHTTPClient.h"

@interface AFAppDotNetAPIClient : AFHTTPClient

+ (AFAppDotNetAPIClient *)sharedClient;

@end

AFAppDotNetAPIClient.m

#import "AFAppDotNetAPIClient.h"

#import "AFJSONRequestOperation.h"

static NSString * const kAFAppDotNetAPIBaseURLString = @"https://alpha-api.app.net/";

@implementation AFAppDotNetAPIClient

+ (AFAppDotNetAPIClient *)sharedClient {
    static AFAppDotNetAPIClient *_sharedClient = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedClient = [[AFAppDotNetAPIClient alloc] initWithBaseURL:[NSURL URLWithString:kAFAppDotNetAPIBaseURLString]];
    });

    return _sharedClient;
}

- (id)initWithBaseURL:(NSURL *)url {
    self = [super initWithBaseURL:url];
    if (!self) {
        return nil;
    }

    [self registerHTTPOperationClass:[AFJSONRequestOperation class]];

    // Accept HTTP Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
        [self setDefaultHeader:@"Accept" value:@"application/json"];

    return self;
}

@end

1 个答案:

答案 0 :(得分:1)

我想你会把它们放在

之下

[self setDefaultHeader:@"Accept" value:@"application/json"];

但这引出了问题what have you tried