小编典典

使用SOAP的iOS Swift Call Web服务

swift

我仔细研究了如何在Internet上快速通过SOAP表单调用Web服务的答案,并找到了一些答案。我试图实现在这些答案中找到的代码,但是不断获得http
400状态代码。我试图弄清楚我们在做什么错。

我将问题简化为视图控制器中的几行代码,如下所示,当按下UI上的按钮时,将调用该代码。我尝试调用的Web服务可以在http://www.cgsapi.com/CGSWebService.asmx上找到。

(要查看WSDL文件,请将?wsdl附加到URL的末尾。)

import UIKit

class ViewController: UIViewController {
var is_SoapMessage: String = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:cgs=\"http://www.cgsapi.com/\"><soapenv:Header/><soapenv:Body><cgs:GetSystemStatus/></soapenv:Body></soapenv:Envelope>"

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func btnClicked(sender: AnyObject)
{
    var is_URL: String = "http://www.cgsapi.com/CGSWebService.asmx"

    var lobj_Request = NSMutableURLRequest(URL: NSURL(string: is_URL)!)
    var session = NSURLSession.sharedSession()
    var err: NSError?

    lobj_Request.HTTPMethod = "POST"
    lobj_Request.addValue("www.cgsapi.com", forHTTPHeaderField: "Host")
    lobj_Request.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
    //lobj_Request.addValue(String(count(is_SoapMessage)), forHTTPHeaderField: "Content-Length")
    lobj_Request.addValue("223", forHTTPHeaderField: "Content-Length")
    lobj_Request.addValue("http://www.cgsapi.com/GetSystemStatus", forHTTPHeaderField: "SOAPAction")

    var task = session.dataTaskWithRequest(lobj_Request, completionHandler: {data, response, error -> Void in
        println("Response: \(response)")
        var strData = NSString(data: data, encoding: NSUTF8StringEncoding)
        println("Body: \(strData)")

        if error != nil
        {
            println("Error: " + error.description)
        }

    })
    task.resume()
}

}

知道为什么我打电话时会获得http 400状态吗?


阅读 271

收藏
2020-07-07

共1个答案

小编典典

所以我很傻。最主要的是,我错过了将消息正文设置为SOAP请求的过程。我更新后的更正代码如下:

//
//  ViewController.swift
//  TestWebServiceSoap
//
//  Created by George M. Ceaser Jr on 6/2/15.
//  Copyright (c) 2015 George M. Ceaser Jr. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
var is_SoapMessage: String = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:cgs=\"http://www.cgsapi.com/\"><soapenv:Header/><soapenv:Body><cgs:GetSystemStatus/></soapenv:Body></soapenv:Envelope>"

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func btnClicked(sender: AnyObject)
{
    var is_URL: String = "http://www.cgsapi.com/CGSWebService.asmx"

    var lobj_Request = NSMutableURLRequest(URL: NSURL(string: is_URL)!)
    var session = NSURLSession.sharedSession()
    var err: NSError?

    lobj_Request.HTTPMethod = "POST"
    lobj_Request.HTTPBody = is_SoapMessage.dataUsingEncoding(NSUTF8StringEncoding)
    lobj_Request.addValue("www.cgsapi.com", forHTTPHeaderField: "Host")
    lobj_Request.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
    lobj_Request.addValue(String(count(is_SoapMessage)), forHTTPHeaderField: "Content-Length")
    //lobj_Request.addValue("223", forHTTPHeaderField: "Content-Length")
    lobj_Request.addValue("http://www.cgsapi.com/GetSystemStatus", forHTTPHeaderField: "SOAPAction")

    var task = session.dataTaskWithRequest(lobj_Request, completionHandler: {data, response, error -> Void in
        println("Response: \(response)")
        var strData = NSString(data: data, encoding: NSUTF8StringEncoding)
        println("Body: \(strData)")

        if error != nil
        {
            println("Error: " + error.description)
        }

    })
    task.resume()
}

}
2020-07-07