NSURLConnection GET请求返回-1005,“网络连接丢失” [英] NSURLConnection GET request returns -1005, "the network connection was lost"
问题描述
NSURLConnection
做一个简单的GET请求,这是失败的代码1005,网络连接丢失了。当我尝试从网络中抓取 http://www.google.com 或其他几个示例页面时,调用失败,但如果成功我向localhost( python -m SimpleHTTPServer
)上的简单HTTP服务器发出请求。我也尝试使用 AFNetworking
库(2.4.1) - 与NSURLConnection失败的URL也失败与库。 这是我的代码 -
NSString * url = @http ://0.0.0.0:8000;
// NSString * url = @http://www.google.com;
NSLog(@URL:%@,url);
//可能不需要Mutable,但是为了防止它真正需要设置HTTP方法
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]);
}
日志:
2014-09-11 17:34:23.950 SearchExample [5092:2074687]网址: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 =网络连接丢失。
NSUnderlyingError =错误域= kCFErrorDomainCFNetwork代码= -1005 \网络连接丢失。\UserInfo = 0x7fc8515640a0 {NSErrorFailingURLStringKey = http://www.google.com/,NSErrorFailingURLKey = http:// www.google.com/,_kCFStreamErrorCodeKey = 57,_kCFStreamErrorDomainKey = 1,NSLocalizedDescription =网络连接丢失。
_kCFStreamErrorCodeKey= 57;
_kCFStreamErrorDomainKey= 1;
}
2014-09-11 17:34:24.023 SearchExample [5092:2074687] URLResponse:(null)
我最近在iPhone 6模拟器上看到互联网连接失败,导致相同的错误。我的Mac有一个工作的互联网连接模拟器没有。重新启动模拟器固定问题。
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)
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.
这篇关于NSURLConnection GET请求返回-1005,“网络连接丢失”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!