2016-03-08 64 views
0

我自上週以來一直在努力研究一個令人頭痛的問題。我有一個訪問身份驗證的SOAP服務。簡短的答案是它不工作,更長的故事是,我已經嘗試了Android的相同的服務,並有一些幫助,我能夠成功地運行它。所以最後的結論是存在於我的iOS代碼中。iOS基本SOAP身份驗證過程

這裏是代碼示例。注意:我已經爲其他在線服務嘗試了相同的代碼(沒有認證部分),並且工作正常。但是我正在處理的服務沒有按預期做出響應。

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動作中。我試過儘可能多的鏈接,因爲我發現了谷歌,但無法得到正確的答案或結果。所以,如果你們中的任何人知道答案對我來說很好,那麼請。由於

回答

0

嘗試增加授權:

您需要用戶名和密碼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);