title: IOS Swift Soap Message with Ampersand in value
body: <p>I have the following code to call web service from my IOS Swift application.  (The code is execute when a button on the page is pressed.)  </p>
  
  <p>The challenge is with the value for Hours.  When the ampersand is in the value, I get an http error code 400.  When I take out the ampersand, the call to the web service works fine.  I have tried various ways to encode the value as well as the whole soap message but none of them worked.  I tried the %26 and @26; as a replacement for the ampersand and while the web service call inserted that data into the database, on the server %26 or %26; got inserted into the database instead of an ampersand.  I also tried using stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding) on the entire SOAP message as well as just the hours value and both still failed.  Any idea what to do with the ampersand so that it can be sent as part of the SOAP message and is automatically back to an ampersand when it is received on the server?</p>
  
  <pre><code>//
  //  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 = "&lt;soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:cgs=\"http://www.cgsapi.com/\"&gt;&lt;soapenv:Header/&gt;&lt;soapenv:Body&gt;&lt;cgs:Add&gt;&lt;cgs:Language_Code&gt;EN&lt;/cgs:Language_Code&gt;&lt;cgs:Country_Code&gt;USA&lt;/cgs:Country_Code&gt;&lt;cgs:State_Province_Code&gt;USA-DC&lt;/cgs:State_Province_Code&gt;&lt;cgs:City_ID&gt;1&lt;/cgs:City_ID&gt;&lt;cgs:Location_ID&gt;14&lt;/cgs:Location_ID&gt;&lt;cgs:Name&gt;Green Lantern / Tool Shed&lt;/cgs:Name&gt;&lt;cgs:Additional_Info&gt;null&lt;/cgs:Additional_Info&gt;&lt;cgs:Hours&gt;{hours}&lt;/cgs:Hours&gt;&lt;cgs:Address_Line1&gt;1335 Green Court NW&lt;/cgs:Address_Line1&gt;&lt;cgs:Address_Line2&gt;In alley behind building at 1335 L Street NW&lt;/cgs:Address_Line2&gt;&lt;cgs:Postal_Code&gt;20005&lt;/cgs:Postal_Code&gt;&lt;cgs:Phone&gt;202.347.4533&lt;/cgs:Phone&gt;&lt;cgs:Email_Address&gt;null&lt;/cgs:Email_Address&gt;&lt;cgs:Contact_Email_Address&gt;null&lt;/cgs:Contact_Email_Address&gt;&lt;cgs:WebSite&gt;http://www.greenlanterndc.com&lt;/cgs:WebSite&gt;&lt;cgs:Open_Status&gt;true&lt;/cgs:Open_Status&gt;&lt;cgs:Attribute_Code_List&gt;B,G,LL&lt;/cgs:Attribute_Code_List&gt;&lt;cgs:Submitter_Email&gt;null&lt;/cgs:Submitter_Email&gt;&lt;cgs:Latitude&gt;38.90447617&lt;/cgs:Latitude&gt;&lt;cgs:Longitude&gt;38.90447617&lt;/cgs:Longitude&gt;&lt;cgs:Other_Change&gt;null&lt;/cgs:Other_Change&gt;&lt;cgs:NewCityLatitude&gt;0.0&lt;/cgs:NewCityLatitude&gt;&lt;cgs:NewCityLongitude&gt;0.0&lt;/cgs:NewCityLongitude&gt;&lt;cgs:NewCityName&gt;null&lt;/cgs:NewCityName&gt;&lt;/cgs:Add&gt;&lt;/soapenv:Body&gt;&lt;/soapenv:Envelope&gt;"
  
  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?
  
      var ls_Hours: String = "16-2 Fri &amp; Sat-3 Sun 13-2"
  
      is_SoapMessage =  is_SoapMessage.stringByReplacingOccurrencesOfString("{hours}", withString: ls_Hours, options: NSStringCompareOptions.LiteralSearch, range: nil)
  
      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/Add", forHTTPHeaderField: "SOAPAction")
  
      var task = session.dataTaskWithRequest(lobj_Request, completionHandler: {data, response, error -&gt; Void in
          println("Response: \(response)")
          var strData = NSString(data: data, encoding: NSUTF8StringEncoding)
          println("Body: \(strData)")
  
          if error != nil
          {
              println("Error: " + error.description)
          }
  
      })
      task.resume()
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Does anyone have any ideas on this?  If not I am going to have to talk to Apple as I am sure I am just missing something simple. :) Any help would be greatly appreciated.</p>