streem
streem

Reputation: 9154

Request not sent

I'm having a weird problem when i consume my API from my app. Sometimes, for no reason, the request is just not sent, and it fails at the end of the time-out with the following error:

Error Domain=NSURLErrorDomain Code=-1001 "The request timed out."

I have tried many API such as NSURLConnection delegates, NSURLSession and NSURLConnection.sendSynchronousRequest without success.

Here is a sample project i have made to highlight the issue. ConnectionBugApp

Here are the steps to reproduce:

A few details that might help:

Any ideas or workaround would be greatly appreciated ! Feel free to ask for more details.

Thanks

EDIT

I've edited the description a lot since i first posted it (hoping to make it cleaner and clearer), i'm sorry if some answers or comment don't really make sense anymore.

Upvotes: 11

Views: 2200

Answers (4)

streem
streem

Reputation: 9154

Like mentioned in comments, I had DNSSEC (cache poisoning protection) enabled on my hosting service.

Disabling it, fixed the issue, even though that might not be a really nice solution. After a few weeks of searching, that'll be good enough.

I'll give the bounty to someone that can explain it, or who can provide a better solution.

Upvotes: 2

Kirit Modi
Kirit Modi

Reputation: 23407

In your code your request take default timeout is 60s, but you can change Request time out in your code as below.

in your NetworkItem class change time out.

init(request:NSMutableURLRequest){
    self.request = request
    self.request.timeoutInterval = 120
    super.init()
}

Upvotes: 1

djbp
djbp

Reputation: 715

I have come across this issue when using an asynchronous request. It seems as though iOS limits the number of open connections to a single domain, such that all subsequent connections fail in the manner you have described.

If connections typically complete quickly, this possibly won't be an issue.

The solution is to limit the number of open connections to the same domain to prevent this from happening.

The answer posted by karlos works because the synchronisity of the connection blocks others from being opened.

Upvotes: 2

Karlos
Karlos

Reputation: 1661

Try the following code for the connection.This would help you.

      let urlData = NSURLConnection.sendSynchronousRequest(request, returningResponse: &response, error: &error)

            if error != nil || urlData!.length == 0
            {
                println("Error happend timeout======\(error?.code)!")

                continue
            }
            else //else1
            {if let httpResponse = response as? NSHTTPURLResponse
                {
                    println("Status Code for successful---\(httpResponse.statusCode)")

                    // For example 502 is for Bad Gateway
                    if (httpResponse.statusCode == 502)
                    {
                        // Check the response code from your server and break
                        break
                    }
                    else
                    {

                        //Your code
                     }
            }
    }

You can get the list of HTTPS status code in the following link StatusCode

Upvotes: 0

Related Questions