当前位置: 首页 > 知识库问答 >
问题:

swift web服务通信

危文乐
2023-03-14

我正试图构建我的第一个swift应用程序,当我试图使用soap web服务时遇到了麻烦…方法如下:

           var userName:NSString = "abc"
           var password:NSString = "def"


            var soapMsg = "<?xml version='1.0' encoding='UTF-8'?><soap:Envelope xmlns:xsi='http://87.106.20.80:8080/axis2/services/app2beeService?wsdl' xmlns:xsd='http://localhost:8080/beeSmartWS' xmlns:soap='http:/bee.myservice.com/userLogin'><soap:Body><userLogin xmlns 'http://87.106.20.80:8080/axis2/services/app2beeService?wsdl/userLogin><:userId>" + userName + "</userId><password>" + password + "</password></userLogin></soap:Body></soap:Envelope>"




                //let soapAction = "http://bee.myservice.com/userLogin"
                //let urlString = "http://bee.myservice.com/"




            let urlString: String = "http://bee.myservice.com/"
            var url: NSURL = NSURL(string: urlString)!
            //var request: NSURLRequest = NSURLRequest(URL: url)
            var request = NSMutableURLRequest(URL: url)
            var msgLength = String(countElements(soapMsg))

                request.HTTPMethod = "POST"
                request.HTTPBody = soapMsg.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
                request.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
                request.addValue(msgLength, forHTTPHeaderField: "Content-Length")
                request.addValue("http://bee.myservice.com/userLogin", forHTTPHeaderField: "Action")



            var connection: NSURLConnection = NSURLConnection(request: request, delegate: self, startImmediately: true)!
            connection.start()
            println(request)

            var response: NSURLResponse?

            var data = NSURLConnection.sendSynchronousRequest(request, returningResponse: &response, error: nil) as NSData?

            if let httpResponse = response as? NSHTTPURLResponse {
                println("error \(httpResponse.statusCode)")
                println(response)
            }

            }

当我发送此请求时,没有得到任何响应

Web服务详细信息:Web服务类型:Axis2(基于Java和SOAP)Web服务WSDL链接:http://87.106.20.80:8080/Axis2/services/app2beeservice?WSDL Target namespace:http://bee.myservice.com SOAP action:Add/userlogin to Target namespace

共有1个答案

吕成业
2023-03-14

我建议使用Alamofire(https://github.com/Alamofire/Alamofire)。您的代码如下所示:

var request = NSMutableURLRequest(URL: NSURL(string: "http://bee.myservice.com"))
let msgLength = String(countElements(soapMsg))
let data = soapMsg.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)

request.HTTPMethod = "POST"
request.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.addValue(msgLength, forHTTPHeaderField: "Content-Length")
request.addValue("http://bee.myservice.com/userLogin", forHTTPHeaderField: "Action")

Alamofire.upload( request, data )
    .response { (request, response, data, error) in
        // do stuff here
    }

很多人都在使用Alamofire,操作是异步执行的(但由于闭包,操作非常容易)。

 类似资料:
  • 我实际上在研究微服务,我面临一个问题。 上下文 我正在开发两个微服务: 用户管理,基于spring,使用MySQL数据库 计划管理,基于ASP.NET与SQL Server数据库。此服务的唯一访问点是列出一些RESTFULendpoint的API,如 计费管理,基于MongoDB的node.js。 问题 > 我该怎么做才能只允许通过用户服务访问规划信息,而不耦合这两个服务?知道以后可以从其他地方访

  • 在Openshift中,我有两个需要彼此通信的服务,我想知道您是否知道一种方法来做到这一点,而不公开这两个微服务。 示例: 服务A需要在服务B上请求一个endpoint,为此,我正在使用DNS,但要做到这一点,我需要公开服务B。 有什么方法可以在不公开服务B的情况下完成此场景? 要公开服务A,我运行命令:“oc expose Service/servicea”

  • 在 Ark 服务机制 中,我们详细介绍了如何引用和发布插件服务,主要是解决 Plugin 和 Biz 的通信问题;为了解决 Biz 之间的通信问题,SOFAArk 引入了 SOFABoot 提供的 SofaService/SofaReference 编程界面;下面介绍其使用方法。 引入依赖 引入 runtime-sofa-boot-plugin 依赖,如果应用基于 Spring Boot 1.x

  • 2.2 服务通信 服务通信也是ROS中一种极其常用的通信模式,服务通信是基于请求响应模式的,是一种应答机制。也即: 一个节点A向另一个节点B发送请求,B接收处理请求并产生响应结果返回给A。比如如下场景: 机器人巡逻过程中,控制系统分析传感器数据发现可疑物体或人... 此时需要拍摄照片并留存。 在上述场景中,就使用到了服务通信。 一个节点需要向相机节点发送拍照请求,相机节点处理请求,并返回处理结果

  • 通用服务 网页生命周期 1.BdHiJs.appnative.webview.load 打开新窗口加载当前应用页面(用于应用内页面的跳转,具有本应用的权限、JS SDK能力) 如果加载的URL为非应用可信域名,则该函数不做任何操作(console中应提示error信息以告知开发者错误原因:{URL}包含不可信域名)。 旧应用兼容:旧应用因未设置可信域名,则按原有逻辑,不做可信域名校验。 B