gpt4 book ai didi

ios - 在 SWIFT 中调用 SOAP 服务

转载 作者:行者123 更新时间:2023-11-30 14:07:48 25 4
gpt4 key购买 nike

我尝试在 SWIFT 代码中调用临时转换器服务,但是,我在控制台中得到相同的 println“否”:

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

var is_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>0</Celsius></CelsiusToFahrenheit></soap:Body></soap:Envelope>"



var is_URL: String = "http://www.w3schools.com/webservices/CelsiusToFahrenheit"

var lobj_Request = NSMutableURLRequest(URL: NSURL(string: is_URL)!)
var session = NSURLSession.sharedSession()
var err: NSError?

/*

lobj_Request.HTTPMethod = "POST"
lobj_Request.HTTPBody = is_SoapMessage.dataUsingEncoding(NSUTF8StringEncoding)
lobj_Request.addValue("testest.it", forHTTPHeaderField: "Host")
lobj_Request.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
lobj_Request.addValue(String(count(is_SoapMessage)), forHTTPHeaderField: "Content-Length")
lobj_Request.addValue("http://www.w3schools.com/webservices/CelsiusToFahrenheit", forHTTPHeaderField: "SOAPAction")

*/
var msgLength = String(count(is_SoapMessage))


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

/*
var task = session.dataTaskWithRequest(lobj_Request, completionHandler: {data, response, error -> Void in
println("Response: \(response)")
var strData = NSString(data: data, encoding: NSUTF8StringEncoding)
println("Body: \(strData)")




if error != nil
{
println("Error: " + error.description)
}
else
{
println("OKAY")
}
})

task.resume()

*/

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

if (connection == true) {
var mutableData : Void = NSMutableData.initialize()
println("OKAY")
}
else
{
println("NO")
}

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


}

我看不出错误在哪里,我没有出现错误的行。我试图理解为什么我无法收到"is"消息,有人可以帮助我吗?

最佳答案

connection 不是 Bool 类型,因此 connection == true 总是会得到 falso。这就像问香蕉是不是苹果一样。显而易见的答案永远是“假”

要检查连接对象是否已分配,您应该检查它是否不为零,因此 if 应该是 if connection != nil 这并不能确保您“调用正常”,例如你在评论中说。它只会确保连接对象已成功创建,并且您应该在调用连接对象的 start 方法之前检查它。 (并且您实际上不需要调用 start 方法,因为您将 true 作为立即启动参数)

if let connection = NSURLConnection(request: lobj_Request, delegate: self, startImmediately: true) {
connection.start() //You don't need it because you set the startImmediately param to true
//whatever you want to do the connection
}

关于ios - 在 SWIFT 中调用 SOAP 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32141286/

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