Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 目标C HTTPS请求失败?_Ios_Objective C_Json_Https - Fatal编程技术网

Ios 目标C HTTPS请求失败?

Ios 目标C HTTPS请求失败?,ios,objective-c,json,https,Ios,Objective C,Json,Https,我对Objective C相当陌生,甚至对网络API(如NSMutableURLRequest和NSURLSession)也比较新。无论如何,我在调用异步API以发送到HTTPS地址并获取JSON数据包时遇到问题: 我可以从命令行调用curl,并成功获得如下数据包: curl -X POST -H "Content-Type: application/json" -d '{"name":"foo","pass":"bar"}' --insecure https://bla bla bla.com

我对Objective C相当陌生,甚至对网络API(如NSMutableURLRequest和NSURLSession)也比较新。无论如何,我在调用异步API以发送到HTTPS地址并获取JSON数据包时遇到问题:

我可以从命令行调用curl,并成功获得如下数据包:

curl -X POST -H "Content-Type: application/json" -d '{"name":"foo","pass":"bar"}' --insecure https://bla bla bla.com/xxxx
并获取一个json数据包作为对curl的响应:

"{'key1':'val1','key2':'val2','key3':'val3'}
我想在目标C中做同样的事情。 这就是我到目前为止所做的:

#define USER_URI @"https://bla bla bla.com"
    NSDictionary* gistDict = [NSDictionary dictionaryWithObjectsAndKeys:
                              username, @"name",
                              password, @"pass", nil];
    NSError* jsonError;
    NSData* jsonData = [NSJSONSerialization dataWithJSONObject:gistDict options: NSJSONWritingPrettyPrinted error: &jsonError];

    NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString: USER_URI]];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:jsonData];

    NSURLSession* session      = [NSURLSession sharedSession];
    NSURLSessionDataTask* task = [session dataTaskWithRequest:request completionHandler: ^(NSData* data, NSURLResponse* response, NSError* error) {
        NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse *)response;
        BOOL success = data && httpResponse.statusCode == 200;

        if (success) {
            NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error: nil];
            NSLog(@"json dict %@", jsonDictionary);
        } 
    }];
问题是在运行时:数据为0字节,响应为零。块参数
error
返回一条消息,说明:

Error Domain=NSURLErrorDomain Code=-1202 "The operation couldn’t be completed.

这是因为我发布的是HTTPS url而不是HTTP url吗?

HTTPS证书是否由密钥链中的CA签名?您可以使用Carles Proxy检查实际发送和接收的内容,它与SSL一起工作。你有没有写过jsonData,好吗?嗨,我不知道什么是CA。但是当我记录我的jsonData时,它得到了这样一个信息:JSON Data=我认为这没有多大帮助。CA是证书颁发机构,谁签署了你的SSL证书,CA还是自签名的?至于
jsonData
,主要的一点是它不是零。我没有关于CA的更多信息。我必须与其他人核实更多信息。