0

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)
    }
user1939991
  • 187
  • 4
  • 14
  • Not sure...but this may help you http://stackoverflow.com/questions/24016142/how-to-make-an-http-request-in-swift – Kostis Oct 27 '14 at 23:46
  • Works for me in the swift REPL. Are you on mac/ios/repl? Do you have any other code affecting NSURL things? – Kevin Oct 27 '14 at 23:46
  • 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 Oct 28 '14 at 01:49
  • 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 Oct 28 '14 at 16:24

1 Answers1

0

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

let html = NSString(contentsOfURL: url!, encoding: NSASCIIStringEncoding, error: &error)
fpg1503
  • 7,492
  • 6
  • 29
  • 49