0

過去数日間、Swift で SOAP リクエストを作成しようとして失敗しましたが、誰かが私に成功した応答を得るように指示できるかどうか疑問に思っています。NSURLConnection が非推奨であることは理解していますが、オンラインで見つけることができるすべての例で使用されているため、リクエストを成功させることができるかどうかをテストしたかったのです。

SOAP リクエストは、API Web サイト ( 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. ---&gt; Request format is invalid: Missing required soap:Envelope element.
    </faultstring><detail />
</soap:Fault></soap:Body></soap:Envelope>

このコードはウェブサイトの例から直接外れているため、何が間違っているのかわかりません。ご協力ありがとうございました。

4

2 に答える 2

0

SourceCredentialsキャメルケースを使用するために持っているタグを変更してみてください:

"<SourceCredentials>" +
"<SourceName>Siteowner</SourceName>" +
"<Password>apitest1234</Password>" +
"<SiteIDs> <int>-99</int> </SiteIDs>" +
"</SourceCredentials>" +
于 2016-02-17T21:46:22.287 に答える