在过去的几天里,我一直在尝试快速发出 SOAP 请求,但没有成功,我想知道是否有人可以指导我获得成功的响应。我知道 NSURLConnection 已被弃用,但我想测试一下我是否可以发出成功的请求,因为我可以在网上找到的所有示例都使用了它。
肥皂请求直接来自他们的 API 网站https://developers.mindbodyonline.com/,可以免费注册。
let requestURL = NSURL(string: "https://api.mindbodyonline.com/0_5/SiteService.asmx")
let myRequest = NSMutableURLRequest(URL: requestURL!)
let soapMessage =
"<soapenv:envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns=\"http://clients.mindbodyonline.com/api/0_5\">" +
"<soapenv:header />" +
"<soapenv:body>" +
"<GetLocations xmlns=\"http://clients.mindbodyonline.com/api/0_5\">" +
"<request>" +
"<sourcecredentials>" +
"<sourcename>Siteowner</sourcename>" +
"<password>apitest1234</password>" +
"<siteids> <int>-99</int> </siteids>" +
"</sourcecredentials>" +
"<XMLDetail>Bare</XMLDetail>" +
"<PageSize>10</PageSize>" +
"<CurrentPageIndex>0</CurrentPageIndex>" +
"<Fields>" +
"<string>Locations.Name</string>" +
"<string>Locations.City</string>" +
"</Fields>" +
"</request>" +
"</GetLocations>" +
"</soapenv:Body> </soapenv:envelope>"
let sMessageLength = NSString(format: "%d", soapMessage.characters.count)
myRequest.addValue("text/xml; charset=UTF-8", forHTTPHeaderField: "Content-Type")
myRequest.addValue(sMessageLength as String, forHTTPHeaderField: "Content-Length")
myRequest.HTTPMethod = "POST"
myRequest.addValue("http://clients.mindbodyonline.com/api/0_5/GetLocations", forHTTPHeaderField: "SOAPAction")
myRequest.HTTPBody = soapMessage.dataUsingEncoding(NSUTF8StringEncoding)
sConnection = NSURLConnection(request: myRequest, delegate: self)
我尝试了很多东西,但大多数时候我得到以下错误响应。
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<soap:Fault>
<faultcode>soap:Client</faultcode>
<faultstring>Server was unable to read request. ---> Request format is invalid: Missing required soap:Envelope element.
</faultstring><detail />
</soap:Fault></soap:Body></soap:Envelope>
我无法弄清楚我做错了什么,因为这段代码直接来自他们的网站示例。谢谢您的帮助。