发送http post请求时NSURLErrorDomain Code = -1001错误 [英] 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()
}
其中 createRequest()
创建所需的NSURLRequest对象。
where createRequest()
creates the required NSURLRequest object.
使用模拟器时,此功能正常。问题是当我在iPhone上运行我的应用程序时出现以下错误。
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.)"}
<我知道这个错误是超时错误。重新启动应用程序或手机没有帮助。此外,我已经尝试从Web浏览器将图像发布到我的服务器,它没有问题。
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?
编辑:当我监控时我的网络活动,我意识到应用程序发送10 MB数据,即使我发送的图像是0.1 MB。
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.
推荐答案
显然我的帖子请求收到超时错误,因为我发送了一个服务器发现太大的图像。我按比例缩小图像以使服务器接受我的图像。
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 这篇文章建议, NSURLErrorDomain Code = -1001
可能导致错误许多事情包括;
As this post suggests, NSURLErrorDomain Code=-1001
error might be caused by many things including;
- 服务器响应时间限制
- 有关传入数据的服务器规则
- 身份验证问题
希望对其他人有帮助
这篇关于发送http post请求时NSURLErrorDomain Code = -1001错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!