gpt4 book ai didi

ios - NSURLConnection GET 请求返回-1005, "the network connection was lost"

转载 作者:IT王子 更新时间:2023-10-29 07:38:37 36 4
gpt4 key购买 nike

我正在尝试在 iOS 8 SDK 的 XCode 6 (Beta7 2) 中使用 NSURLConnection 发出一个简单的 GET 请求,该请求因“代码 1005,网络连接丢失”而失败。当我尝试获取 http://www.google.com 时调用失败或网络上的其他一些示例页面,但如果我向本地主机上的简单 HTTP 服务器发出请求,则会成功 (python -m SimpleHTTPServer)。我也尝试过使用 AFNetworking 库 (2.4.1) - 使用 NSURLConnection 失败的 URL 也使用库失败。

这是我的代码-

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]);
}

日志:

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)

最佳答案

我最近看到 iPhone 6 模拟器上的互联网连接失败,导致了同样的错误。我的 Mac 有一个可用的互联网连接,而模拟器没有。重新启动模拟器解决了这个问题。

关于ios - NSURLConnection GET 请求返回-1005, "the network connection was lost",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25797339/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com