【问题标题】:error code 400 while consuming svc service in ios using swift使用swift在ios中使用svc服务时出现错误代码400
【发布时间】:2015-02-13 09:58:32
【问题描述】:

我在 ios 中使用 swift 调用服务,如下所示, 我的服务在 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 和连接中的五次错误并得到错误代码 400 错误。

请帮帮我。

【问题讨论】:

    标签: ios wcf swift svc


    【解决方案1】:

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

    【讨论】:

    • if (connection != nil) { } 解决了我进入 else 块的问题,另请参阅下面解决错误 400 的答案
    【解决方案2】:

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

    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")
        }
    

    【讨论】:

      【解决方案3】:

      最终的解决方案是先完美映射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/'>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 ")
      

      在这里打印结果 } 元素值 = 无 } }

      谢谢然后得到结果

      【讨论】:

        猜你喜欢
        • 2021-01-01
        • 1970-01-01
        • 2020-06-05
        • 1970-01-01
        • 2011-12-06
        • 2013-10-11
        • 1970-01-01
        • 1970-01-01
        • 2023-03-25
        相关资源
        最近更新 更多