iOS基本SOAP身份validation过程
自上周以来,我一直在努力解决这个问题。 我有一个带访问身份validation的SOAP服务。 简短的回答是它不起作用,更长的故事是我已经为Android尝试了相同的服务,并且在一些帮助下我能够成功运行它。 所以最终的结论是我的iOS代码中存在的问题。
这是代码示例。 注意:我已经为其他在线服务尝试了相同的代码(没有身份validation部分),并且它工作正常。 但我正在处理的服务没有按预期响应。
NSString *sSOAPMessage = @"" "" "" "" "" "" ""; NSURL *sRequestURL = [NSURL URLWithString:@"http://www.superplaice.co.uk/AppWebServices.asmx"]; NSMutableURLRequest *myRequest = [NSMutableURLRequest requestWithURL:sRequestURL]; NSString *sMessageLength = [NSString stringWithFormat:@"%lu", (unsigned long)[sSOAPMessage length]]; //Basic Authentication NSString *username = @"john"; NSString *password = @"john123"; NSString *authenticationString = [NSString stringWithFormat:@"%@%@%@", username,@":", password]; NSData *authenticationData = [authenticationString dataUsingEncoding:NSASCIIStringEncoding]; NSString *authenticationValue = [authenticationData base64EncodedStringWithOptions:0]; //Set up your request NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.superplaice.co.uk/AppWebServices.asmx/"]]; // Set your user login credentials [request setValue:[NSString stringWithFormat:@"Basic %@", authenticationValue] forHTTPHeaderField:@"Authorization"]; [myRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; [myRequest addValue: @"http://www.eposanytime.co.uk/HelloWorld" forHTTPHeaderField:@"SOAPAction"]; [myRequest addValue: sMessageLength forHTTPHeaderField:@"Content-Length"]; [myRequest setHTTPMethod:@"POST"]; [myRequest setHTTPBody: [sSOAPMessage dataUsingEncoding:NSUTF8StringEncoding]]; NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:myRequest delegate:self]; if( theConnection ) { webResponseData = [NSMutableData data]; }else { NSLog(@"Some error occurred in Connection"); }
每当我运行此代码时,我都会收到Unauthorized Access错误。
此代码放在viewDidLoad操作中。 我尝试了尽可能多的链接,因为我发现谷歌,但无法得到正确的答案或结果。 所以,如果你们中的任何人都知道答案对我来说很棒。 谢谢
尝试添加授权:
您需要用户和密码API
// Create the request NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:0]; // New Create the connection NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration]; NSURLSession *session = [NSURLSession sharedSession];//sessionWithConfiguration:defaultConfigObject delegate:self delegateQueue:[NSOperationQueue mainQueue]]; NSURLCredential *creds = [NSURLCredential credentialWithUser:self.username password:self.password persistence:NSURLCredentialPersistenceForSession]; NSString *authStr = [NSString stringWithFormat:@"%@:%@",self.username,self.password];// @"username:password"; NSData *authData = [authStr dataUsingEncoding:NSUTF8StringEncoding]; NSString *authValue = [NSString stringWithFormat: @"Basic %@",[authData base64EncodedStringWithOptions:0]]; // Part Important [request setValue:authValue forHTTPHeaderField:@"Authorization"]; // NSString *postLength = [NSString stringWithFormat:@"%lu",[postData length]+2]; // [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { receivedData = [NSMutableData data]; NSString* responseData = [[NSString alloc] initWithData:data encoding: NSUTF8StringEncoding]; NSLog(@"%@",responseData); if (error) { [self handleError: error]; } }]; [dataTask resume]; NSLog(@"Header Request--->> %@",request.allHTTPHeaderFields);