【问题标题】:How to consume WCF .svc service in iOS 8 using swift?如何使用 swift 在 iOS 8 中使用 WCF .svc 服务?
【发布时间】:2015-02-12 09:41:36
【问题描述】:

我是 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()
    }

请帮帮我。

【问题讨论】:

    标签: ios web-services wcf swift xcode6


    【解决方案1】:
        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")
        }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-14
      • 1970-01-01
      • 1970-01-01
      • 2011-09-11
      相关资源
      最近更新 更多