幾個月前,我曾處於同樣的情況,目前我正在開發一個需要連接到SOAP服務的iPad應用程序。我已經有了這些課程,但是它們僅限於我的項目,我無權分享。然而,我正在製作一個更通用的Objective-C類(無論如何是所有SOAP服務器的最終解決方案),以便在Github上與世界共享它。
首先你必須設置SOAP消息。它有這樣一個共同的結構:
<?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:Header>
// here comes the header information: credentials, connection information. If needed, of course
</soap:Header>
<soap:Body>
// here comes the request parameters. In my case, these parameters are related to a resource called SOAPAction (That identifies the method to be called. e.g getListOfClients). I don't know if it is the same in all servers
</soap:Body>
</soap:Envelope>
我對你說,爲此,我正在做應用的公司,爲我提供了方法和認證信息。我不知道這是否是你的情況,但你在這裏一般瞭解如何做。
我用AFNetworking
發送請求,以這樣的方式
NSString *messageLength = [NSString stringWithFormat:@"%lu", (unsigned long)[msg length]];
NSURL *requestURL = [NSURL URLWithString:self.requestURL];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:requestURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:30];
[theRequest addValue:[requestURL host] forHTTPHeaderField:@"Host"];
[theRequest addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue:soapAction forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue:messageLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody:[msg dataUsingEncoding:NSUTF8StringEncoding]];
// sending the request
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:theRequest];
operation.responseSerializer = [AFHTTPResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *xml = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"Take the data master Yoda: %@", xml);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Bad SOAP server!. Error: %@", error.description)
}];
[[NSOperationQueue mainQueue] addOperation:operation];
self.requestURL
是服務器URL,效果顯着。如果請求成功,則可以準備解析服務器的xml響應。否則,請求錯誤描述。
此頁幫了我很多,找出我遇到了一些問題的解決方案,它是關係到網站http://sudzc.com/您在上面引述:
http://blog.exadel.com/working-with-ios-and-soap/
我希望這有助於在任何辦法。
所以,@ kmiklas,你找到了解決辦法嗎? – epologee 2015-03-19 11:25:19