7

AFNetworking をセットアップしましたが、https URL を受け入れません。AFNEtworking を SSL 経由で接続するにはどうすればよいですか。

次のコードがあります。

  NSMutableURLRequest *apiRequest =
[self multipartFormRequestWithMethod:@"POST"
                                path: pathstr
                          parameters: params
           constructingBodyWithBlock: ^(id <AFMultipartFormData>formData)
           {
               //TODO: attach file if needed
           }];



AFJSONRequestOperation* operation = [[AFJSONRequestOperation alloc] initWithRequest: apiRequest];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    //success!
    completionBlock(responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    //failure :(

    NSLog(@"%@", error);
    completionBlock([NSDictionary dictionaryWithObject:[error localizedDescription] forKey:@"error"]);
}];

[operation start];
4

4 に答える 4

6

これは明らかに、自己署名されていない証明書がある場合、または追加した場合にのみ機能します。

#define _AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES_ を pch ファイルに追加します。これにココア ポッドを使用している場合は、AFHTTPRequestOperation をサブクラス化し、以下を実装する必要があります。

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
    if ([[protectionSpace authenticationMethod] isEqualToString:NSURLAuthenticationMethodServerTrust]) {

        if ([self bypassSslCertValidation:protectionSpace])
            return YES;
        else
            return [super connection:connection canAuthenticateAgainstProtectionSpace:protectionSpace];

    }
    return [super connection:connection canAuthenticateAgainstProtectionSpace:protectionSpace];

}

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
        if ([self bypassSslCertValidation:challenge.protectionSpace]) {
            [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
            return;
        }
        else
            return [super connection:connection didReceiveAuthenticationChallenge:challenge];
        return;
    }
}

- (BOOL) bypassSslCertValidation:(NSURLProtectionSpace *) protectionSpace
{
    if (ENVIRONMENT_TYPE == DEV_ENV || ENVIRONMENT_TYPE == STAGING_ENV) {
        return YES;
    }
    return NO;
}

次に、AFNEtworking に新しいサブクラスを使用するように指示します。

AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@""]];
[client registerHTTPOperationClass:[YourSubClassHTTPRequestOperation class]];

これは世界で最も簡単なことではなく、技術的に自己署名を無視しても機能しませんが、標準の SLL 証明書を使用すれば問題なく機能する可能性があります。このコードを削除するか、デバッグ時にのみ使用できるようにすることを忘れないでくださいリリースする予定がある場合。

コメントには文字制限があるため、回答に追加してください!

ヘッダーを見ていくつかの選択肢

キューに手動で追加できる戻り操作:

- (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)urlRequest

または、カスタム サブクラス操作をこれに渡します。

- (void)enqueueHTTPRequestOperation:(AFHTTPRequestOperation *)operation;
于 2013-02-15T20:20:36.413 に答える
2

このコードを試してください。

  NSMutableURLRequest *apiRequest =
[self multipartFormRequestWithMethod:@"POST"
                                path: pathstr
                          parameters: params
           constructingBodyWithBlock: ^(id <AFMultipartFormData>formData)
           {
               //TODO: attach file if needed
           }];



AFJSONRequestOperation* operation = [[AFJSONRequestOperation alloc] initWithRequest: apiRequest];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    //success!
    completionBlock(responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    //failure :(

    NSLog(@"%@", error);
    completionBlock([NSDictionary dictionaryWithObject:[error localizedDescription] forKey:@"error"]);
}];
operation.securityPolicy.allowInvalidCertificates = YES;
[operation start];
于 2013-10-10T13:08:33.510 に答える