我正在尝试连接 iOS 中的网络服务,但遇到了很多问题。我在这里找到了一个练习网络服务 -http://www.w3schools.com/webservices/tempconvert.asmx?op=CelsiusToFahrenheit http://www.w3schools.com/webservices/tempconvert.asmx?op=CelsiusToFahrenheit
到目前为止我已经写了这段代码
let soapMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<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/\">\n"
"<soap:Body>\n"
"<CelsiusToFahrenheit xmlns=\"http://www.w3schools.com/webservices/\">\n"
"<Celsius>50</Celsius>\n"
"</CelsiusToFahrenheit>\n"
"</soap:Body>\n"
"</soap:Envelope>\n"
let url = NSURL(string: "http://w3schools.com/webservices/tempconvert.asmx")!
var request = NSMutableURLRequest(URL: url)
let messageLength = "\(soapMessage.utf16Count)"
NSLog("\(messageLength)")
request.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.addValue("http://www.w3schools.com/webservices/CelsiusToFahrenheit", forHTTPHeaderField: "SOAPAction")
request.addValue(messageLength, forHTTPHeaderField: "Content-Length")
request.HTTPMethod = "POST"
request.HTTPBody = soapMessage.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
var urlSession: NSURLSession = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration(), delegate: self, delegateQueue: NSOperationQueue.mainQueue())
它可以连接,但我的响应返回为 text/html 而不是 text/xml。我的问题是为什么它返回错误?我还使用 NSURLSessionDataTask 或 NSURLSessionDownloadTask 吗?
对于你的情况,我认为它是 NSURLSessionDataTask。基于这个网站:http://www.kaleidosblog.com/nsurlsession-in-swift-get-and-post-data http://www.kaleidosblog.com/nsurlsession-in-swift-get-and-post-data
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)