gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 09:13:35 24 4
gpt4 key购买 nike

我是 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com