Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AFNetworking - HTTP load failed (error code: -999) swift iOS

I am using AFNetworking with iOS 11. I am getting error like:

Task <2EC9C49F-1889-4BFF-83B4-2047ED6E5F2A>.<1> HTTP load failed (error code: -999 [1:89]) Error(countries.php): Error Domain=NSURLErrorDomain Code=-999 "cancelled" UserInfo={NSErrorFailingURLStringKey=https://www.plrc.us/api/countries.php, NSLocalizedDescription=cancelled, NSErrorFailingURLKey=https://www.plrc.us/api/countries.php}

I have tried all the possible way.

info.plist

Please help me to solve this.

Thank you

like image 775
Riddhi Shah Avatar asked Sep 27 '17 18:09

Riddhi Shah


1 Answers

There are a few, minor, unrelated issues:

  1. NSExceptionAllowsInsecureHTTPLoads should be a Boolean, not a string.

  2. The web service returns JSON, but leaves its Content-Type as text/html. But AFNetworking (and its Swifty sibling, Alamofire), validate Content-Type HTTP headers.

    If using AFNetworking, this means that you probably want to add text/html to the list of acceptable content-types for the AFJSONResponseSerializer. (Or you could use AFHTTPResponseSerializer, but then you have to parse the JSON yourself.)

But neither of these are likely the source of the NSURLErrorCancelled. I was able to query this web service from iOS without getting this "cancelled" error (both with your plist settings, and without any plist network related settings at all).

I wonder if there might be some configuration problem on your computer or network. I'd try testing this on a physical device rather than the simulator. (That eliminates the computer configuration as possible source of the problem.) If that works, I'd try testing this on cellular connection rather than over wifi. (That eliminates the configuration of your LAN, e.g. some proxy server weirdness, as the source of the problem.)

In AFNetworking, the default authentication challenge routine passes NSURLSessionAuthChallengeCancelAuthenticationChallenge to the completion handler of URLSession:didReceiveChallenge:completionHandler:, which will result in NSURLErrorCancelled. But standard system configuration will not trigger this. But if there's something about your computer or network which triggers this authentication challenge in AFURLSessionManager.

For example, I was able to reproduce your problem when using Charles, and disabling the Charles Root Certificate, resulting in this sort of un-enlightening "cancelled" error. In my particular example, once I configured my simulator to trust Charles for SSL, via Charles' "Help" » "SSL Proxying" » "Install Charles Root Certificate for iOS Simulators"). I'm not saying that this is the precise problem in your case (this is unique to users of Charles), but it's an example of a broader class of problems that might trigger an authentication challenge, resulting in AFNetworking to cancel the challenge, yielding NSURLErrorCancelled.

Bottom line, try varying the configuration with which you're trying to connect to the remote server and see if you can identify configurations that do not manifest the problem and others that do.

like image 183
Rob Avatar answered Oct 10 '22 05:10

Rob