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