我已经陷入困境了。有人可以帮忙吗?我有一个 Web 服务,需要一个名为 FilteredString 的参数,该参数必须传递给名为 GetProjectRegisterFiltered 的 Web 服务。我尝试了多种变体,但仍然没有得到任何结果。该网络服务与另一个有效的网络服务相同,我只是向其中添加了一个参数。
func GetFilteredRecords(){
let is_SoapMessage: String = "<?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></soap:Body></soap:Envelope>"
let URL: String = "http://192.168.1.208:8080/Service.asmx"
let WebRequ = NSMutableURLRequest(url: NSURL(string: URL)! as URL)
WebRequ.httpMethod = "POST"
WebRequ.httpBody = is_SoapMessage.data(using: String.Encoding.utf8)
WebRequ.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
WebRequ.addValue(String(is_SoapMessage), forHTTPHeaderField: "Content-Length")
WebRequ.addValue("myServices/GetProjectRegisterFiltered", forHTTPHeaderField: "SOAPAction")
var Str: String = ""
let task = session.dataTask(with: WebRequ as URLRequest, completionHandler: {data, response, error -> Void in
let strData = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
Str = String(strData!) as String
print(strData!)
Str = Str.replacingOccurrences(of: "<", with: "<")
Str = Str.replacingOccurrences(of: ">", with: ">")
Str = Str.replacingOccurrences(of: "&", with: "&")
let data2 = Data(Str.utf8)
self.ReadPRData(Data: data2)
if error != nil
{
print("Error: " + error.debugDescription)
}
})
task.resume()
}
最佳答案
我忘了感谢大家的回复。我没有完全理解 SOAP 请求的工作原理。不过我现在也这么做了。我不知道我只需要将变量添加到 SOAP 请求字符串,如下所示:
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><GetDiaryByDay xmlns=\"MyWebWebsite\"><Username>\(Username)</Username><Password>\(Password)</Password><Day>\(Day)</Day><Month>\(Month)</Month><Year>\(Year)</Year></GetDiaryByDay></soap:Body></soap:Envelope>"
关于Swift - 将参数传递给 Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53837525/