/ / Come fare questo con AFNetworking? - nsurlconnection, afnetworking, asiformdatarequest, nsurlconnectiondelegate

Come fare questo con AFNetworking? - nsurlconnection, afnetworking, asiformdatarequest, nsurlconnectiondelegate

Sto cercando di cambiare il mio vecchio codice "ASIHTTP" in AFNetworking, e mi sono bloccato su questo, Qualcuno può dirmi come convertire questo frammento di codice in 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];

Ho provato AFHTTPClient insieme all'operazione di richiesta ma bloccato nella parte di autenticazione. E ricevendo l'errore 401.

Il codice che ho usato per 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];

risposte:

0 per risposta № 1

Utilizzare AFHTTPClient setAuthorizationHeaderWithUsername: password: metodo per gestire l'autenticazione. Dovrebbe funzionare, almeno nel mio caso lo fa.


0 per risposta № 2

Risolto !!

"ASIHTTP" aggiungeva per default il ""Agente utente", come "App_Name_with_Version (iPad Simulator, iPhone OS 6.0; en_US)" e AFNetworking come no. Così l'ho aggiunto durante la creazione della richiesta e ora funziona.