[英]NSURLErrorDomain Code=-1001 error when a http post request is sent
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.其中
createRequest()
创建所需的 NSURLRequest 对象。
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.
问题是当我在 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.
此外,我尝试将图像从 Web 浏览器发布到我的服务器,并且没有问题。
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.编辑:当我监控我的网络活动时,我意识到即使我发送的图像是 0.1 MB,应用程序也会发送 10 MB 的数据。
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;正如这篇文章所暗示的,
NSURLErrorDomain Code=-1001
错误可能是由许多原因引起的,包括:
Hope that helps to other people希望对其他人有帮助
In my case issue was in 3rd party library - FirebasePerformance
.就我而言,问题出在第 3 方库 -
FirebasePerformance
。 Bug report URL: https://github.com/firebase/firebase-ios-sdk/issues/486错误报告网址: https : //github.com/firebase/firebase-ios-sdk/issues/486
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.
该应用程序是其中之一,其URL 域被我公司的系统管理员阻止。
I ask them to allow me the URL access after which the response came and the application run perfectly.我要求他们允许我访问 URL,然后响应出现并且应用程序运行完美。
This may be not the actual solution for everyone but some of the developers may have the restricted environment can get help by this.这可能不是每个人的实际解决方案,但一些开发人员可能在受限环境中可以通过此获得帮助。
好吧,就我而言,只需增加请求的超时时间即可解决问题。
I fixed using headers with application/json and sending on the request:我修复了使用带有 application/json 的标头并发送请求:
let headers: HTTPHeaders = [
.accept("application/json")
]
AF.request(url, method: .post, parameters: score, encoder: JSONParameterEncoder.default, headers: headers)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.