NSURLConnection GET request returns -1005, "the network connection was lost"
I am trying to make a simple GET request using NSURLConnection
in XCode 6 (Beta7 2) on iOS 8 SDK, which is failing with "Code 1005, the network connection was lost". The call fails when I try to fetch http://www.google.com or a few other sample pages from the web, but succeeds if I make a request to a simple HTTP server on localhost (python -m SimpleHTTPServer
). I have also tried using AFNetworking
library (2.4.1) - URLs that fail with NSURLConnection also fail with the library.
Here's 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)
Solution 1:
I have seen internet connectivity failing on the iPhone 6 simulator recently, resulting in the same errors. My Mac had a working internet connection the simulator did not. Restarting the simulator fixed the issue.
Solution 2:
I was getting this error consistently on iOS 9 with certain network calls. Two were working fine but another two were not.
It turned out to be caused by some incorrect parameters I was passing with the request's body... I wouldn't have expected that to cause a -1005 error... but it did.. Getting rid of the unnecessary parameters made it all work!
Solution 3:
I've tried everything suggested on at least 15 answers from Google but not one of them solved my problem until I tried the following which totally addressed my issue. It appears that Wi-Fi connections can become corrupt on the Mac so if you remove the specific connection you are using and then connect again (by choosing the network and entering your password again) then this will fix the issue and no more dreaded -1005 “the network connection was lost” errors.
- Go to the Wi-Fi symbol on your Mac's menubar and "Turn Wi-Fi Off"
- Then choose "Open Network Preferences" (from the same menu, at the bottom).
- In the bottom right-hand corner of the Network panel, choose "Advanced".
- Select the network connection you were previously connected to.
- Hit the minus symbol right below this table to delete this connection.
- Hit "OK" for this window.
- Hit "Apply" on the Network window.
- Go back to the Wi-Fi symbol on your menubar and turn Wi-Fi back on.
- Wait for your network connection to appear and then select it (and it will now ask for a password again because you deleted the connection info).
- It will now remember this newly refreshed connection which should solve the problem!
Solution 4:
Try to change request serialization in AFNetworking http or json. in my case that was json then i set to http. Now that is working.
[[VTNetworkingHelper sharedInstance] performRequestWithPath:@"Your url " withAuth:YES forMethod:@"POST" withRequestJSONSerialized:NO withParams:params withCompletionHandler:^(VTNetworkResponse *response) {
if (response.isSuccessful) {
}else {
}];