0
votes

I am trying to perform an HTTP Request but I am getting the error: Optional(Error Domain=NSCocoaErrorDomain Code=256 "The operation couldn’t be completed. (Cocoa error 256.)" UserInfo=0x7d26e9b0 {NSURL=https://www.google.com/?gws_rd=ssl})

Here is my code:

    let url = NSURL(string: "http://www.google.com/?gws_rd=ssl")
    var error: NSError?
    let html = NSString(contentsOfURL: url, encoding: NSUTF8StringEncoding, error: &error)

    if (error != nil) {
        println(error)
    } else {
        println(html)
    }
1
Not sure...but this may help you stackoverflow.com/questions/24016142/… - Kostis
Works for me in the swift REPL. Are you on mac/ios/repl? Do you have any other code affecting NSURL things? - Kevin
I am on a Mac, I do not have any other things affecting it. I have discovered it works on other URL's but I am still interested as to why this fails. Could it be the SSL? - user1939991
Other than the fact that Xcode (latest/6.1) prompts me to unwrap the variable url with a ! before this compiles, and both error and html should be unwrapped when printing/using them, the code worked fine when I tested it - Seb Jachec

1 Answers

0
votes

You must unwrap url and change encoding to NSASCIIStringEncoding. This line becomes as follows:

let html = NSString(contentsOfURL: url!, encoding: NSASCIIStringEncoding, error: &error)