如何修复 setAllowsAnyHTTPS 认证用于 Apple 应用程序提交的私有 API



我使用以下代码将用户登录信息发送到服务器,但Apple表示我使用的是私有API,并将拒绝该应用程序。我该如何解决这个问题?

[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]]; [request setHTTPMethod:@"POST"]; 替换就足够了吗?

    @interface NSURLRequest (DummyInterface)
+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString*)host;
+ (void)setAllowsAnyHTTPSCertificate:(BOOL)allow forHost:(NSString*)host;
@end

和我的登录代码:

            // token url
            NSURL *url=[NSURL URLWithString:@"http://xxxxxxxx.com/api/token"];
            NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
            NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
            NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
            [request setURL:url];
            [request setHTTPMethod:@"POST"];
            [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
            [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
            [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
            [request setHTTPBody:postData];
            NSLog(@"url is %@",url),
            NSLog(@"request is %@",request),

           **[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];**

似乎有一个公共 API。

如何使用 NSURLConnection 与 SSL 连接以获取不受信任的证书?

一定迟到了,但可能会有所帮助。

最新更新