0

如何使用 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
4

1 回答 1

1

我想你会把它们放在下面

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

但这引出了一个问题,您尝试过什么?

于 2012-11-17T18:11:26.760 回答