AFNetworking 401在设备上,在模拟器上工作

时间:2015-04-01 20:35:09

标签: ios afnetworking

所以 - 我有相同的AFNetworking代码,当在模拟器上运行时,会按预期返回结果,并为我提供所有正确的JSON。

当我在设备上运行它时,我得到401.现在,我明白这意味着未经授权,'但它究竟如何在模拟器上完全游泳,然后在设备上给我一个401?

代码如下所示,仅更改为删除必须保密的API地址。

phoneNumber = phoneNumberField.text;

[[NSUserDefaults standardUserDefaults] setObject:phoneNumber forKey:@"phone_number"];

NSString *UDIDstring = [[[UIDevice currentDevice] identifierForVendor] UUIDString];

NSLog(@"UDID: %@",UDIDstring);

NSString *jsonRequest = [NSString stringWithFormat:@"{\"userId\":\"%@\",\"userType\":\"%@\",\"deviceId\":\"%@\",\"deviceType\":\"%@\"}",phoneNumberField.text,@"Number",UDIDstring,@"iPhone"];
NSLog(@"json: %@",jsonRequest);

NSDictionary *stuff = [[NSDictionary alloc] initWithObjectsAndKeys:
                       phoneNumberField.text, @"userId",
                       @"Number", @"userType",
                       UDIDstring,@"deviceId",
                       @"iPhone",@"deviceType",
                       nil];

NSLog(@"stuff: %@",stuff);


NSError *error;

NSData *postData = [NSJSONSerialization dataWithJSONObject:stuff options:0 error:&error];

NSString *urlString = @"(hidden for privacy reasons)"

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]
                                                       cachePolicy:NSURLRequestReloadIgnoringCacheData  timeoutInterval:10];

NSString *userpass = [@"vclient:VastV1" base64EncodedString];
NSLog(@"userpass: %@",userpass);

[request setHTTPMethod:@"POST"];
[request setValue:[NSString stringWithFormat:@"Basic: %@",userpass] forHTTPHeaderField:@"Authorization"];
//[request setValue:@"*/*" forHTTPHeaderField:@"Accept"];
[request setValue: @"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
    securityPolicy.allowInvalidCertificates = YES;
    op.securityPolicy = securityPolicy;
op.responseSerializer = [AFJSONResponseSerializer serializer];


[op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

    NSLog(@"JSON responseObject: %@ ",responseObject);

    [infoLabel setText:@"You have been sent an activation code to the number you used. Please enter it above."];

    [signupLabel setText:@"your code:"];

    [mainButton setTitle:@"Activate" forState:UIControlStateNormal];

    firstSignup=true;

    [phoneNumberField setText:@""];

    [phoneNumberField setPlaceholder:@"Enter activation code."];

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", [error localizedDescription]);

}];
[op start];

3 个答案:

答案 0 :(得分:1)

一个区别是您要报告不同的设备ID。也许您使用的API会跟踪已授权的设备ID。并且您的Mac已获得授权,但iOS设备尚未授权。

您可以尝试在Mac上记录设备ID,然后更改您的代码以将该ID用作硬编码字符串,而不是使用iPhone返回的内容。

答案 1 :(得分:0)

你可以尝试这个,我一直这样做

#import "AFNetworking.h"

NSDictionary *params = @{ @"key1": txtBox1.text, @"key2", txtbox2.text};
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

AFJSONRequestSerializer *serializerRequest = [AFJSONRequestSerializer serializer];
AFJSONResponseSerializer *serializerResponse = [AFJSONResponseSerializer serializer];

[serializerRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]];
serializerResponse.acceptableContentTypes = [NSSet setWithObject:@"application/json"];

manager.requestSerializer = serializerRequest;
manager.responseSerializer = serializerResponse;

[manager.securityPolicy setAllowInvalidCertificates:YES];

[manager POST:@"url"
   parameters:params
      success:^(AFHTTPRequestOperation *operation, id responseObject) {

        // DO WHAT YOU WANT IF SUCCESSED
      }
      failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        // DO WHAT YOU WANT IF FAILED
      }];

答案 2 :(得分:0)

您使用的是依赖于时间的身份验证方案吗?我已经看到模拟器上的漂移导致它变成401。