【问题标题】:Issue with post request with form-data using AFNetworking to https server使用 AFNetworking 向 https 服务器发送带有表单数据的请求的问题
【发布时间】:2015-08-23 20:31:44
【问题描述】:

我遇到了一个我自己无法解决的问题。我正在尝试使用 AFNetworking 使用 https 向我们的后端发出带有表单数据的 POST 请求,但我遇到了一些我不明白的错误。我一直在记录 http 流量,它似乎甚至没有发送请求,只有一个我认为与证书有关的 CONNECT 请求。

这是我的代码:

NSDictionary *params = @{@"client_id"     : @"2",
                             @"grant_type"    : @"password",
                             @"email" : username,
                             @"password" : password};

    AFSecurityPolicy* policy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
    [policy setValidatesDomainName:NO];
    [policy setAllowInvalidCertificates:YES];
    [policy setValidatesDomainName:NO];

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.securityPolicy = policy;
    [manager POST:ssoURL parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"JSON: %@", responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];

这是错误:

Error: Error Domain=NSURLErrorDomain Code=-1012 "The operation couldn’t be completed. (NSURLErrorDomain error -1012.)" UserInfo=0x7fc3b582c4c0 {NSErrorFailingURLKey=https://myaccount-dev.<domain>/oauth/token, NSErrorFailingURLStringKey=https://myaccount-dev.<domain>/oauth/token}

我该如何解决这个问题?

【问题讨论】:

    标签: ios rest ssl afnetworking afnetworking-2


    【解决方案1】:

    您可以在此处允许无效证书:

    AFHTTPClient* client = [AFHTTPClient clientWithBaseURL:@"URL"];
    client.allowsInvalidSSLCertificate = YES;
    

    对于 AFHTTPRequestOperationManager

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.securityPolicy.allowInvalidCertificates = YES;
    

    【讨论】:

    • 仅用于调试。
    • @Grzegorz:我定义了一个附加到我设置标志的 AFHTTPRequestOperationManager 的策略。
    猜你喜欢
    • 2018-10-04
    • 2012-12-12
    • 1970-01-01
    • 2014-09-02
    • 1970-01-01
    • 2020-07-15
    • 2014-11-02
    • 2019-09-24
    • 2021-09-17
    相关资源
    最近更新 更多