我正计划为 iPhone 开发一个应用程序,该应用程序必须访问几个 SOAP 服务。在对 iPhone SDK 进行一些基本检查时,我找不到任何对访问 SOAP 服务的支持,通过谷歌搜索得出的结论是 iPhone SDK 中不支持 SOAP。
因此,如果我确实想构建那个应用程序,我需要想出一种方法来从 iPhone 访问 SOAP 服务。最好的方法是什么?有什么最佳实践吗?是否有人已经使用 iPhone SDK 中的功能编写了一个库来访问 SOAP 服务?
(由于我需要访问的服务由另一方公开,并且他们只将其公开为 SOAP,不幸的是,它不是切换到另一种类型的接口(例如基于 REST 的 API)的选项。
下吕
一个字:不要。
好吧,显然这不是一个真正的答案。但仍应不惜一切代价避免使用 SOAP。 ;-) 是否可以在 iPhone 和网络服务之间添加代理服务器?或许可以为您将 REST 转换为 SOAP?
您可以试试 CSOAP,这是一个依赖于 libxml2(包含在 iPhone SDK 中)的 SOAP 库。
我为 OSX 编写了自己的 SOAP framework。但是,它没有得到积极维护,需要一些时间才能移植到 iPhone(您需要先将 NSXML 替换为 TouchXML)
我的解决方案是使用 PHP 让代理服务器接受 REST、发出 SOAP 请求并返回结果。
实施时间:15-30分钟。
不是最优雅,但坚固。
我一直在低级别(XML 生成和解析)上滚动自己的访问,以处理偶尔需要从 Objective-C 执行 SOAP 样式请求。也就是说,有一个名为 SOAPClient (soapclient) 的库可用,它是开源的(BSD 许可)并可在可能感兴趣的 Google 代码 (mac-soapclient) 上使用。
我不会证明它的能力或有效性,因为我从未使用过它或不得不使用它的 API,但它是可用的,并且可能会根据您的需要为您提供快速解决方案。
Apple 曾经有一个非常糟糕的实用程序,称为 WS-MakeStubs。我不认为它在 iPhone 上可用,但您可能还对旨在替换它的开源库感兴趣 - 代码生成 Objective-C 用于与 SOAP 客户端交互。同样,我没有使用它 - 但我已经在我的笔记中标记了它:wsdl2objc
您可以使用我找到的工具进行连接 http://www.wsdl2code.com
SampleServiceProxy *proxy = [[SampleServiceProxy alloc]initWithUrl:@"YOUR
URL" AndDelegate:self];
[proxy GetDouble];
[proxy GetEnum];
[proxy getEnum:kTestEnumTestEnum2];
[proxy GetInt16];
[proxy GetInt32];
[proxy GetInt64];
[proxy GetString];
[proxy getListStrings];
这是一个使用 SOAP 服务格式执行 API 调用的 swift 4 示例代码。
func callSOAPWSToGetData() {
let strSOAPMessage =
"<?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>" +
"<CelsiusToFahrenheit xmlns=\"http://www.yourapi.com/webservices/\">" +
"<Celsius>50</Celsius>" +
"</CelsiusToFahrenheit>" +
"</soap:Body>" +
"</soap:Envelope>"
guard let url = URL.init(string: "http://www.example.org") else {
return
}
var request = URLRequest.init(url: url)
let length = (strSOAPMessage as NSString).length
request.addValue("application/soap+xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.addValue("http://www.yourapi.com/webservices/CelsiusToFahrenheit", forHTTPHeaderField: "SOAPAction")
request.addValue(String(length), forHTTPHeaderField: "Content-Length")
request.httpMethod = "POST"
request.httpBody = strSOAPMessage.data(using: .utf8)
let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)
let task = session.dataTask(with: request) { (data, response, error) in
guard let responseData = data else {
print("Error: did not receive data")
return
}
guard error == nil else {
print("error calling GET on /todos/1")
print(error ?? "")
return
}
print(responseData)
let strData = String.init(data: responseData, encoding: .utf8)
print(strData ?? "")
}
task.resume()
}
不定期副业成功案例分享