ios - 在 iOS 中使用 SOAP Web 服务

标签 ios swift soap

我正在尝试查找有关如何在 iOS 中使用 Swift 使用 SOAP 服务的信息。我习惯于使用 REST Web 服务,但在这种情况下我没有那么奢侈。我有wsdl。对我需要使用的方法之一的请求如下所示:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:fet="http://www.kyfb.com/ebuz/FetcheBuzAccountInfo/">
<soapenv:Header>
      <wsse:Security soapenv:actor="http://schemas.xmlsoap.org/soap/actor/next" soapenv:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
         <wsse:UsernameToken wsu:Id="SecurityToken-6138db82-5a4c-4bf7-915f-af7a10d9ae96" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
            <wsse:Username>ebusdoc</wsse:Username>
            <wsse:Password>Ebusnes1</wsse:Password>
         </wsse:UsernameToken>
      </wsse:Security>
   </soapenv:Header>

   <soapenv:Body>
      <fet:fetchInformationForGuest>
         <!--Optional:-->
         <FetchInformationForGuestReq>
         <entityNbr>0001354648</entityNbr>
            <entityType>MBR</entityType>
         </FetchInformationForGuestReq>
      </fet:fetchInformationForGuest>
   </soapenv:Body>
</soapenv:Envelope>

我知道如何在 Swift 中实际解析 XML,但我不确定如何使用此请求获取响应。非常感谢这里的任何帮助。

最佳答案

open class HTTPSOAPClient {

open static let sharedInstance = HTTPSOAPClient()// Singleton instance yo use throughout the app
fileprivate init() {} //This prevents others from using the default '()' initializer for this class.


public func sendRequest(_ method: Alamofire.HTTPMethod,url: String, parameters: String, header: [String: String]?,completionHandler: @escaping (NSString)-> Void)  {
    var url = url
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    let str:String = "<?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>" + parameters + "</soap:Body></soap:Envelope>"
    let postData = NSData(data: str.data(using: String.Encoding.utf8)!)
    var request = URLRequest(url: URL(string:url)!)
    request.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")

    request.httpMethod = "POST"
    request.httpBody = postData as Data
    let session = URLSession.shared
    let task =  session.dataTask(with: request) { (data, resp, error) in

        guard error == nil && data != nil else{

            print("connection error or data is nill")

            return
        }

        if resp != nil {



        }

        let mutableData : Void = NSMutableData.initialize()
        print(mutableData)


        let dataString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
        completionHandler(dataString!)

        print(dataString ?? "")
     }

    task.resume()
  }
}

如下调用sendRequest

HTTPSOAPClient.sharedInstance.sendRequest(<#T##method: HTTPMethod##HTTPMethod#>, url: <#T##String#>, parameters: <#T##String#>, header: <#T##[String : String]?#>, completionHandler: <#T##(NSString) -> Void#>)

关于ios - 在 iOS 中使用 SOAP Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48906166/

相关文章:

android - Flutter中的"Error: Member not found: ' packageRoot ' "

ios - 将 NSData 转换为 NSDictionary

ios - 在 NSString stringWithFormat 上崩溃

ios - 使用 UIActivityViewController 为每个事件提供不同的共享项

swift - Xcode 无法在类 UICollectionReusableView 的助理编辑器中打开正确的类

ios - 如何突出显示文本(在 UILabel 中)并将其转换为 IOS8 swift 中的可点击链接/按钮

php - 如何在 soapServer 响应中返回自定义 XML 响应?

c# - 使用 C# 读取 Soap 消息

ios - 如何修复 “swift package generate-xcodeproj” 导致 list 解析错误?

java - 如何用多个wsdl文件生成一个services.xml