15

I am trying to perform a HTTP Get request, but I keep on getting Error Domain=NSURLErrorDomain Code=-1012 error. MY code is :

@try {
    NSString *url = [[NSString alloc] initWithFormat:@"%@%@", @"http://api.mintchat.com/agent/chats/", agentSecret];

    NSLog(@"******Agents Active List URL Str - %@", url);
    NSURL *nsUrl = [NSURL URLWithString:url];  
    NSLog(@"******Agents Active List URL - %@", nsUrl);

   // Make the request, returning a JSON response, just as a plain old string.
   //NSData *jsonDataString = [[NSString stringWithContentsOfURL:[NSURL URLWithString:url] encoding:NSUTF8StringEncoding error: nil] dataUsingEncoding:NSUTF8StringEncoding];

    NSError *error = nil;

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:nsUrl];
    [request setHTTPMethod:@"GET"];
    [request setValue:API_ID forHTTPHeaderField:@"X-API-ID"];
    [request setValue:API_SECRET forHTTPHeaderField:@"X­-API-­SECRET"];

    NSHTTPURLResponse *response = nil;
    NSData *urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

    NSLog(@"Response Code %d", [response statusCode]);
    if ([response statusCode] >= 200 && [response statusCode] < 300) {
        NSString *responseData = [ [NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
       NSLog(@"Response  ==> %@", responseData);        
    } else {
       if (error)
          NSLog(@"ERror : %@", error);
    }
}@catch(NSException *e) {
        NSLog(@"Exception - %@", e);
   }

I am to get response in JSon, but as things are not working so we are trying with some normal response. I confirmed the Headers to be added to the request. I need to add the both values to the headers. The url is also proper.

The results/logs I get is : Response Code 0

Error : Error Domain=NSURLErrorDomain Code=-1012 "The operation couldn’t be completed. (NSURLErrorDomain error -1012.)" UserInfo=0x8abf470 { NSErrorFailingURLKey=.......NSErrorFailingURLStringKey=......NSUnderlyingError=0x8dafed0 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork error -1012.)" }

I also searched a lot on net reg this error, bit couldn't get any substantial help. I also confirmed regarding access to the web server api. With other url also I get the same results. From 3 urls, I could achieve results of 1st url using POST, but those other 2 urls - just could't get thru it.

Can anyone please help me with this problem. Where am going wrong ? Why can't access the api's. Any help is highly appreciated. Thanks

zhangxaochen
  • 32,744
  • 15
  • 77
  • 108
Tvd
  • 4,463
  • 18
  • 79
  • 125

5 Answers5

38

You may get this error while hitting a HTTPS (not HTTP) URL with a bad/expired certificate with AFNetworking. (Chrome shows the "Your connection is not private" message when hitting the URL).

Peter O.
  • 32,158
  • 14
  • 82
  • 96
manroe
  • 1,645
  • 20
  • 35
10

This error is NSURLErrorUserCancelledAuthentication

Returned when an asynchronous request for authentication is cancelled by the user. This is typically incurred by clicking a “Cancel” button in a username/password dialog, rather than the user making an attempt to authenticate.

It means you need authentication to access the resource which you are trying to access w/o signing your request properly (wrong agentSecret or API_ID or API_SECRET)

msk
  • 8,885
  • 6
  • 41
  • 72
  • Got the solution from your error. Required to use NSURLRequest instead of NSMutableRequest, hence added headers to mutRequest & made a copy of it to request. And the original problem - In header fields, in place of '-' in X­-API-­SECRET, unicode value was being sent thru the request so it was X\u000API\u000SECRET; thus it wasn't being authenticated and was getting failed. Added those header keys & values in instance vars, corrected the request factor & thus was able to access the api successfully. Thanks. – Tvd Mar 01 '14 at 09:58
4

For my case i just set Yes for following flag of afnetworking http client. allowsInvalidSSLCertificate = YES, it starts working for https get request.

jatin
  • 181
  • 7
  • I am having a similar situation here. It works after I switch not to use HTTPS. – chubao Jan 22 '16 at 02:28
  • that is correct if you use http instead of https, but actually we need to have valid self sign certificate and need to set HTTPClient.defaultSSLPinningMode = AFSSLPinningModePublicKey; – jatin Jan 31 '16 at 11:06
2

If it helps anyone, you will get this error whenever a server sends back a 401 (unauthorized) status code.

inorganik
  • 24,255
  • 17
  • 90
  • 114
0
<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>localhost</key>
        <dict>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
        </dict>
    </dict>
</dict>

just paste these lines in info-list