NSURLConnection GET 请求返回-1005,“网络连接丢失"; [英] NSURLConnection GET request returns -1005, "the network connection was lost"

查看:16
本文介绍了NSURLConnection GET 请求返回-1005,“网络连接丢失";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 iOS 8 SDK 上的 XCode 6 (Beta7 2) 中使用 NSURLConnection 发出一个简单的 GET 请求,但失败并显示代码 1005,网络连接丢失".当我尝试从web,但如果我向本地主机上的简单 HTTP 服务器发出请求(python -m SimpleHTTPServer),则成功.我也尝试过使用 AFNetworking 库 (2.4.1) - 因 NSURLConnection 失败的 URL 也因库失败.

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.

这是我的代码 -

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 有一个有效的互联网连接,而模拟器没有.重新启动模拟器解决了这个问题.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆