alex_izh
alex_izh

Reputation: 527

AFNetworking https request not working

I try connect to https server, but I get error -1012(NSErrorFailingURLKey). If I try connect to http then all is good. But I should connect to https. What does it can be? I tried connect via AFNetworking v1.0 and v2.0. I used AFHTTPRequestOperation, AFHTTPRequestOperationManager, AFHTTPClient and other. P.S. When I try connect to https via firebug, all is good. I mean, server works good.

Upvotes: 6

Views: 3796

Answers (4)

taojigu
taojigu

Reputation: 457

It works on me:

AFHTTPSessionManager* manager = [AFHTTPSessionManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager POST: urlString parameters:bodyDict
     success:^(NSURLSessionDataTask *task, id responseObject){

     return;
     }
     failure:^(NSURLSessionDataTask *task, NSError *error){

      failBlock(error);
     }
 ];

Upvotes: 0

efremidze
efremidze

Reputation: 2842

The security policy in AFNetworking states that allowInvalidCertificates defaults to NO. You need to set it to YES.

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.securityPolicy.allowInvalidCertificates = YES; // not recommended for production

More details: https://github.com/AFNetworking/AFNetworking#security-policy

Upvotes: 4

Davide Vosti
Davide Vosti

Reputation: 2485

I'm using a newer version of the AFNetworking library and #define is not working.

The issue was fixed with setting the property allowsInvalidSSLCertificate to YES on the client itself:

httpClient.allowsInvalidSSLCertificate = YES;

Upvotes: 1

Michaël
Michaël

Reputation: 6734

Try with #define _AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES_ 1 to allow invalid or self signed certificates.

By default, they are not allowed, see AFURLConnectionOperation.h

Upvotes: 5

Related Questions