0

I am trying to access a website. I am using a dataTaskWithURL request.

Here is my code:

let task = NSURLSession.sharedSession().dataTaskWithURL(url, completionHandler: { (data, response, error) in

    print("2")

    if let urlContent = data {

        print("3")

        let stringContent = NSString(data: urlContent, encoding: NSUTF8StringEncoding)!

        let arr = stringContent.componentsSeparatedByString("<b>The computer's solutions will appear below</b><br>")

        let second = arr[1]

        let newArr = second.componentsSeparatedByString("</tr></table></center><p align=\"center\">")

        let results = newArr[0]

        self.resultsLabel.text = results

        self.stopActivity()
    } else {
        print(error)
        self.stopActivity()
    }
})

task.resume()

I have also tried running this code on the main block:

dispatch_async(dispatch_get_main_queue(), {() -> Void in
    // Code
})

However, neither of these has been successful. I repeatedly get the following error

Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo={NSUnderlyingError=0x7ff0a3f4c6e0 {Error Domain=kCFErrorDomainCFNetwork Code=-1001 "(null)" UserInfo={_kCFStreamErrorCodeKey=-2102, _kCFStreamErrorDomainKey=4}}, NSErrorFailingURLStringKey=THE URL I AM TRYING TO ACCESS, NSErrorFailingURLKey=THE URL I AM TRYING TO ACCESS, _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-2102, NSLocalizedDescription=The request timed out.})

I have tried looking at this post and this one as well. However, I have not been successful with them. The website loads fairly quickly. How do I get rid of this error?

pkamb
  • 33,281
  • 23
  • 160
  • 191
Pranav Wadhwa
  • 7,666
  • 6
  • 39
  • 61

2 Answers2

0

It could be caused by a number of things; If the issue is happening in the Simulator, I suggest restarting it as per the suggestions in the SO posts below:

It might be help to print response as well to see if the HTTP headers can provide a clue.

Community
  • 1
  • 1
Hitster GTD
  • 96
  • 1
  • 4
0

I realized that I was using https:// instead of http://.

Pranav Wadhwa
  • 7,666
  • 6
  • 39
  • 61