質問

I see how to set default headers using AFHTTPClient, but I would like to set certain headers only when I perform requests to certain resources. Is there a way to this than building my own NSURLRequest (with the correct headers), creating an operation and enqueuing that operation to my subclass of AFHTTPClient?

役に立ちましたか?

解決

If your network request are going to differ significantly it might be easier to create multiple AFHTTPClient subclasses to deal with the different cases. Adding your headers in in - (id)initWithBaseURL:(NSURL *)url.

Otherwise whenever you are creating your NSURLRequest with your AFHTTPClient subclass you are able to set headers with a few different methods:

[client setDefaultHeader:@"key" value:@"value"];
[client setAuthorizationHeaderWithUsername:@"username" password:@"password"];
[client setAuthorizationHeaderWithToken:@"token"];

You can set these before creating your NSURLRequest with something like:

NSURLRequest *request = [client requestWithMethod:@"someMethod" path:@"somePath" parameters:someParameters];

Then your headers should only be in that request. You can also clear your authorization header using - (void)clearAuthorizationHeader. Taking a bit from the AFNetworking source code you can remove any necessary objects from the NSMutableDictionary if they are not otherwise removed.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top