Вызов службы WCF (HTTPS) с iPhone - PullRequest
       0

Вызов службы WCF (HTTPS) с iPhone

1 голос
/ 07 декабря 2011

С помощью учебника от IMRAN BHADEL я создал iPhone, который вызывает веб-сервис WCF.Работает отлично.Сейчас я пытаюсь вызвать службу https, но в методе

-(void)connection:(NSURLConnection)connection didReceiveResponse:(NSURLResponse *)response

я получаю код состояния 404. Метод didReceiveData не вызывается.Если я попытаюсь перейти к:

https://username:password@192.168.100.102/iphone/IphoneService.svc 

в браузере симулятора iphones (safari), появится сообщение:

"The certificate for this website is invalid. Tap Accept to connect to this website anyway"

Если я нажму Принять, он откроется правильно.Может кто-нибудь помочь мне?

Вот мой код:

- (IBAction) login: (id) sender {

    NSString *soapMessage = [NSString 
                             stringWithFormat:@"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"><SOAP-ENV:Body><GetData><name>%@</name><surname>%@</surname></GetData></SOAP-ENV:Body></SOAP-ENV:Envelope>"
                             , firstfield.text, cesondField.text]];

    NSURL *url = [NSURL URLWithString:@"https://username:password@192.168.100.102/iphone/IphoneService.svc/basic"];


    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];                        
    NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];             

    [theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [theRequest addValue: @"urn:IIphoneService/GetData" forHTTPHeaderField:@"SOAPAction"];
    [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
    [theRequest setHTTPMethod:@"POST"];    
    [theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];


    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

    if (theConnection) {
        webData = [[NSMutableData data] retain];
    }

    loginIndicator.hidden = FALSE;
    [loginIndicator startAnimating];
    loginButton.enabled = FALSE;
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    NSLog(@"___didReceiveResponse");
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
    NSLog(@"Status code %d", [httpResponse statusCode]);

    [webData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    NSLog(@"___didReceiveData");
    [webData appendData:data];
}

- (void) connectionDidFinishLoading:(NSURLConnection *)connection {
    NSLog(@"___connectionDidFinishLoading");
    NSLog(@"DONE. Received Bytes: %d", [webData length]);   
    NSString *xmlData = [[NSString alloc]
                        initWithBytes:[webData mutableBytes]
                        length:[webData length]
                        encoding:NSUTF8StringEncoding];
    //
    //
    //  some code
    //
    //
}


// for self-signed certificates
- (BOOL) connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
    NSLog(@"___canAuthenticateAgainstProtectionSpace");
    return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]; 

}

- (void) connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
    NSLog(@"___didReceiveAuthenticationChallenge");
    if([challenge.protectionSpace.authenticationMethod isEqualToString: NSURLAuthenticationMethodServerTrust]) {

        //trust our domain
        if ([challenge.protectionSpace.host isEqualToString: @"192.168.100.102"]) {
            NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
            [challenge.sender useCredential:credential forAuthenticationChallenge:challenge];
        }
    }
    [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}

1 Ответ

0 голосов
/ 07 декабря 2011

Вы захотите реализовать методы аутентификации класса делегата NSURLConnection. Они позволяют вам перехватывать и проверять подлинность. Подробности смотрите: http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/URLLoadingSystem/Articles/AuthenticationChallenges.html#//apple_ref/doc/uid/TP40009507-SW1

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...