2

我必须在 Objective C 中编辑代码,以使用登录名、密码和 cookie 容器调用安全的 https 网络服务。

代码实际上:

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 容器。

4

0 回答 0