С помощью учебника от 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];
}