OStack程序员社区-中国程序员成长平台

标题: ios - 通过 AFNetworking 2.0 发送加密的 JSON [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 17:37
标题: ios - 通过 AFNetworking 2.0 发送加密的 JSON

我正在从 AFNetworking1 迁移到AFNetworking 2.0。`

我必须发送一个加密的 JSON,在我们刚刚发送一个 NSSTring 之前,但现在我们发送一个 NSDictionary 作为 JSON 到 POST。 我一直在寻找,但找不到如何为 2.0 版本做同样的事情。

这是之前的代码:

request = [NSMutableURLRequest requestWithURL:url];
    NSLog(@"url: %@", url);

    NSString *jsonStr = [[NSString alloc] initWithData:
                         [NSJSONSerialization dataWithJSONObject:params
                                                         options:NSJSONReadingMutableLeaves
                                                           error:nil]
                                              encoding:NSUTF8StringEncoding];

    NSLog(@"%@", jsonStr);

    NSData *requestData =  [jsonStr dataUsingEncoding:NSUTF8StringEncoding];//[NSData dataWithBytes:[jsonStr UTF8String] length:[jsonStr length]];


    [request setHTTPMethod"UT"];
    [request setValue"application/json" forHTTPHeaderField"Accept"];
    [request setValue"application/json" forHTTPHeaderField"Content-Type"];
    [request setValue"json" forHTTPHeaderField"Data-Type"];
    [request setValue:[NSString stringWithFormat"%d", [jsonStr length]] forHTTPHeaderField"Content-Length"];
    [request setValue:[self sha256:jsonStr] forHTTPHeaderField"X-Hash"];
    [request setHTTPBody: requestData];

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];
    [operation setAuthenticationChallengeBlock:^(NSURLConnection *connection, NSURLAuthenticationChallenge *challenge) {
        NSURLCredential *newCredential = [NSURLCredential credentialWithUser:USERNAME passwordASSWORD persistence:NSURLCredentialPersistenceForSession];
        [challenge.sender useCredential:newCredential forAuthenticationChallenge:challenge];
    }];

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id response) {

 }

我想在 AFNetworking 2.0.. 上找到相同的功能 如您所见,我添加了一个 X-Hash header ,并使用 sha256 函数发送了 JSON 的加密版本。

谢谢。



Best Answer-推荐答案


NSURLCredential *credential = [NSURLCredential credentialWithUser:@"login" password:@"password" persistence:NSURLCredentialPersistenceForSession];
[operation setCredential:credential];

关于ios - 通过 AFNetworking 2.0 发送加密的 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20839934/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4