简体   繁体   English

仅在移动网络上获取错误域= NSURLErrorDomain代码= -1005

[英]Getting Error Domain=NSURLErrorDomain Code=-1005 on mobile network only

I have built an app using Xcode 8.3.2 on iOS 10.3.2. 我已经在iOS 10.3.2上使用Xcode 8.3.2构建了一个应用程序。 And I have used NSURLSession to implement the webservices. 而且我已经使用NSURLSession来实现Web服务。 I'm using the following code to do it. 我正在使用以下代码来做到这一点。

NSMutableURLRequest *request=[[NSMutableURLRequest alloc]initWithURL:Url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:180.0] ;

NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
[dict setObject:lati forKey:@"latitude"];
[dict setObject:longi forKey:@"longitude"];

NSError *errJsonToData = nil;
NSData *requestPostData=[NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&errJsonToData] ; 

[request setHTTPMethod:@"POST"] ;
[request setValue:[NSString stringWithFormat:@"%lu",(unsigned long)[requestPostData length]] ;forHTTPHeaderField:@"Content-Length"] ;
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:requestPostData] ;


NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session=[NSURLSession sessionWithConfiguration:sessionConfiguration delegate:nil delegateQueue:[NSOperationQueue mainQueue]];

NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable connectionError) {
    if(connectionError)
    {
      NSLog(@"Error Description: %@",[connectionError localizedDescription]);
    } else { //do anything}
}];
[dataTask resume];

Now My webservices urls are http connection. 现在,我的网络服务网址是http连接。 For this I have also added App Transport Security in Info.plist . 为此,我还在Info.plist中添加了App Transport Security But the problem is this code works fine when connected to wifi but throws error sometimes while connected to mobile network ie 3G, LTE. 但是问题是,此代码在连接wifi时工作正常,但在连接到3G,LTE等移动网络时 有时会引发错误。 I get the error "The network connection was lost." 我收到错误消息“网络连接丢失。” The error is as follows: 错误如下:

Error: Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." 错误:错误域= NSURLErrorDomain代码= -1005“网络连接丢失。” UserInfo=0x7ba8e5b0 {NSErrorFailingURLStringKey=, _kCFStreamErrorCodeKey=57, NSErrorFailingURLKey=, NSLocalizedDescription=The network connection was lost., _kCFStreamErrorDomainKey=1, NSUnderlyingError=0x7a6957e0 "The network connection was lost."} UserInfo = 0x7ba8e5b0 {NSErrorFailingURLStringKey =,_kCFStreamErrorCodeKey = 57,NSErrorFailingURLKey =,NSLocalizedDescription =网络连接丢失。,_kCFStreamErrorDomainKey = 1,NSUnderlyingError = 0x7a6957e0“网络连接丢失。”}

If it is a typical server error then why its only happening on mobile network? 如果这是典型的服务器错误,那为什么仅在移动网络上发生呢?

Depending on what server you're using, it is possible that the issue is with the "KeepAlive" setting on your server (usually due to higher traffic to the server than expected). 根据您使用的服务器,问题可能出在服务器上的“ KeepAlive”设置(通常是由于服务器的流量比预期的要大)。 Try catching for that specific error code and then re-attempt the exact same URL Request. 尝试捕获该特定的错误代码,然后重新尝试完全相同的URL请求。 Hopefully it will just work, but if it has that same error a second time, then there is a bigger issue that needs to be solved. 希望它会起作用,但是如果第二次出现相同的错误,那么还有一个更大的问题需要解决。

You can also try looking at the KeepAlive settings on your server and either disable the feature or tweak the settings, but I suggest trying that other option first to narrow down some possible issues. 您也可以尝试查看服务器上的KeepAlive设置,然后禁用该功能或​​调整设置,但是我建议您首先尝试使用该其他选项来缩小一些可能的问题。

Good luck! 祝好运!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在移动网络上解析iOS SDK错误域= NSURLErrorDomain代码= -1005“网络连接丢失。” - Parse iOS SDK Error Domain=NSURLErrorDomain Code=-1005 “The network connection was lost.” on mobile network Error Domain = NSURLErrorDomain代码= -1005“网络连接丢失 - Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost 随机获取Error Domain = NSURLErrorDomain代码= -1005“网络连接丢失。” - Randomly getting Error Domain=NSURLErrorDomain Code=-1005 “The network connection was lost.” 随机和偶然的网络错误(NSURLErrorDomain Code=-1001 和 NSURLErrorDomain Code=-1005) - Random and occasional network error (NSURLErrorDomain Code=-1001 and NSURLErrorDomain Code=-1005) 无法确定当前的国家/地区代码:错误域= NSURLErrorDomain代码= -1005“网络连接丢失。” - Could not determine current country code: Error Domain=NSURLErrorDomain Code=-1005 “The network connection was lost.” 为什么NSURLConnection失败,错误域= NSURLErrorDomain Code = -1005“网络连接丢失。”在Swift iOS8中? - Why NSURLConnection failed with Error Domain=NSURLErrorDomain Code=-1005 “The network connection was lost.” in Swift iOS8? Error Domain = NSURLErrorDomain代码= -1005“网络连接丢失。”-在iOS 9中使用https - Error Domain=NSURLErrorDomain Code=-1005 “The network connection was lost.” - Occurs in iOS 9 with https 错误域= NSURLErrorDomain代码= 1005向Uber-api的卷曲请求 - Error Domain=NSURLErrorDomain Code=-1005 curl request to Uber-api Alamofire请求错误:NSURLErrorDomain代码-1005网络连接丢失 - Alamofire Request error: NSURLErrorDomain Code -1005 The network connection was lost NSURLErrorDomain代码= -1005“网络连接丢失 - NSURLErrorDomain Code=-1005 "The network connection was lost
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM