Swift-将参数传递到Web服务

时间:2018-12-18 16:41:02

标签: swift web-services web soap

我已经用这个来了。有人可以帮忙吗?我有一个Web服务,它需要一个名为FilteredString的参数,该参数必须传递给一个名为GetProjectRegisterFiltered的Web服务。我尝试了多种变体,但仍然没有任何结果。该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: "&lt;", with: "<")
        Str = Str.replacingOccurrences(of: "&gt;", with: ">")
        Str = Str.replacingOccurrences(of: "&amp;", with: "&")
        let data2 = Data(Str.utf8)
        self.ReadPRData(Data: data2)

        if error != nil
        {
            print("Error: " + error.debugDescription)
        }
    })
    task.resume()
}

1 个答案:

答案 0 :(得分:0)

我忘记感谢每个人的答复。我对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>"