ios - 如何使用 swift 在 iOS 8 中使用 WCF .svc 服务?

标签 ios web-services wcf swift xcode6

我是 iOS 初学者,我想使用 swift 从我的应用程序调用 WCF .svc 服务。

我的服务返回 soap 数据作为响应。

我已经使用以下代码使用了 asmx 服务,是否可以使用相同的代码来调用 wcf .svc 服务,或者我确实需要更改代码。

 var celcius = txtCelsius.text

    var soapMessage = "<?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.w3schools.com/webservices/'><Celsius>\(celcius)</Celsius></CelsiusToFahrenheit></soap:Body></soap:Envelope>"


    var urlString = "http://www.w3schools.com/webservices/tempconvert.asmx"



    var url:NSURL = NSURL(string: urlString)!

    var theRequest = NSMutableURLRequest(URL: url)

    var msgLength = String(countElements(soapMessage))

    theRequest.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
    theRequest.addValue(msgLength, forHTTPHeaderField: "Content-Length")
    theRequest.HTTPMethod = "POST"
    theRequest.HTTPBody = soapMessage.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) // or false

    var connection = NSURLConnection(request: theRequest, delegate: self, startImmediately: true)
    connection?.start()

    if (connection == true) {
        var mutableData : Void = NSMutableData.initialize()
    }

请帮帮我。

最佳答案

    var urlString = "http://assetwebservice.sudesi.in/service.svc"

    var msgLength = String(count(soapMessage))
    var url = NSURL(string: urlString)!

    var theRequest = NSMutableURLRequest(URL: url)
    theRequest.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
    theRequest.addValue("http://tempuri.org/IService/BindCategory", forHTTPHeaderField: "Soapaction")
    theRequest.addValue(msgLength, forHTTPHeaderField: "Content-Length")

    theRequest.HTTPMethod = "POST"
    theRequest.HTTPBody = soapMessage.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)


    var connection = NSURLConnection(request: theRequest, delegate: self,startImmediately: true)
    connection?.start()

    if (connection == true) {
        println("Connection success")
        var mutableData : Void = NSMutableData.initialize()
    }else{
        println("Error in connection")
    }

您好请检查它始终处于其他状态意味着连接错误 得不到结果 谢谢

关于ios - 如何使用 swift 在 iOS 8 中使用 WCF .svc 服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28474034/

相关文章:

.NET 桌面应用程序记住登录到 web 服务

类定义之外的 WCF 序列化信息

c# - 如何从 WCF 服务返回 LINQ to SQL 表实体?

c# - 使用 EF 数据库设置 WCF 服务的正确方法是什么?

ios - 分配 block 属性时发送到实例的无法识别的选择器

ios - AVAudio 的最佳麦克风录音设置是什么?

java - 如何为从 Axis2 Webservice 创建的客户端对象定义 toString() 方法

ios swift 显示/隐藏textView单元格中的元素并清除其空间

ios - 将图像添加到按钮时,如何禁用UIButton的标题向右移动?

java - 修改SoapHandler中的Soap header