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

时间:2015-02-12 09:41:37

标签: ios web-services wcf swift xcode6

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

我的服务回复了肥皂数据。

我使用以下代码消耗了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()
    }

请帮帮我。

1 个答案:

答案 0 :(得分:0)

    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")
    }

嗨请检查它始终处于其他状态意味着连接错误 无法获得结果 感谢