使用swift在ios中使用svc服务时出现错误代码400

时间:2015-02-13 09:58:33

标签: ios wcf swift svc

我正在使用swift in ios打电话给服务, 我的服务与Android版本完美配合,但当我尝试使其与ios一起使用时,它总是返回错误代码400

下面是ios版本的代码

 var soapMessage = "<?xml version='1.0' encoding='UTF-16'?><soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'><soap:Body><GetAirports xmlns='http://groundspan.com/groundspan'/></soap:Body></soap:Envelope>"

   // var urlString = "http://example.com/myeservice.svc"
    var urlString = "http://example.com/myservice.svc?"
    var nameSpace = "http://example.com/service"
    var methodNam = "GetAirports"
    var msgLength = String(countElements(soapMessage))
    var url = NSURL(string: urlString)!
    println("Url : \(url)")
    var theRequest = NSMutableURLRequest(URL: url)
    theRequest.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
    theRequest.addValue(msgLength, forHTTPHeaderField: "Content-Length")
    theRequest.addValue("http://example.com/service/IGroundSpanProcessorService/\(methodNam)", forHTTPHeaderField: "SOAPAction")
    theRequest.HTTPMethod = "POST"
    theRequest.HTTPBody = soapMessage.dataUsingEncoding(NSUTF16StringEncoding, allowLossyConversion: false)


    var connection = NSURLConnection(request: theRequest, delegate: self, startImmediately: true)
    connection?.start()

    if (connection == true) {
        var mutableData : Void = NSMutableData.initialize()
    }else{
        println("Error in connection")
    }

它总是进入else连接块和连接中的五个错误&amp;也会收到错误代码400错误。

请帮帮我。

3 个答案:

答案 0 :(得分:0)

我不确定为什么它会进入“连接中的错误”块但是几天前我不得不处理错误400错误请求(我想它意味着一个)。 首先你应该使用像Wireshark这样的嗅探器(我已经在这里看到Fiddler推荐了很多)来检查你发送的消息以及从服务器获得的消息。这可能会帮助您查看是否收到了WCF无法自动解析的错误。

答案 1 :(得分:0)

以下是我自己的问题的答案

var soapMessage = "<?xml version='1.0' encoding='UTF-8'?><soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'><soap:Body><GetAirports xmlns='http://example.com/example'/></soap:Body></soap:Envelope>"

    var urlString = "http://example.com/GroundSpanProcessorService.svc"

    var msgLength = String(countElements(soapMessage))
    var url = NSURL(string: urlString)!

    var theRequest = NSMutableURLRequest(URL: url)
    theRequest.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
    theRequest.addValue("http://example.com/example/IGroundSpanProcessorService/GetAirports", forHTTPHeaderField: "Soapaction")
    theRequest.addValue(msgLength, forHTTPHeaderField: "Content-Length")

    theRequest.HTTPMethod = "POST"
    theRequest.HTTPBody = soapMessage.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)


    var connection = NSURLConnection(request: theRequest, delegate: self,startImmediately: true)
    connection?.start()

    if (connection == true) {
        println("Connection success")
        var mutableData : Void = NSMutableData.initialize()
    }else{
        println("Error in connection")
    }

答案 2 :(得分:0)

最终解决方案首先完美映射soap信封,然后解析wsdl

var is_SoapMessage:String =“http://www.w3.org/2001/XMLSchema-instance'xmlns:xsd ='http://www.w3.org/2001/XMLSchema'xmlns:soap ='http://schemas.xmlsoap.org/soap/envelope/'&gt; http ://tempuri.org/'>“

var myelement :String = ""
var elementValue: String?
var success = false
var array_string = [String]()


// on click 


    var is_URL: String =  "http://assetwebservice.sudesi.in/service.svc"

    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("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://tempuri.org/IService/BindCategory", forHTTPHeaderField: "SOAPAction")

    var task = session.dataTaskWithRequest(lobj_Request, completionHandler: {data, response, error -> Void in



    var strData = NSString(data: data, encoding: NSUTF8StringEncoding)

      //  println("Body:---- \(strData)")
        let parser = NSXMLParser(data: data)
        parser.delegate = self
        parser.parse()

                          // Just in case, if there's an error, report it. (We don't want
        if error != nil
        {
            println("Error: " + error.description)
        }

    })
    task.resume()

}


func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [NSObject : AnyObject]) {

    println("first case ")
    println(elementName)

    if elementName == "GetDataResponse" {
        elementValue = String()
    }

}

func parser(parser: NSXMLParser, foundCharacters string: String?) {


   println("second  case ")
    array_string.append(string!)
    anstext.text = string


   println(string)

    if elementValue != nil {

        println("come in this part ")
        elementValue! += string!
        array_string.append(elementValue!)


    }
    println((array_string.count))




}

func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?
    ) {

        println("third case ")


    if elementName == "GetDataResponse" {

        if elementValue == "true" {
            success = true
                      // println("this is right answer ")

在这里打印结果             }             elementValue = nil         }     }

谢谢然后得到结果