我是您几个月前的同样处境,目前正在开发一款需要连接到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]];
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/
希望这能在某种程度上帮到你。