【问题标题】:Error Code 500 with Soap Request in SwiftSwift 中带有 Soap 请求的错误代码 500
【发布时间】:2017-08-28 14:46:47
【问题描述】:

我正在尝试执行一个调用加密函数并返回一个值的肥皂请求。当我对邮递员提出请求时,它可以工作。但是,我从邮递员那里得到了代码,但它返回错误代码 500。我认为问题可能出在 postData 中,我在双引号之前添加了 \。 代码:

        let headers = [
        "content-type": "text/xml",
        "cache-control": "no-cache",
        "postman-token": "4d266611-ea55-0c8a-7879-eac8bf387ed0"
    ]

    let postData = NSData(data: "<?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><Encrypt xmlns=\"http://tempuri.org/\"><request>RequestIsValid29:01:2015 16:31</request> </Encrypt></soap:Body> </soap:Envelope>".data(using: String.Encoding.utf8)!)

    let request = NSMutableURLRequest(url: NSURL(string: "http://mobileexam.veripark.com/mobileforeks/service.asmx")! as URL,
                                      cachePolicy: .useProtocolCachePolicy,
                                      timeoutInterval: 10.0)
    request.httpMethod = "POST"
    request.allHTTPHeaderFields = headers
    request.httpBody = postData as Data

    let session = URLSession.shared
    let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
        if (error != nil) {
            print(error)
        } else {
            let httpResponse = response as? HTTPURLResponse
            print(httpResponse)
        }
    })

    dataTask.resume()

错误代码:

    <NSHTTPURLResponse: 0x608000222300> { URL: http://mobileexam.veripark.com/mobileforeks/service.asmx } { status code: 500, headers {
    "Cache-Control" = private;
    Connection = "Keep-Alive";
    "Content-Length" = 1802;
    "Content-Type" = "application/soap+xml; charset=utf-8";
    Date = "Mon, 28 Aug 2017 14:57:47 GMT";
    Server = "Microsoft-IIS/7.5";
    "X-AspNet-Version" = "4.0.30319";
    "X-Powered-By" = "ASP.NET";
} })

【问题讨论】:

  • 设置 URLRequest 的“Content-Type”:stackoverflow.com/questions/2965587/…?
  • 我改了代码,你能查一下吗?
  • @iOSDeveloper 是的。确切的问题是什么?
  • @iOSDeveloper 我在答案中添加了工作代码。请检查一下。

标签: ios swift web-services soap request


【解决方案1】:

正如我在问题中提到的。问题是字符串中的 xml 部分。我在 " 之前添加 \ 并且在需要换行时添加 \r。下面是 XML 的正确字符串版本。

"\rhttp://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\r\rhttp:// tempuri.org/\">\rRequestIsValid29:01:2015 16:31 \r \r"

所以问题的最终工作代码是:

let poststring = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\">\r<soapenv:Header/> <soapenv:Body>\r<tem:GetForexStocksandIndexesInfo> <tem:request>\r<tem:IsIPAD>true</tem:IsIPAD>\r<tem:DeviceID>test</tem:DeviceID>\r<tem:DeviceType>ipad</tem:DeviceType>\r<tem:RequestKey>\(self.encrypt_code!)</tem:RequestKey>\r<tem:RequestedSymbol>\(name)</tem:RequestedSymbol>\r<tem:Period>Month</tem:Period> </tem:request>\r</tem:GetForexStocksandIndexesInfo> </soapenv:Body>\r</soapenv:Envelope>"
let postData = NSData(data: poststring.data(using: String.Encoding.utf8)!)
let request = NSMutableURLRequest(url: NSURL(string: "http://mobileexam.veripark.com/mobileforeks/service.asmx")! as URL,
                                      cachePolicy: .useProtocolCachePolicy,
                                      timeoutInterval: 10.0)

【讨论】:

    【解决方案2】:

    你试过了吗?

    [lobj_Request setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-19
    • 2015-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-17
    相关资源
    最近更新 更多