NSURLConnection/CFURLConnection HTTP 加载失败(kCFStreamErrorDomainSSL,-9813)

问题描述 投票:0回答:3

我正在尝试在 https 上进行 HTTP 调用。这是我的代码片段。

NSMutableURLRequest *request = [[NSMutableURLRequest alloc]
                                initWithURL:[NSURL
                                             URLWithString:@"https://testservice.fiamm.com/token"]];

[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-type"];

NSString *postString = @"username=TestIphone&Password=T3st1ph$n&Grant_type=password";

[request setValue:[NSString stringWithFormat:@"%d", [postString length]] forHTTPHeaderField:@"Content-length"];

[request setHTTPBody:[postString
                      dataUsingEncoding:NSUTF8StringEncoding]];

// Fetch the JSON response
NSData *urlData;
NSURLResponse *response;
NSError *error;

// Make synchronous request
urlData = [NSURLConnection sendSynchronousRequest:request
                                returningResponse:&response
                                            error:&error];

// Construct a String around the Data from the response
NSString *strFiamm = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];

当我尝试使用 hurl 或 postman 时,我得到了正确的响应,但是当我尝试使用我的代码时,我得到了这个错误。

NSURLConnection/CFURLConnection HTTP 加载失败(kCFStreamErrorDomainSSL,-9813)

任何帮助或建议表示赞赏。!

objective-c iphone http nsurlrequest nsmutableurlrequest
3个回答
0
投票

你在这里提到的链接不是 https 证书所以使用简单的 http 而不是 https

编辑#1

我试过这些值

http://testservice.fiamm.com/token
username=TestIphone
Password=T3st1ph$n
Grant_type=password

输出为

403 - Forbidden: Access is denied.
You do not have permission to view this directory or page using the credentials that you supplied.

这意味着您的用户名密码或键值不正确


0
投票

您没有正确设置

Content-Length
标题。内容的正确长度是以字节为单位的以 UTF-8 编码的
postString
[postString length]
返回 UTF-16 代码点的数量(既不总是 UTF-8 中的字节数,也不总是字符串中的字符数)。

您还应该确保帖子字符串经过正确的 URL 编码,以便与

application/x-www-form-urlencoded
一起使用。您会在 SO 上找到许多关于此的帖子。不过,权威 指南是这样的:URL 编码的表单数据。您应该特别注意第一段中关于
application/x-www-form-urlencoded
表单数据的警告:

“这种表单数据集编码在很多方面都是一种异常的怪物,是多年实施事故和妥协的结果,导致了互操作性所必需的一组要求,但绝不代表良好的设计实践。特别是,读者是警告要密切注意涉及字符编码和字节序列之间重复(在某些情况下嵌套)转换的扭曲细节。”

话虽如此,我强烈建议使用另一种 Content-Type 将数据传递到服务器,例如 JSON。


0
投票

在你的课堂上添加这个方法

-(void) connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *) challenge {

    if ([challenge.protectionSpace.authenticationMethod isEqualToString: NSURLAuthenticationMethodServerTrust])
    {

        NSURL* baseURL = [NSURL URLWithString:@"yourURL”];

        if ([challenge.protectionSpace.host isEqualToString:baseURL.host])
        {
            SecTrustRef trust = challenge.protectionSpace.serverTrust;

            NSURLCredential *cred = [NSURLCredential credentialForTrust:trust];

            [challenge.sender useCredential:cred forAuthenticationChallenge:challenge];

        }
        else
        NSLog(@"Not trusting connection to host %@", challenge.protectionSpace.host);

    }
    [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}
© www.soinside.com 2019 - 2024. All rights reserved.