2

I am taking a stab at connecting to web services in iOS and I am running into a lot of problems. I found a practice web service here - http://www.w3schools.com/webservices/tempconvert.asmx?op=CelsiusToFahrenheit

I have written this code out so far

    let soapMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
    "<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/\">\n"
    "<soap:Body>\n"
    "<CelsiusToFahrenheit xmlns=\"http://www.w3schools.com/webservices/\">\n"
    "<Celsius>50</Celsius>\n"
    "</CelsiusToFahrenheit>\n"
    "</soap:Body>\n"
    "</soap:Envelope>\n"

    let url = NSURL(string: "http://w3schools.com/webservices/tempconvert.asmx")!

    var request = NSMutableURLRequest(URL: url)

    let messageLength = "\(soapMessage.utf16Count)"
    NSLog("\(messageLength)")
    request.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
    request.addValue("http://www.w3schools.com/webservices/CelsiusToFahrenheit", forHTTPHeaderField: "SOAPAction")
    request.addValue(messageLength, forHTTPHeaderField: "Content-Length")
    request.HTTPMethod = "POST"
    request.HTTPBody = soapMessage.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)

    var urlSession: NSURLSession = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration(), delegate: self, delegateQueue: NSOperationQueue.mainQueue())

It works to connect but my response comes back as as text/html and not text/xml. My question is why is it returning incorrectly? Also do I use a NSURLSessionDataTask or NSURLSessionDownloadTask?

1
  • You would e better off converting the soapMessage to NSData early and then using the data count, not the utf16Count considering you are converting to data with the string format: NSUTF8StringEncoding. Commented Nov 17, 2014 at 3:23

1 Answer 1

0

for your case, i think its NSURLSessionDataTask. based on this site: http://www.kaleidosblog.com/nsurlsession-in-swift-get-and-post-data

Sign up to request clarification or add additional context in comments.

1 Comment

Welcome to Stack Overflow! Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.