gpt4 book ai didi

ios - 401错误-使用AFNetworking AFHTTPClient和Tastypie进行基本身份验证

转载 作者:行者123 更新时间:2023-12-01 18:27:09 24 4
gpt4 key购买 nike

我正在使用来自AFNetworking的AFHTTPClient从我的IOS应用程序向服务器进行调用,该服务器使用Django与DeliciousPie。当我在服务器端关闭身份验证时,它的工作效果很好。但是,当我需要身份验证并将正确的用户名和密码插入我的代码时,我会收到以下401身份验证错误:

\2012-09-16 00:24:37.877 RESTtest[76909:f803] 
Complex AF: Error Domain=AFNetworkingErrorDomain Code=-1011
"Expected status code in (200-299), got 401"
UserInfo=0x686ba00 {AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x686f130>,
NSErrorFailingURLKey=http://127.0.0.1:8000/api/v1/shoppinglist,
NSLocalizedDescription=Expected status code in (200-299), got 401,
AFNetworkingOperationFailingURLRequestErrorKey=<NSMutableURLRequest http://127.0.0.1:8000/api/v1/shoppinglist>}

这是我的代码:

AFAPIClient.h
#import“AFHTTPClient.h”
@interface AFAPIClient : AFHTTPClient

-(void)setUsername:(NSString *)username andPassword:(NSString *)password;

+ (AFAPIClient *)sharedClient;

@end

AFAPIClient.m:
#import "AFAPIClient.h"
#import "AFJSONRequestOperation.h"


static NSString * const baseURL = @"http://@127.0.0.1:8000/api/v1";


@implementation AFAPIClient

+ (AFAPIClient *)sharedClient {
static AFAPIClient *_sharedClient = nil;
static dispatch_once_t pred;
dispatch_once(&pred, ^{
_sharedClient = [[AFAPIClient alloc] initWithBaseURL:[NSURL URLWithString:baseURL]];
//[_sharedClient setAuthorizationHeaderWithUsername:@"myusername" password:@"mypassword"]; I tried putting the authorization command here
});

return _sharedClient;
};

#pragma mark - Methods

-(void)setUsername:(NSString *)username andPassword:(NSString *)password;
{
[self clearAuthorizationHeader];
[self setAuthorizationHeaderWithUsername:username password:password];
}

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

[self registerHTTPOperationClass:[AFJSONRequestOperation class]];
[self setParameterEncoding:AFJSONParameterEncoding];


//[self setAuthorizationHeaderWithUsername:@"myusername" password:@"mypassword"]; I also tried putting the authorization command here

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

return self;
}


@end

TQViewController.h:
[...]
- (IBAction)sendAFClientRequest:(id)sender {
//[[AFAPIClient sharedClient] setUsername:@"myusername" andPassword:@"mypassword"];
[[AFAPIClient sharedClient] getPath:@"shoppinglist" parameters:nil success:^(AFHTTPRequestOperation *operation, id response) {
NSLog(@"Complex AF: %@", [response valueForKeyPath:@"objects"]);
} failure:^(AFHTTPRequestOperation *operation, id response) {
NSLog(@"Complex AF: %@", response);
}
];

}
[...]

我知道这不是服务器或用户名/密码的问题,因为我可以通过将用户名/密码插入URL来进行身份验证:
@"http://myusername:mypassword@127.0.0.1:8000/api/v1/shoppinglist"

任何帮助都会很棒。能够使用AFHTTPClient而不将身份验证信息直接插入到静态基本URL(这似乎是完全不正确的)将是很棒的。提前致谢!

最佳答案

基于此:https://github.com/AFNetworking/AFNetworking/issues/426

我在AFHTTPClient中覆盖了- (void)getPath:(NSString *)path parameters...方法
子类看起来像这样:

- (void)getPath:(NSString *)path parameters:(NSDictionary *)parameters
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
{
NSURLRequest *request = [self requestWithMethod:@"GET" path:path parameters:parameters];
AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
[operation setAuthenticationChallengeBlock:^(NSURLConnection *connection, NSURLAuthenticationChallenge *challenge) {
NSURLCredential *newCredential = [NSURLCredential credentialWithUser:self.username password:self.password persistence:NSURLCredentialPersistenceForSession];
[challenge.sender useCredential:newCredential forAuthenticationChallenge:challenge];
}];
[self enqueueHTTPRequestOperation:operation];
}

它仅将身份验证质询块添加到AFHTTPRequestOpertaion,其余与原始实现 https://github.com/AFNetworking/AFNetworking/blob/master/AFNetworking/AFHTTPClient.m相同

关于ios - 401错误-使用AFNetworking AFHTTPClient和Tastypie进行基本身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12445221/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com