10

- (NSString *)baseURL pulls a String from a file.

NSString *endpoint = @"/authentication.json";
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

NSString *token = @"mytokenvalue"

[manager.requestSerializer setValue:token forHTTPHeaderField:TOKEN_HEADER];
manager.requestSerializer = [AFJSONRequestSerializer serializer];

NSString *path = [NSString stringWithFormat:@"%@%@", [self baseURL], endpoint];

[manager    POST:path
    parameters:nil
    success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (operation.response.statusCode == 200) {
            [self.delegate validated:true];
        }
  } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self.delegate validated:false];
}];

(lldb) po error Error Domain=NSURLErrorDomain Code=-1012 "The operation couldn’t be completed. (NSURLErrorDomain error -1012.)" UserInfo=0x7bf3ed90 {NSErrorFailingURLKey=https://1.2.3.4/authentication.json, NSErrorFailingURLStringKey=https://1.2.3.4/authentication.json}

I can connect to 1.2.3.4 through VPN in my terminal, but not through AFNetworking. I tried 1.2.3.4 and https://1.2.3.4 .

I have

[AFHTTPRequestOperationManager manager].securityPolicy.allowInvalidCertificates = YES;

In my app Delegate.

How do I connect to this IP through AFNetworking?

EDIT: Great point on validating certificate chain.

I disabled that and validates domain name in AFSecurityPolicy.m

Confirmed that db) po [AFHTTPRequestOperationManager manager].securityPolicy.SSLPinningMode AFSSLPinningModeNone is left on None, too.

Steve Wilford
  • 8,894
  • 5
  • 42
  • 66
quantumpotato
  • 9,637
  • 14
  • 70
  • 146
  • I don't see where you are setting the security policy in your code. – l'L'l Jun 25 '15 at 22:12
  • As per this [list of error codes](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Constants/index.html#//apple_ref/doc/constant_group/URL_Loading_System_Error_Codes) your authentication was cancelled `NSURLErrorUserCancelledAuthentication = -1012`. So i would try with `validatesCertificateChain` set to `NO` – JamesWebbTelescopeAlien Jun 25 '15 at 22:18
  • Where is `[AFHTTPRequestOperationManager manager].securityPolicy.allowInvalidCertificates = YES; ` call? – JamesWebbTelescopeAlien Jun 25 '15 at 22:49
  • In App Delegate just prior to assigning window rootViewController and calling makeKeyAndVisible – quantumpotato Jun 26 '15 at 02:00
  • You might not be able to as the network interfaces belong to the Mac OS with the simulator. – uchuugaka Jun 28 '15 at 23:31
  • After reading up on this a bit, I think there is a chance that even if you set the property to `YES` it gets reset somehow in the `NSURLConnectionDelegate` implementation... just a guess. I kind of just posted what I found in the answer below, I'm trying to clean it up a bit and test so I can give you a better answer. – tacos_tacos_tacos Jun 29 '15 at 01:46
  • Edit - I deleted my answer because it didn't work. That's annoying! I'll try some more things, but of course I did try just changing the security policy to the most permissive settings and that did not help. Error code was `1012` for `AFNetworking` and `9086` for `CFNetwork` – tacos_tacos_tacos Jun 29 '15 at 02:39
  • Thanks @tacos_tacos_tacos -- I've tried setting the AFSecurityPolicy's default policy to the most permissive settings and still getting that. I'm going to test with VPN on the actual hardware but I need to get the code working. – quantumpotato Jun 29 '15 at 02:57
  • @quantumpotato one thing I meant to ask , and have not tested, is whether there is a distinction in result between an invalid host with an IP address vs an invalid host with an actual hostname. I would think not, but then again by convention IP addresses are never the subject of a SSL certificate. – tacos_tacos_tacos Jun 29 '15 at 03:13
  • What's the point of using HTTPS if you're ignoring the cert? It just adds overhead without providing any added security whatsoever. You might as well just use HTTP. If you really want to get a self-signed cert working in a way that actually adds security, you'll have to subclass parts of AFNetworking (or ditch it and use NSURLConnection directly). For details on what your custom authentication delegate method needs to do, read: https://developer.apple.com/library/mac/documentation/NetworkingInternet/Conceptual/NetworkingTopics/Articles/OverridingSSLChainValidationCorrectly.html – dgatwood Jun 29 '15 at 04:20
  • So I found the solution. It was the port. cURL was automatically finding the right port, but NSURLconnection wasn't – quantumpotato Jun 29 '15 at 20:44
  • have you connect vpn in your simulator?? – Gaurav Patel Jul 03 '15 at 05:31

1 Answers1

0

So I found the solution. It was the port. cURL was automatically finding the right port, but NSURLconnection wasn't. I had to specify

https://1.2.3.4:12345 as my URL. (my computer's IP)

On my machine I run

socat TCP-LISTEN:12345,fork TCP:5.6.7.8:12345 (where 5.6.7.8 is server IP)

quantumpotato
  • 9,637
  • 14
  • 70
  • 146