几个月前我遇到了同样的情况,我目前正在使用需要连接到 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/
我希望这对您有所帮助。