I want to allow invalid SSL certificates with AFNe

2019-01-10 08:22发布

I want to allow invalid SSL certificates. My main code is below:

myClient = [[MyClient alloc] init];
[myClient getHtml:@"/path/to/the/distination.html"];

The MyClient class code is below:

#import <Foundation/Foundation.h>
#import "AFNetworking.h"

@interface MyClient : NSObject

- (void)getHtml:(NSString *)path;

@end

#define _AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES_

@implementation MyClient

- (void)getHtml:(NSString *)path
{
    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"https://trusted.server.net"]];
    [httpClient getPath:path parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"%@", responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error = %@", error);
    }];
}

@end

I read below page and tried the macro but this doesn't work.

Self Signed certificate SSL · Issue #189 · AFNetworking/AFNetworking https://github.com/AFNetworking/AFNetworking/issues/189

Please help me...

13条回答
Juvenile、少年°
2楼-- · 2019-01-10 09:07

Here's how you can do it using the manager for a POST operation.

 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; //initialize
 manager.securityPolicy.allowInvalidCertificates=YES;    //allow unsigned
 manager.responseSerializer=[AFJSONResponseSerializer serializer];   //set up for JSOn  
 [manager POST:@"myweb.com" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
     //success stuff
 }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
     //error stuff
}];

EDIT - swift version:

    var securityPolicy = AFSecurityPolicy()
    securityPolicy.allowInvalidCertificates = true;
    manager.securityPolicy = securityPolicy;
    manager.POST(
        "https://exmaple.com/foobar.php",
        nil,
    ...
查看更多
登录 后发表回答