How to do this with AFNetworking?

1k Views Asked by At

I am trying to change my old 'ASIHTTP' code to AFNetworking, and stuck over this, Can some one tell me how to convert this chunk of code to AFNetworking

NSString *urlString = [NSString stringWithFormat:kLogin];
NSURL *url= [NSURL URLWithString:urlString];

ASIFormDataRequest *request= [ASIFormDataRequest requestWithURL:url];
[activeRequests addObject:request];
[request setValidatesSecureCertificate:YES]; //SSL enforcement
[request addRequestHeader:@"Host" value:kHost];
[request addRequestHeader:@"Accept" value:@"application/xml, text/javascript, */*; q=0.01"];
[request addRequestHeader:@"Accept-Language" value:@"en-us,en;q=0.5"];
[request addRequestHeader:@"Accept-Encoding" value:@"gzip, deflate"];
[request addRequestHeader:@"Connection" value:@"keep-alive"];
[request addRequestHeader:@"X-Requested-With" value:@"XMLHttpRequest"];
[request addRequestHeader:@"Authorization" value:[NSString stringWithFormat:@"Basic %@", [Utils base64String:[NSString stringWithFormat:@"%@:%@", username, password]]]];
[request setDelegate:self];        
[[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
[ASIHTTPRequest clearSession];
[request setTimeOutSeconds:REQUEST_TIME_OUT_SECS];
[request startAsynchronous];

I have tried AFHTTPClient along with request operation but stuck in the authentication part. And getting Error 401.

The code which I have used for AFNetworking,

NSString *urlString = [NSString stringWithFormat:kLogin]; NSURL *url= [NSURL URLWithString:urlString];

AppHTTPClient* httpClient = [[AppHTTPClient alloc] initWithBaseURL:url];
[httpClient setDefaultHeader:@"Host" value:kHost];
[httpClient setDefaultHeader:@"Accept" value:@"application/xml, text/javascript, */*; q=0.01"];
[httpClient setDefaultHeader:@"Accept-Language" value:@"en-us,en;q=0.5"];
[httpClient setDefaultHeader:@"Accept-Encoding" value:@"gzip, deflate"];
[httpClient setDefaultHeader:@"Connection" value:@"keep-alive"];
[httpClient setDefaultHeader:@"X-Requested-With" value:@"XMLHttpRequest"];

[httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];

[httpClient setAuthorizationHeaderWithUsername:username password:password];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"GET"
                                                        path:nil
                                                  parameters:nil];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];


operation.allowsInvalidSSLCertificate = YES;

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"2");

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    NSLog([error localizedDescription]);
    NSLog(operation.responseString);

}
 ];
[operation start];
2

There are 2 best solutions below

1
On

Use the AFHTTPClient setAuthorizationHeaderWithUsername:password: method to deal with the authentication. It should work, at least in my case it does.

0
On

Solved !!

The 'ASIHTTP' was by default adding the 'User-Agent', as 'App_Name_with_Version (iPad Simulator; iPhone OS 6.0; en_US)' and AFNetworking as not. So I added this during the request creation and its working now.