ios 错误:错误域=NSURLErrorDomain 代码=-1001“请求超时。”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26972822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 03:39:50  来源:igfitidea点击:

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

iosios8ios-simulatorxcode6.1

提问by iYoung

I am working on an application in Xcode 6.1, iOS 8.1; the application was working completely fine till 2 days before, but today as I executed it I got an error in the web service & the error is printed below.

我正在 Xcode 6.1、iOS 8.1 中开发一个应用程序;该应用程序在 2 天前一直运行良好,但今天当我执行它时,我在 Web 服务中出现错误,错误打印如下。

Error: Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo=0x7c6899b0 {NSErrorFailingURLStringKey=, NSErrorFailingURLKey=, NSLocalizedDescription=The request timed out., NSUnderlyingError=0x7c688f60 "The request timed out."}

错误:错误域=NSURLErrorDomain 代码=-1001“请求超时。” UserInfo=0x7c6899b0 {NSErrorFailingURLStringKey=, NSErrorFailingURLKey=, NSLocalizedDescription=请求超时,NSUnderlyingError=0x7c688f60 "请求超时。"}

I had used AFNetworking 2.x and following code snippet to make network call:

我曾使用 AFNetworking 2.x 和以下代码片段进行网络调用:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes=[manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];

[manager POST:<URL>
   parameters:<parameters>
      success:^(AFHTTPRequestOperation *operation, id responseObject) {

          NSLog(@"JSON: %@", responseObject);
          NSError *error = nil;
          NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:&error];
          if (error) {
              NSLog(@"Error serializing %@", error);
          }
          NSLog(@"Dictionary %@", JSON);
          NSLog(@"Success");
      }
      failure:^(AFHTTPRequestOperation *operation, NSError *error) {
          NSLog(@"Error: %@", error);
      }]; 
}

UPDATE: I had done quit/relaunched the iOS Simulator.app, reseted content & settings, but nothing worked.

更新:我已经退出/重新启动了 iOS Simulator.app,重置了内容和设置,但没有任何效果。

采纳答案by iYoung

There was no issue in the code. I guess the simulator internally was not able to connect to the internet, so that is why it was not able to connect to the server after providing various timeout intervals. But it worked completely fine when I executed another day. Thanks to @Andrew Jaffee for helping me to find this issue.

代码中没有问题。我猜模拟器内部无法连接到互联网,这就是为什么它在提供各种超时间隔后无法连接到服务器。但是当我改天执行时它完全正常。感谢@Andrew Jaffee 帮助我找到了这个问题。

回答by agrippa

Had this problem and had a different resolution so I thought I'd add it here:

有这个问题并且有不同的分辨率,所以我想我会在这里添加它:

Basically, I was sending some parameters when it should have been a clean GET request. Deleted the parameters, GET request worked just fine.

基本上,当它应该是一个干净的 GET 请求时,我发送了一些参数。删除了参数,GET 请求工作得很好。

回答by Babac

iOS Simulator -> Reset Content and Settings; worked for me

iOS 模拟器 -> 重置内容和设置;为我工作

回答by sha123

I was calling local server connection and was getting this error. I was using different network in my device and phone. When I connected both to same wifi, it worked.

我正在调用本地服务器连接并收到此错误。我在我的设备和手机中使用不同的网络。当我将两者都连接到同一个 wifi 时,它起作用了。

回答by Jeremy Huddleston Sequoia

This can happen if your network configuration changes while the simulator is running. Please reboot the simulator runtime (eg: quit/relaunch the iOS Simulator.app) and that will likely fix your problem.

如果您的网络配置在模拟器运行时发生变化,就会发生这种情况。请重新启动模拟器运行时(例如:退出/重新启动 iOS Simulator.app),这可能会解决您的问题。

If the problem persists, please update your question to indicate that the issue you are experiencing is not this known problem. Thanks.

如果问题仍然存在,请更新您的问题以表明您遇到的问题不是此已知问题。谢谢。

回答by Szuwar_Jr

Ok, I lost a lot of time investigeting similar issue.

好的,我在调查类似问题上浪费了很多时间。

In my case the problem was strange (bad?) firewall on the server. It was banning device when there was many (not so many) requests in short period of time.

在我的情况下,问题是服务器上的奇怪(坏?)防火墙。当短时间内有很多(不是很多)请求时,它会禁止设备。

I believe you can do easy test if you are facing similar issue.

如果您遇到类似的问题,我相信您可以进行简单的测试。

  1. Send a lot of (depends of firewall settings) requests in loop (let's say 50 in 1 second).
  2. Close/Kill app (this will close connections to server)
  3. (OPTIONAL) Wait a while (lets say 60 seconds)
  4. Start app again and try send request
  1. 循环发送大量(取决于防火墙设置)请求(假设 1 秒内发送 50 个)。
  2. 关闭/杀死应用程序(这将关闭与服务器的连接)
  3. (可选)稍等片刻(比如说 60 秒)
  4. 再次启动应用程序并尝试发送请求

If you now got timeout for all next requests, you probably have same issue and you should talk

如果你现在所有下一个请求都超时了,你可能有同样的问题,你应该谈谈

with sever guys.

与服务器的家伙。

PS: if you don't have access to server you can give user info that he should restart wifi on device to quit that timeout loop. It could be last resort in some cases.

PS:如果您无权访问服务器,您可以向用户提供他应该重新启动设备上的 wifi 以退出超时循环的信息。在某些情况下,它可能是最后的手段。

回答by Ahmed Salama

You must close the firewall and it will work. I tried this solution and it worked for me.

您必须关闭防火墙,它才能工作。我尝试了这个解决方案,它对我有用。