gpt4 book ai didi

ios - 通过 AFNetworking 2.0 发送加密的 JSON

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:37:41 25 4
gpt4 key购买 nike

我正在从 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:@"PUT"];
[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 password:PASSWORD persistence:NSURLCredentialPersistenceForSession];
[challenge.sender useCredential:newCredential forAuthenticationChallenge:challenge];
}];

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

}

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

谢谢。

最佳答案

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

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com