2011-12-07 11 views
1

tutorial by IMRAN BHADELでは、WCF Webサービスを呼び出すiPhoneを作成しました。完璧に動作します。今、私はサービスのhttpsを呼び出すしようとしていますが、この方法iPhoneからWCFサービス(HTTPS)を呼び出す

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

に私はdidReceiveDataが呼び出されないステータスコード404の方法を取得します。私はを参照しよう:私は同意する]をタップした場合、それは正しく開く

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

:iPhoneのシミュレータのブラウザ(サファリ)で

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

、そのメッセージを示しています。

誰かが私を助けることができますか?

- (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

ありがとうございますが、私は問題を解決しませんでした。私はおそらく問題がURLにあることに気づいた。 httpの場合http:// username:[email protected]/iphone/IphoneService.svc/basicを呼び出します。ブラウザでは、http:// username:[email protected]/iphone/IphoneService.svcまたはhttps:// username:[email protected]/iphone/IphoneService.svc。私はURLの最後に "basic"の代わりに他の単語を使うべきですか? – dule

関連する問題