首页 文章

如何从iPhone访问SOAP服务

提问于
浏览
223

我打算为iPhone开发一个应用程序,该应用程序必须访问几个SOAP服务 . 在iPhone SDK中进行一些基本检查时,我无法找到任何访问SOAP服务的支持,一点谷歌搜索得出的结论是iPhone SDK中不支持SOAP .

因此,如果我想构建该应用程序,我需要提出一种从iPhone访问SOAP服务的方法 . 什么是最好的方法?任何最佳做法?有人已经使用iPhone SDK中存在的功能来编写库以访问SOAP服务吗?

(由于我需要访问的服务是由另一方公开的,并且它们只将它公开为SOAP,遗憾的是不能选择切换到另一种类型的接口(例如基于REST的API) .

格罗

7 回答

  • 52

    我的解决方案是让代理服务器接受REST,发出SOAP请求,并使用PHP返回结果 .

    实施时间:15-30分钟 .

    不是最优雅,但坚实 .

  • 4

    一句话:不要 .

    好吧,显然这不是一个真正的答案 . 但是仍然应该不惜一切代价避免使用SOAP . ;-)是否可以在iPhone和Web服务之间添加代理服务器?也许将REST转换为SOAP的东西?

    您可以尝试CSOAP,这是一个依赖于libxml2(包含在iPhone SDK中)的SOAP库 .

    我为OSX编写了自己的SOAP framework . 但是它没有被主动维护,并且需要一些时间来移植到iPhone(你需要用TouchXML取代NSXML作为开始)

  • 7

    我've historically rolled my own access at a low level (XML generation and parsing) to deal with the occasional need to do SOAP style requests from Objective-C. That said, there'是一个名为SOAPClient(soapclient)的库,它是开源的(BSD许可的),可以在谷歌代码(mac-soapclient)上获得,可能会引起关注 .

    我不会证明它的能力或效果,因为我从未使用它或不得不使用它的API,但它可用,并可能根据您的需要为您提供快速解决方案 .

    Apple曾经有一个名为WS-MakeStubs的非常破碎的实用工具 . 我不会在iPhone上提供't think it',但您可能也对一个旨在取代它的开源库感兴趣 - 代码生成Objective-C以与SOAP客户端进行交互 . 再一次,我没有在我的笔记中标记出来:wsdl2objc

  • 2

    您可以使用我找到的工具进行连接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];
    
  • 0

    这是一个 swift 4 示例代码,它使用SOAP服务格式执行API调用 .

    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()
        }
    
  • 2

    看看here this link及其路线图 . 他们在路上有RO | C,可以连接到他们的Web服务,可能包括SOAP(我使用的VCL版本肯定包含它) .

  • 0

    查看gsoap,其中包含ios_plugin下载包中的两个iOS示例 . 该工具将WSDL转换为SOAP和XML REST的代码 .

相关问题