2011-12-07 60 views
1

我使用tutorial by IMRAN BHADEL创建了调用WCF Web服务的iPhone。它工作完美。现在,我想打电话给HTTPS服务,但在方法从iPhone调用WCF服务(HTTPS)

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

我得到状态码404方法didReceiveData不叫。如果我尝试浏览到:

https://username:[email protected]/iphone/IphoneService.svc 

中的iPhone模拟器的浏览器(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:[email protected]/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]; 
} 

回答

0

你要实现的NSURLConnection的委托类的身份验证方法。这些允许您拦截并根据身份验证挑战采取行动。查看详情:http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/URLLoadingSystem/Articles/AuthenticationChallenges.html#//apple_ref/doc/uid/TP40009507-SW1

+0

感谢您的支持,但我没有解决问题。我意识到,也许问题是在网址。对于http,我打电话给http:// username:[email protected]/iphone/IphoneService.svc/basic ...在浏览器中,我使用http:// username:[email protected]/iphone/IphoneService.svc打开服务,或者https://开头的用户名:[email protected]/iphone/IphoneService.svc。我应该在网址的末尾使用其他字词而不是“基本”字样 – dule