I want to allow invalid SSL certificates with AFNetworking

前端 未结 13 581
南笙
南笙 2020-12-02 10:11

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

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


        
相关标签:
13条回答
  • 2020-12-02 10:33

    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,
        ...
    
    0 讨论(0)
  • 2020-12-02 10:33

    You should subclass the AFHttpClient,and override

    - (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)request 
                                                    success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
                                                    failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;
    

    This is my code.

    - (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)urlRequest success:(void (^)(AFHTTPRequestOperation *, id))success failure:(void (^)(AFHTTPRequestOperation *, NSError *))failure
    {
        AFHTTPRequestOperation *operation = [super HTTPRequestOperationWithRequest:urlRequest success:success failure:failure];
    
        [operation setAuthenticationAgainstProtectionSpaceBlock:^BOOL(NSURLConnection *connection, NSURLProtectionSpace *protectionSpace) {
            return YES;
        }];
        [operation setAuthenticationChallengeBlock:^(NSURLConnection *connection, NSURLAuthenticationChallenge *challenge) {
            if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
                [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
            }
        }];
        return operation;
    }
    

    you use this httpclient and it can access self-unsigned web successfully.

    0 讨论(0)
  • 2020-12-02 10:33

    This is the best method for allowign invalid SSL certificates with AFNetworking.

    use add a _AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES_ to

    project > Build Settings > Preprocessor Macros and add it to Debug entry.

    Hope it Helps

    0 讨论(0)
  • 2020-12-02 10:35

    Note that if you are installing through CocoaPods, #define-ing this macro in your project will not be enough--the compiler macro must be set when compiling the static library in order for it to take effect.

    0 讨论(0)
  • 2020-12-02 10:39

    I am using RestKit so

    client.allowsInvalidSSLCertificate = YES;
    

    does not work. The option is not propagated to the operations created by restkit.

    I am using cocoapods so any changes in the pch file or the pods project get overriden. The "hack" I have been using is a cocoapod post-install operation that adds the required preprocessor definition. At the end of my pod file I have added:

    post_install do |installer_representation|
      installer_representation.project.targets.each do |target|
        if target.name == 'Pods-AFNetworking'
          target.build_configurations.each do |config|
            config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)']
            config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] << '_AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES_=1'
          end
        end
      end
    end
    
    0 讨论(0)
  • 2020-12-02 10:40

    You can now use the allowsInvalidSSLCertificate property of the AFHTTPClient. No need to use defines in the latest versions of AFNetworking.

    AFHTTPClient* client = [AFHTTPClient clientWithBaseURL:@"url"];
    client.allowsInvalidSSLCertificate = YES; //this defaults to no
    
    0 讨论(0)
提交回复
热议问题