我计划为iPhone开发一个应用程序,该应用程序必须访问几个SOAP服务。虽然在iPhone SDK中做了一些基本的检查,但我无法找到任何对访问SOAP服务的支持,谷歌一下得出的结论是,iPhone SDK中不支持SOAP。
因此,如果我确实想要构建该应用程序,我将需要提出一种从iPhone访问SOAP服务的方法。最好的方法是什么?有什么最佳实践吗?是否有人已经编写了一个使用iPhone SDK中提供的功能来访问SOAP服务的库?
(因为我需要访问的服务是由另一方公开的,他们只将其公开为SOAP,不幸的是,不能切换到另一种类型的接口(例如基于REST的API)。
Gero
下面是一个使用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()
}
下面是一个使用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()
}