NSURLErrorDomain Code=-1001 error when a http post request is sent

你离开我真会死。 提交于 2019-12-30 17:16:30

问题


I am using the following code to post an image to my server.

@IBAction func postButtonPressed(sender: UIButton) {
    let task = NSURLSession.sharedSession().dataTaskWithRequest(createRequest("http://xx.xx.xxx.xxx/xxxx/"), completionHandler: {
        data, response, error in
        println(NSString(data: data, encoding: NSUTF8StringEncoding))
    })
    task.resume()
}

where createRequest() creates the required NSURLRequest object.

This works fine when I use a simulator. The problem is that I am getting the following error when I run my app on an iPhone.

Error Domain=NSURLErrorDomain Code=-1001 "The operation couldn’t be completed. (NSURLErrorDomain error -1001.)" UserInfo=0x155e71f0 {NSErrorFailingURLKey=http://xx.xxx.xxx.xxx/xxxx/, NSErrorFailingURLStringKey=http://54.148.156.117/query/, NSUnderlyingError=0x155674d0 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork error -1001.)"}

I have learned that this error is a timeout error. Restarting the app or phone didn't helped. Moreover I have tried posting an image to my server from a web browser and it worked with no problem.

What might be causing this timeout error?

EDIT: When I monitor my network activity, I realized the app sends 10 MB data even though the image that I am sending is 0.1 MB.


回答1:


Apparently my post request was getting a timeout error since I was sending an image that the server would find too large. I scaled down the images in order to make my images acceptable by the server.

As this post suggests, NSURLErrorDomain Code=-1001 error might be caused by many things including;

  • Server response time limit
  • Server rules about the incoming data
  • Authentication problems

Hope that helps to other people




回答2:


In my case issue was in 3rd party library - FirebasePerformance. Bug report URL: https://github.com/firebase/firebase-ios-sdk/issues/486




回答3:


It is late to reply but I recently face the error while running the new application assigned to me. The application is one of them whose URL Domains are blocked by the system administrator of my company.
I ask them to allow me the URL access after which the response came and the application run perfectly.
This may be not the actual solution for everyone but some of the developers may have the restricted environment can get help by this.




回答4:


Well, in my case simply increasing the timeout of request solved the problem.



来源:https://stackoverflow.com/questions/27159510/nsurlerrordomain-code-1001-error-when-a-http-post-request-is-sent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!