gpt4 book ai didi

ios - 如何在 AFNetworking 2.0 中做一个简单的 form-urlencoded POST 请求?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:52:03 26 4
gpt4 key购买 nike

如何在 AFNetworking 2.0 中复制此 NSURLConnection 代码?

NSString *post = @"key=xxx";
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://test.com/"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[conn start];

最佳答案

简短回答:使用 AFNetworking 提供的 AFHTTPRequestSerializer

根据document :

[[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:URLString parameters:parameters];

发送:

POST http://example.com/
Content-Type: application/x-www-form-urlencoded

foo=bar&baz[]=1&baz[]=2&baz[]=3

如果您正在使用 AFHTTPRequestOperationManager:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];

// you can use different serializer for response.
manager.responseSerializer = [AFJSONResponseSerializer serializer];

关于ios - 如何在 AFNetworking 2.0 中做一个简单的 form-urlencoded POST 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20064028/

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