使用tutorial by IMRAN BHADEL我创建的iPhone调用WCF webservice。它完美无缺。现在我试图调用https服务,但在方法
-(void)connection:(NSURLConnection)connection didReceiveResponse:(NSURLResponse *)response
我得到状态代码404.方法didReceiveData未被调用。如果我尝试浏览:
https://username:password@192.168.100.102/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: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];
}
答案 0 :(得分:0)
您需要实现NSURLConnection委托类的身份验证方法。这些允许您拦截并根据身份验证挑战采取行动。有关详细信息,请参阅:http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/URLLoadingSystem/Articles/AuthenticationChallenges.html#//apple_ref/doc/uid/TP40009507-SW1