在Objective C中使用安全的https webservice

时间:2012-09-07 07:23:30

标签: objective-c web-services cocoa cookies cookiecontainer

我必须编辑Objective C中的代码,使用登录名,密码和cookie容器调用安全的https webservice。

实际代码:

NSString *plistInfosServeurPath = [[NSBundle mainBundle] pathForResource:@"InfosServeur" ofType:@"plist"];
NSDictionary *infosServeurDictionary = [NSDictionary dictionaryWithContentsOfFile:plistInfosServeurPath];
NSString *urlServeur = [infosServeurDictionary objectForKey:@"serveur_url"];


/* ADDED BY ME */
/* ------- */   
NSURLCredential *newcredential = [NSURLCredential credentialWithUser:@"plop" password:@"Plop" persistence:NSURLCredentialPersistenceNone];

NSURLProtectionSpace *space = [[NSURLProtectionSpace alloc] initWithHost:@"https://plop.com"
                                                                    port:443
                                                                protocol:@"https"
                                                                   realm:nil
                                                    authenticationMethod:NSURLAuthenticationMethodHTTPBasic];

[[NSURLCredentialStorage sharedCredentialStorage] setCredential:newcredential
                                             forProtectionSpace:space];

/* ------- */

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[[NSURL alloc] initWithString:[NSString stringWithFormat:@"%@/plop.svc/plop/%@", urlServeur, _gare.code]]];
[request setHTTPMethod:@"GET"];

[NSURLConnection sendAsynchronousRequest:request 
                                   queue:[NSOperationQueue mainQueue] 
                       completionHandler:^(NSURLResponse *response, NSData *result, NSError *error){
                           if (result) 
                           {

                               NSArray *trains = [[CJSONDeserializer deserializer] deserializeAsArray:result error:&error];

                               }
                           if (erro) {
                               NSLog(@"connexion NOK");
                               NSLog(@"ERROR : %@", error);
                           }
                       }
 ];

该代码适用于http的web服务,但现在使用https,我尝试添加凭据,但没有成功,也没有找到如何添加cookie容器。

0 个答案:

没有答案