iOS基本SOAP身份验证过程 [英] iOS Basic SOAP Authentication process

查看:89
本文介绍了iOS基本SOAP身份验证过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自上周以来,我一直在处理一个直截了当且令人讨厌的问题.我有一个带有访问身份验证的SOAP服务.简短的答案是它不起作用,而更长的故事是,我已经尝试了相同的Android服务,并且在一些帮助下我能够成功运行它.因此,最后的结论是我的iOS代码中存在问题.

I have a straight and tiresome issue I have been working on since last week. I have a SOAP service with access authentication. Short answer is it's not working, the longer story is that I have tried the same service for Android and with some help I am able to run it successfully. So the final conclusion is issue exists in my code for iOS.

这是代码示例.注意:我已经为其他在线服务尝试了相同的代码(没有身份验证部分),并且工作正常.但是我正在使用的服务没有按预期方式响应.

Here is the code example. Note: I have tried the same code (without authentication portion) for other online services and it's working fine. But the service I am working on is not responding as expected.

NSString *sSOAPMessage = @"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
"<soap:Body>"
"<HelloWorld xmlns=\"http://www.eposanytime.co.uk\">"
"</HelloWorld>"
"</soap:Body>"
"</soap:Envelope>";

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");
}

每当我运行此代码时,都会出现未授权访问的错误.

此代码放置在viewDidLoad操作中.我已经尝试过在Google上找到的尽可能多的链接,但无法获得正确的答案或结果.因此,如果您知道的话,请对我来说是个好办法.谢谢

This code is placed inside viewDidLoad action. I have tried as many links as I found over google but couldn't get proper answer or result. So please if any of you know the answer would be great for me. Thanks

推荐答案

尝试添加授权:

您需要用户名和密码API

You need user and password 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);

这篇关于iOS基本SOAP身份验证过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆