iOS 4.3 - Network connection lost when attempting web service connection?
I am working on some code to connect to a secured .NET based web service. I have tried to connect using a manually created SOAP messsage, HTTP POST and HTTP GET. In each try, I am using NSMutableURLRequest on a NSURLConnection. I have implemented didReceiveAuthenticationChallenge in the following manner...
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
NSLog(@"Authentication challenge");
if ([challenge previousFailureCount] == 0)
{
NSLog(@"authentication failed");
NSURLCredential *credential ;
credential = [NSURLCredential credentialWithUser:@"username" password:@"password" persistence:NSURLCredentialPersistenceForSession];
NSLog(@"credential created");
[[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
NSLog(@"credential sent");
}
else
{
NSLog(@"Credentials are wrong!");
[[challenge sender] cancelAuthenticationChallenge:challenge];
}
}
All log messages appear. About 30 seconds after "credential sent" is written, I get the following error.
Connection failed: Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost."
I've found numerous examples here and elsewhere on handling the authentication challenge. Nothing I'm doing seems to be out of the ordinary. I also implemented the following just in case but the results are the same.
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
NSLog(@"canAuthenticateAgainstProtectionSpace");
//return YES to say that we have the necessary credentials to access the requested resource
return YES;
}
The username/password I am sending in response to the challenge exist. I have verified by entering the service URL in a browser and entering the username/password in the authentication dialog. XML is returned when invoking the s开发者_高级运维ervice through the browser.
I've been banging my head against the wall on this for a couple of days now. Any pointers would be greatly appreciated.
Thanks
Sean
I just found my error. The URL wasn't being encoded properly, but I didn't realize that until I had dug a bit into NSUrl URLWithString:
. I added the following code and all is well.
NSString *myURL = [NSString stringWithFormat:@"http://server/path/to/service.asmx/Method?param=%@", param.text];
NSString *fixedURL = [myURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *URL = [NSURL URLWithString:fixedURL];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:URL];
iOS 4.3.1 (just released!) has this in the release notes:
Resolves bugs related to activating and connecting to some cellular networks
Maybe it also addresses your issue. I have similar effects using SOAP under MonoTouch on the iPad. When switching to 3G it claims that it cannot connect (Socket error). I will also try if updating helps.
No not yet. I'm thinking I may have to write a separate service to handle the authentication that sits between the iPad and the service that has the data I need. The iPad won't do any authentication. Not the ideal solution but it'd work for the time being.
精彩评论