NSURLConnection GET request returns -1005, "network connection was lost"

I am trying to make a simple GET request using NSURLConnection in NSURLConnection 6 (Beta7 2) in the iOS 8 SDK, which does not work with "Code 1005, network connection was lost." The call fails when I try to retrieve http://www.google.com or several other sample pages from the Internet, but succeeds if I make a request to a simple HTTP server on localhost ( python -m SimpleHTTPServer ). I also tried using the AFNetworking library (2.4.1) - URLs that do not match NSURLConnection also fail with the library.

Here is my code -

 NSString * url = @"http://0.0.0.0:8000"; // NSString * url = @"http://www.google.com"; NSLog(@"URL : %@", url); // Mutable is probably not required, but just in case it REALLY WANTS me to set HTTP method NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]]; [theRequest setHTTPMethod:@"GET"]; NSURLResponse *urlResponse = nil; NSError *error = nil; NSData * data = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&urlResponse error:&error]; if (error == nil) { NSString *response = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; NSLog(response); } else { NSString *response = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; NSLog(@"%@", [error userInfo]); } 

Logs:

 2014-09-11 17:34:23.950 SearchExample[5092:2074687] URL : http://www.google.com 2014-09-11 17:34:24.023 SearchExample[5092:2074687] { NSErrorFailingURLKey = "http://www.google.com"; NSErrorFailingURLStringKey = "http://www.google.com"; NSLocalizedDescription = "The network connection was lost."; NSUnderlyingError = "Error Domain=kCFErrorDomainCFNetwork Code=-1005 \"The network connection was lost.\" UserInfo=0x7fc8515640a0 {NSErrorFailingURLStringKey=http://www.google.com/, NSErrorFailingURLKey=http://www.google.com/, _kCFStreamErrorCodeKey=57, _kCFStreamErrorDomainKey=1, NSLocalizedDescription=The network connection was lost.}"; "_kCFStreamErrorCodeKey" = 57; "_kCFStreamErrorDomainKey" = 1; } 2014-09-11 17:34:24.023 SearchExample[5092:2074687] URLResponse: (null) 
+42
ios cocoa-touch
Sep 11 '14 at 21:35
source share
7 answers

Recently, I have seen interruptions in the Internet connection on the iPhone 6 simulator, which led to the same errors. My Mac had a working internet connection that the simulator did not. Restarting the simulator fixed the problem.

+92
Sep 11 '14 at 22:14
source share

I was getting this error sequentially on iOS 9 with certain network calls. Two worked fine, but two were not.

It turned out that this was caused by some incorrect parameters that I passed with the request body ... I would not expect this to cause -1005 error ... but it happened .. Getting rid of unnecessary parameters made it all work!

+8
Dec 09 '15 at 14:26
source share

I tried everything that was offered on at least 15 answers from Google, but none of them solved my problem until I tried the following, fully dedicated to my problem. It looks like Wi-Fi connections can get corrupted on a Mac, so if you delete the specific connection you are using and then reconnect (choosing a network and enter your password again), then this will fix the problem and will not become more frightening -1005 " network connection was lost. "

  • Go to the Wi-Fi symbol in the Mac menu and "Turn Wi-Fi Off"
  • Then select "Open Network Preferences" (from the same menu below).
  • In the lower right corner of the Network panel, select Advanced.
  • Select the network connection to which you were previously connected.
  • Click the minus sign below this table to remove this connection.
  • Click "OK" for this window.
  • Click "Apply" in the "Network" window.
  • Return to the Wi-Fi symbol in the menu bar and turn on Wi-Fi again.
  • Wait until the network connection appears, and then select it (and it will ask for the password again because you deleted the connection information).
  • Now he will remember this newly updated connection, which should solve the problem!
+2
Jun 12 '15 at 2:37
source share

Try changing the serialization of the request in AFNetworking http or json. in my case it was json, then I installed http. Now it works.

 [[VTNetworkingHelper sharedInstance] performRequestWithPath:@"Your url " withAuth:YES forMethod:@"POST" withRequestJSONSerialized:NO withParams:params withCompletionHandler:^(VTNetworkResponse *response) { if (response.isSuccessful) { }else { }]; 
+1
May 27 '16 at 10:54
source share

I observed that this problem occurs when you continue the active simulator, and your maker sleeps for a long time (say, from 5 to 10 hours). Then suddenly you run the application on the simulator, it displays the log as

 NSURLConnection GET request returns Code=-1005 "The network connection was lost." 

The solution is to simply exit the simulator, clear the project, and restart the project. It worked for me!

0
Aug 27 '15 at 5:49
source share

I had a similar problem and restarting the simulator did not work. In my case, I could hit the web service as an alternative, since at odd times this would be successful, and even during that time he threw me with this error. I know this is strange, but it was. Solved it with a quick approach:

  let urlconfig = NSURLSessionConfiguration.defaultSessionConfiguration() urlconfig.timeoutIntervalForRequest = 1 urlconfig.timeoutIntervalForResource = 1 let session = NSURLSession(configuration: urlconfig) let task = session.dataTaskWithRequest(request){(data,response,error) in //Processing } task.resume() 
0
Feb 14 '17 at 11:52
source share

A simple and selective solution, tested many times, works great.

// Check the response error with the status code, and if you get -1005, then call this api again.

  if let strErrorReasonCode : Int = response.response?.statusCode { if authentication_Errors_Range.contains(Alamofire_Error) { self.POST(urlString, paramaters: paramaters, showLoader: showLoader, success: { (responseObject) in if response.result.isSuccess { if let value = response.result.value { let dictResponce = self.isValidated(value as AnyObject) if dictResponce.0 == true { success(dictResponce.1) } else { failure(dictResponce.1) } } } }, failure: {_ in failure(jsonResponce) }) } } 
0
Aug 25 '17 at 6:31 on
source share



All Articles