【问题标题】:Xml request header with root element in swiftswift中带有根元素的XML请求标头
【发布时间】:2023-03-09 14:32:01
【问题描述】:

我一直在尝试使用Alamofire 发出 xml 请求。但是,我收到了<soap:Text xml:lang="en">Server was unable to process request. ---> Root element is missing.</soap:Text> 错误。实际上,这是我在 ios 中对 xml 请求的第一次体验。我怎样才能毫无问题地提出这个请求。这是我的代码;

Alamofire.request(.POST, "http://example.com/WS.asmx?op=AuthenticateUser", parameters: nil, encoding: .PropertyList(.XMLFormat_v1_0, 0), headers: ["Username": "username", "Password": "password" , "AuthenticatedToken" : "123123"])
                    .responsePropertyList { response in 
    let xml = SWXMLHash.parse(response.data!)
                        print(xml) 
}

我做错了什么?你能帮我吗?

感谢您的回答

最好的问候

【问题讨论】:

    标签: ios xml swift soap alamofire


    【解决方案1】:

    试试看

    Alamofire.request(.POST, "http://example.com/WS.asmx?op=AuthenticateUser", 
        parameters: nil, 
        // Try ->
        encoding: .Custom({
           (convertible, params) in
               let mutableRequest = convertible.URLRequest.copy() as! NSMutableURLRequest
               mutableRequest.HTTPBody =       
                    "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
                    <SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns1=\"urn:sap-com:document:sap:rfc:functions\"><SOAP-ENV:Body>
                      <soap:Text xml:lang="en">Server was unable to process request. ---> Root element is missing.</soap:Text>
                     </SOAP-ENV:Body></SOAP-ENV:Envelope>".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
    
            return (mutableRequest, nil)}),
         headers: 
            ["Username": "username",
             "Password": "password" ,
             "AuthenticatedToken" : "123123"])
         .responsePropertyList { response in 
               let xml = SWXMLHash.parse(response.data!)
                        print(xml) 
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-29
      • 2017-08-31
      • 1970-01-01
      • 1970-01-01
      • 2018-09-10
      相关资源
      最近更新 更多